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