1 //===-- SystemZElimCompare.cpp - Eliminate comparison instructions --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass:
11 // (1) tries to remove compares if CC already contains the required information
12 // (2) fuses compares and branches into COMPARE AND BRANCH instructions
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "SystemZ.h"
17 #include "SystemZInstrInfo.h"
18 #include "SystemZTargetMachine.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineOperand.h"
28 #include "llvm/CodeGen/TargetRegisterInfo.h"
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/MC/MCInstrDesc.h"
31 #include <cassert>
32 #include <cstdint>
33 
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "systemz-elim-compare"
37 
38 STATISTIC(BranchOnCounts, "Number of branch-on-count instructions");
39 STATISTIC(LoadAndTraps, "Number of load-and-trap instructions");
40 STATISTIC(EliminatedComparisons, "Number of eliminated comparisons");
41 STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions");
42 
43 namespace {
44 
45 // Represents the references to a particular register in one or more
46 // instructions.
47 struct Reference {
48   Reference() = default;
49 
50   Reference &operator|=(const Reference &Other) {
51     Def |= Other.Def;
52     Use |= Other.Use;
53     return *this;
54   }
55 
56   explicit operator bool() const { return Def || Use; }
57 
58   // True if the register is defined or used in some form, either directly or
59   // via a sub- or super-register.
60   bool Def = false;
61   bool Use = false;
62 };
63 
64 class SystemZElimCompare : public MachineFunctionPass {
65 public:
66   static char ID;
67 
68   SystemZElimCompare(const SystemZTargetMachine &tm)
69     : MachineFunctionPass(ID) {}
70 
71   StringRef getPassName() const override {
72     return "SystemZ Comparison Elimination";
73   }
74 
75   bool processBlock(MachineBasicBlock &MBB);
76   bool runOnMachineFunction(MachineFunction &F) override;
77 
78   MachineFunctionProperties getRequiredProperties() const override {
79     return MachineFunctionProperties().set(
80         MachineFunctionProperties::Property::NoVRegs);
81   }
82 
83 private:
84   Reference getRegReferences(MachineInstr &MI, unsigned Reg);
85   bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare,
86                      SmallVectorImpl<MachineInstr *> &CCUsers);
87   bool convertToLoadAndTrap(MachineInstr &MI, MachineInstr &Compare,
88                             SmallVectorImpl<MachineInstr *> &CCUsers);
89   bool convertToLoadAndTest(MachineInstr &MI);
90   bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare,
91                              SmallVectorImpl<MachineInstr *> &CCUsers);
92   bool optimizeCompareZero(MachineInstr &Compare,
93                            SmallVectorImpl<MachineInstr *> &CCUsers);
94   bool fuseCompareOperations(MachineInstr &Compare,
95                              SmallVectorImpl<MachineInstr *> &CCUsers);
96 
97   const SystemZInstrInfo *TII = nullptr;
98   const TargetRegisterInfo *TRI = nullptr;
99 };
100 
101 char SystemZElimCompare::ID = 0;
102 
103 } // end anonymous namespace
104 
105 // Return true if CC is live out of MBB.
106 static bool isCCLiveOut(MachineBasicBlock &MBB) {
107   for (auto SI = MBB.succ_begin(), SE = MBB.succ_end(); SI != SE; ++SI)
108     if ((*SI)->isLiveIn(SystemZ::CC))
109       return true;
110   return false;
111 }
112 
113 // Returns true if MI is an instruction whose output equals the value in Reg.
114 static bool preservesValueOf(MachineInstr &MI, unsigned Reg) {
115   switch (MI.getOpcode()) {
116   case SystemZ::LR:
117   case SystemZ::LGR:
118   case SystemZ::LGFR:
119   case SystemZ::LTR:
120   case SystemZ::LTGR:
121   case SystemZ::LTGFR:
122   case SystemZ::LER:
123   case SystemZ::LDR:
124   case SystemZ::LXR:
125   case SystemZ::LTEBR:
126   case SystemZ::LTDBR:
127   case SystemZ::LTXBR:
128     if (MI.getOperand(1).getReg() == Reg)
129       return true;
130   }
131 
132   return false;
133 }
134 
135 // Return true if any CC result of MI would (perhaps after conversion)
136 // reflect the value of Reg.
137 static bool resultTests(MachineInstr &MI, unsigned Reg) {
138   if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() &&
139       MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg)
140     return true;
141 
142   return (preservesValueOf(MI, Reg));
143 }
144 
145 // Describe the references to Reg or any of its aliases in MI.
146 Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) {
147   Reference Ref;
148   for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
149     const MachineOperand &MO = MI.getOperand(I);
150     if (MO.isReg()) {
151       if (unsigned MOReg = MO.getReg()) {
152         if (TRI->regsOverlap(MOReg, Reg)) {
153           if (MO.isUse())
154             Ref.Use = true;
155           else if (MO.isDef())
156             Ref.Def = true;
157         }
158       }
159     }
160   }
161   return Ref;
162 }
163 
164 // Return true if this is a load and test which can be optimized the
165 // same way as compare instruction.
166 static bool isLoadAndTestAsCmp(MachineInstr &MI) {
167   // If we during isel used a load-and-test as a compare with 0, the
168   // def operand is dead.
169   return (MI.getOpcode() == SystemZ::LTEBR ||
170           MI.getOpcode() == SystemZ::LTDBR ||
171           MI.getOpcode() == SystemZ::LTXBR) &&
172          MI.getOperand(0).isDead();
173 }
174 
175 // Return the source register of Compare, which is the unknown value
176 // being tested.
177 static unsigned getCompareSourceReg(MachineInstr &Compare) {
178   unsigned reg = 0;
179   if (Compare.isCompare())
180     reg = Compare.getOperand(0).getReg();
181   else if (isLoadAndTestAsCmp(Compare))
182     reg = Compare.getOperand(1).getReg();
183   assert(reg);
184 
185   return reg;
186 }
187 
188 // Compare compares the result of MI against zero.  If MI is an addition
189 // of -1 and if CCUsers is a single branch on nonzero, eliminate the addition
190 // and convert the branch to a BRCT(G) or BRCTH.  Return true on success.
191 bool SystemZElimCompare::convertToBRCT(
192     MachineInstr &MI, MachineInstr &Compare,
193     SmallVectorImpl<MachineInstr *> &CCUsers) {
194   // Check whether we have an addition of -1.
195   unsigned Opcode = MI.getOpcode();
196   unsigned BRCT;
197   if (Opcode == SystemZ::AHI)
198     BRCT = SystemZ::BRCT;
199   else if (Opcode == SystemZ::AGHI)
200     BRCT = SystemZ::BRCTG;
201   else if (Opcode == SystemZ::AIH)
202     BRCT = SystemZ::BRCTH;
203   else
204     return false;
205   if (MI.getOperand(2).getImm() != -1)
206     return false;
207 
208   // Check whether we have a single JLH.
209   if (CCUsers.size() != 1)
210     return false;
211   MachineInstr *Branch = CCUsers[0];
212   if (Branch->getOpcode() != SystemZ::BRC ||
213       Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
214       Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE)
215     return false;
216 
217   // We already know that there are no references to the register between
218   // MI and Compare.  Make sure that there are also no references between
219   // Compare and Branch.
220   unsigned SrcReg = getCompareSourceReg(Compare);
221   MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
222   for (++MBBI; MBBI != MBBE; ++MBBI)
223     if (getRegReferences(*MBBI, SrcReg))
224       return false;
225 
226   // The transformation is OK.  Rebuild Branch as a BRCT(G) or BRCTH.
227   MachineOperand Target(Branch->getOperand(2));
228   while (Branch->getNumOperands())
229     Branch->RemoveOperand(0);
230   Branch->setDesc(TII->get(BRCT));
231   MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
232   MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target);
233   // Add a CC def to BRCT(G), since we may have to split them again if the
234   // branch displacement overflows.  BRCTH has a 32-bit displacement, so
235   // this is not necessary there.
236   if (BRCT != SystemZ::BRCTH)
237     MIB.addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
238   MI.eraseFromParent();
239   return true;
240 }
241 
242 // Compare compares the result of MI against zero.  If MI is a suitable load
243 // instruction and if CCUsers is a single conditional trap on zero, eliminate
244 // the load and convert the branch to a load-and-trap.  Return true on success.
245 bool SystemZElimCompare::convertToLoadAndTrap(
246     MachineInstr &MI, MachineInstr &Compare,
247     SmallVectorImpl<MachineInstr *> &CCUsers) {
248   unsigned LATOpcode = TII->getLoadAndTrap(MI.getOpcode());
249   if (!LATOpcode)
250     return false;
251 
252   // Check whether we have a single CondTrap that traps on zero.
253   if (CCUsers.size() != 1)
254     return false;
255   MachineInstr *Branch = CCUsers[0];
256   if (Branch->getOpcode() != SystemZ::CondTrap ||
257       Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
258       Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_EQ)
259     return false;
260 
261   // We already know that there are no references to the register between
262   // MI and Compare.  Make sure that there are also no references between
263   // Compare and Branch.
264   unsigned SrcReg = getCompareSourceReg(Compare);
265   MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
266   for (++MBBI; MBBI != MBBE; ++MBBI)
267     if (getRegReferences(*MBBI, SrcReg))
268       return false;
269 
270   // The transformation is OK.  Rebuild Branch as a load-and-trap.
271   while (Branch->getNumOperands())
272     Branch->RemoveOperand(0);
273   Branch->setDesc(TII->get(LATOpcode));
274   MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
275       .add(MI.getOperand(0))
276       .add(MI.getOperand(1))
277       .add(MI.getOperand(2))
278       .add(MI.getOperand(3));
279   MI.eraseFromParent();
280   return true;
281 }
282 
283 // If MI is a load instruction, try to convert it into a LOAD AND TEST.
284 // Return true on success.
285 bool SystemZElimCompare::convertToLoadAndTest(MachineInstr &MI) {
286   unsigned Opcode = TII->getLoadAndTest(MI.getOpcode());
287   if (!Opcode)
288     return false;
289 
290   MI.setDesc(TII->get(Opcode));
291   MachineInstrBuilder(*MI.getParent()->getParent(), MI)
292       .addReg(SystemZ::CC, RegState::ImplicitDefine);
293   return true;
294 }
295 
296 // The CC users in CCUsers are testing the result of a comparison of some
297 // value X against zero and we know that any CC value produced by MI
298 // would also reflect the value of X.  Try to adjust CCUsers so that
299 // they test the result of MI directly, returning true on success.
300 // Leave everything unchanged on failure.
301 bool SystemZElimCompare::adjustCCMasksForInstr(
302     MachineInstr &MI, MachineInstr &Compare,
303     SmallVectorImpl<MachineInstr *> &CCUsers) {
304   int Opcode = MI.getOpcode();
305   const MCInstrDesc &Desc = TII->get(Opcode);
306   unsigned MIFlags = Desc.TSFlags;
307 
308   // See which compare-style condition codes are available.
309   unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags);
310 
311   // For unsigned comparisons with zero, only equality makes sense.
312   unsigned CompareFlags = Compare.getDesc().TSFlags;
313   if (CompareFlags & SystemZII::IsLogical)
314     ReusableCCMask &= SystemZ::CCMASK_CMP_EQ;
315 
316   if (ReusableCCMask == 0)
317     return false;
318 
319   unsigned CCValues = SystemZII::getCCValues(MIFlags);
320   assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues");
321 
322   // Now check whether these flags are enough for all users.
323   SmallVector<MachineOperand *, 4> AlterMasks;
324   for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) {
325     MachineInstr *MI = CCUsers[I];
326 
327     // Fail if this isn't a use of CC that we understand.
328     unsigned Flags = MI->getDesc().TSFlags;
329     unsigned FirstOpNum;
330     if (Flags & SystemZII::CCMaskFirst)
331       FirstOpNum = 0;
332     else if (Flags & SystemZII::CCMaskLast)
333       FirstOpNum = MI->getNumExplicitOperands() - 2;
334     else
335       return false;
336 
337     // Check whether the instruction predicate treats all CC values
338     // outside of ReusableCCMask in the same way.  In that case it
339     // doesn't matter what those CC values mean.
340     unsigned CCValid = MI->getOperand(FirstOpNum).getImm();
341     unsigned CCMask = MI->getOperand(FirstOpNum + 1).getImm();
342     unsigned OutValid = ~ReusableCCMask & CCValid;
343     unsigned OutMask = ~ReusableCCMask & CCMask;
344     if (OutMask != 0 && OutMask != OutValid)
345       return false;
346 
347     AlterMasks.push_back(&MI->getOperand(FirstOpNum));
348     AlterMasks.push_back(&MI->getOperand(FirstOpNum + 1));
349   }
350 
351   // All users are OK.  Adjust the masks for MI.
352   for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) {
353     AlterMasks[I]->setImm(CCValues);
354     unsigned CCMask = AlterMasks[I + 1]->getImm();
355     if (CCMask & ~ReusableCCMask)
356       AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) |
357                                 (CCValues & ~ReusableCCMask));
358   }
359 
360   // CC is now live after MI.
361   int CCDef = MI.findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI);
362   assert(CCDef >= 0 && "Couldn't find CC set");
363   MI.getOperand(CCDef).setIsDead(false);
364 
365   // Clear any intervening kills of CC.
366   MachineBasicBlock::iterator MBBI = MI, MBBE = Compare;
367   for (++MBBI; MBBI != MBBE; ++MBBI)
368     MBBI->clearRegisterKills(SystemZ::CC, TRI);
369 
370   return true;
371 }
372 
373 // Return true if Compare is a comparison against zero.
374 static bool isCompareZero(MachineInstr &Compare) {
375   switch (Compare.getOpcode()) {
376   case SystemZ::LTEBRCompare:
377   case SystemZ::LTDBRCompare:
378   case SystemZ::LTXBRCompare:
379     return true;
380 
381   default:
382     if (isLoadAndTestAsCmp(Compare))
383       return true;
384     return Compare.getNumExplicitOperands() == 2 &&
385            Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0;
386   }
387 }
388 
389 // Try to optimize cases where comparison instruction Compare is testing
390 // a value against zero.  Return true on success and if Compare should be
391 // deleted as dead.  CCUsers is the list of instructions that use the CC
392 // value produced by Compare.
393 bool SystemZElimCompare::optimizeCompareZero(
394     MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
395   if (!isCompareZero(Compare))
396     return false;
397 
398   // Search back for CC results that are based on the first operand.
399   unsigned SrcReg = getCompareSourceReg(Compare);
400   MachineBasicBlock &MBB = *Compare.getParent();
401   MachineBasicBlock::iterator MBBI = Compare, MBBE = MBB.begin();
402   Reference CCRefs;
403   Reference SrcRefs;
404   while (MBBI != MBBE) {
405     --MBBI;
406     MachineInstr &MI = *MBBI;
407     if (resultTests(MI, SrcReg)) {
408       // Try to remove both MI and Compare by converting a branch to BRCT(G).
409       // or a load-and-trap instruction.  We don't care in this case whether
410       // CC is modified between MI and Compare.
411       if (!CCRefs.Use && !SrcRefs) {
412         if (convertToBRCT(MI, Compare, CCUsers)) {
413           BranchOnCounts += 1;
414           return true;
415         }
416         if (convertToLoadAndTrap(MI, Compare, CCUsers)) {
417           LoadAndTraps += 1;
418           return true;
419         }
420       }
421       // Try to eliminate Compare by reusing a CC result from MI.
422       if ((!CCRefs && convertToLoadAndTest(MI)) ||
423           (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) {
424         EliminatedComparisons += 1;
425         return true;
426       }
427     }
428     SrcRefs |= getRegReferences(MI, SrcReg);
429     if (SrcRefs.Def)
430       break;
431     CCRefs |= getRegReferences(MI, SystemZ::CC);
432     if (CCRefs.Use && CCRefs.Def)
433       break;
434   }
435 
436   // Also do a forward search to handle cases where an instruction after the
437   // compare can be converted like
438   //
439   // LTEBRCompare %F0S, %F0S, %CC<imp-def> LTEBRCompare %F0S, %F0S, %CC<imp-def>
440   // %F2S<def> = LER %F0S
441   //
442   MBBI = Compare, MBBE = MBB.end();
443   while (++MBBI != MBBE) {
444     MachineInstr &MI = *MBBI;
445     if (preservesValueOf(MI, SrcReg)) {
446       // Try to eliminate Compare by reusing a CC result from MI.
447       if (convertToLoadAndTest(MI)) {
448         EliminatedComparisons += 1;
449         return true;
450       }
451     }
452     if (getRegReferences(MI, SrcReg).Def)
453       return false;
454     if (getRegReferences(MI, SystemZ::CC))
455       return false;
456   }
457 
458   return false;
459 }
460 
461 // Try to fuse comparison instruction Compare into a later branch.
462 // Return true on success and if Compare is therefore redundant.
463 bool SystemZElimCompare::fuseCompareOperations(
464     MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
465   // See whether we have a single branch with which to fuse.
466   if (CCUsers.size() != 1)
467     return false;
468   MachineInstr *Branch = CCUsers[0];
469   SystemZII::FusedCompareType Type;
470   switch (Branch->getOpcode()) {
471   case SystemZ::BRC:
472     Type = SystemZII::CompareAndBranch;
473     break;
474   case SystemZ::CondReturn:
475     Type = SystemZII::CompareAndReturn;
476     break;
477   case SystemZ::CallBCR:
478     Type = SystemZII::CompareAndSibcall;
479     break;
480   case SystemZ::CondTrap:
481     Type = SystemZII::CompareAndTrap;
482     break;
483   default:
484     return false;
485   }
486 
487   // See whether we have a comparison that can be fused.
488   unsigned FusedOpcode =
489       TII->getFusedCompare(Compare.getOpcode(), Type, &Compare);
490   if (!FusedOpcode)
491     return false;
492 
493   // Make sure that the operands are available at the branch.
494   // SrcReg2 is the register if the source operand is a register,
495   // 0 if the source operand is immediate, and the base register
496   // if the source operand is memory (index is not supported).
497   unsigned SrcReg = Compare.getOperand(0).getReg();
498   unsigned SrcReg2 =
499       Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : 0;
500   MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
501   for (++MBBI; MBBI != MBBE; ++MBBI)
502     if (MBBI->modifiesRegister(SrcReg, TRI) ||
503         (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
504       return false;
505 
506   // Read the branch mask, target (if applicable), regmask (if applicable).
507   MachineOperand CCMask(MBBI->getOperand(1));
508   assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
509          "Invalid condition-code mask for integer comparison");
510   // This is only valid for CompareAndBranch.
511   MachineOperand Target(MBBI->getOperand(
512     Type == SystemZII::CompareAndBranch ? 2 : 0));
513   const uint32_t *RegMask;
514   if (Type == SystemZII::CompareAndSibcall)
515     RegMask = MBBI->getOperand(2).getRegMask();
516 
517   // Clear out all current operands.
518   int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
519   assert(CCUse >= 0 && "BRC/BCR must use CC");
520   Branch->RemoveOperand(CCUse);
521   // Remove target (branch) or regmask (sibcall).
522   if (Type == SystemZII::CompareAndBranch ||
523       Type == SystemZII::CompareAndSibcall)
524     Branch->RemoveOperand(2);
525   Branch->RemoveOperand(1);
526   Branch->RemoveOperand(0);
527 
528   // Rebuild Branch as a fused compare and branch.
529   // SrcNOps is the number of MI operands of the compare instruction
530   // that we need to copy over.
531   unsigned SrcNOps = 2;
532   if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT)
533     SrcNOps = 3;
534   Branch->setDesc(TII->get(FusedOpcode));
535   MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
536   for (unsigned I = 0; I < SrcNOps; I++)
537     MIB.add(Compare.getOperand(I));
538   MIB.add(CCMask);
539 
540   if (Type == SystemZII::CompareAndBranch) {
541     // Only conditional branches define CC, as they may be converted back
542     // to a non-fused branch because of a long displacement.  Conditional
543     // returns don't have that problem.
544     MIB.add(Target).addReg(SystemZ::CC,
545                            RegState::ImplicitDefine | RegState::Dead);
546   }
547 
548   if (Type == SystemZII::CompareAndSibcall)
549     MIB.addRegMask(RegMask);
550 
551   // Clear any intervening kills of SrcReg and SrcReg2.
552   MBBI = Compare;
553   for (++MBBI; MBBI != MBBE; ++MBBI) {
554     MBBI->clearRegisterKills(SrcReg, TRI);
555     if (SrcReg2)
556       MBBI->clearRegisterKills(SrcReg2, TRI);
557   }
558   FusedComparisons += 1;
559   return true;
560 }
561 
562 // Process all comparison instructions in MBB.  Return true if something
563 // changed.
564 bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) {
565   bool Changed = false;
566 
567   // Walk backwards through the block looking for comparisons, recording
568   // all CC users as we go.  The subroutines can delete Compare and
569   // instructions before it.
570   bool CompleteCCUsers = !isCCLiveOut(MBB);
571   SmallVector<MachineInstr *, 4> CCUsers;
572   MachineBasicBlock::iterator MBBI = MBB.end();
573   while (MBBI != MBB.begin()) {
574     MachineInstr &MI = *--MBBI;
575     if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) &&
576         (optimizeCompareZero(MI, CCUsers) ||
577          fuseCompareOperations(MI, CCUsers))) {
578       ++MBBI;
579       MI.eraseFromParent();
580       Changed = true;
581       CCUsers.clear();
582       continue;
583     }
584 
585     if (MI.definesRegister(SystemZ::CC)) {
586       CCUsers.clear();
587       CompleteCCUsers = true;
588     }
589     if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers)
590       CCUsers.push_back(&MI);
591   }
592   return Changed;
593 }
594 
595 bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) {
596   if (skipFunction(*F.getFunction()))
597     return false;
598 
599   TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
600   TRI = &TII->getRegisterInfo();
601 
602   bool Changed = false;
603   for (auto &MBB : F)
604     Changed |= processBlock(MBB);
605 
606   return Changed;
607 }
608 
609 FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) {
610   return new SystemZElimCompare(TM);
611 }
612