blob: 89edf535fe09614c1431003b41ba3f9a39f5bc62 [file] [log] [blame]
Richard Sandifordbdbb8af2013-08-05 10:58:53 +00001//===-- 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
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000016#include "SystemZTargetMachine.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/IR/Function.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000021#include "llvm/Support/MathExtras.h"
22#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetRegisterInfo.h"
25
26using namespace llvm;
27
Chandler Carruth84e68b22014-04-22 02:41:26 +000028#define DEBUG_TYPE "systemz-elim-compare"
29
Richard Sandifordc2121252013-08-05 11:23:46 +000030STATISTIC(BranchOnCounts, "Number of branch-on-count instructions");
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000031STATISTIC(EliminatedComparisons, "Number of eliminated comparisons");
32STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions");
33
34namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +000035// Represents the references to a particular register in one or more
36// instructions.
37struct Reference {
38 Reference()
Jonas Paulssonee3685f2015-10-09 11:27:44 +000039 : Def(false), Use(false) {}
Richard Sandifordc2121252013-08-05 11:23:46 +000040
Richard Sandifordc2312692014-03-06 10:38:30 +000041 Reference &operator|=(const Reference &Other) {
42 Def |= Other.Def;
Richard Sandifordc2312692014-03-06 10:38:30 +000043 Use |= Other.Use;
Richard Sandifordc2312692014-03-06 10:38:30 +000044 return *this;
45 }
Richard Sandifordc2121252013-08-05 11:23:46 +000046
Aaron Ballmanb46962f2015-02-15 22:00:20 +000047 explicit operator bool() const { return Def || Use; }
Richard Sandifordc2121252013-08-05 11:23:46 +000048
Richard Sandifordc2312692014-03-06 10:38:30 +000049 // True if the register is defined or used in some form, either directly or
50 // via a sub- or super-register.
51 bool Def;
52 bool Use;
Richard Sandifordc2312692014-03-06 10:38:30 +000053};
Richard Sandifordc2121252013-08-05 11:23:46 +000054
Richard Sandifordc2312692014-03-06 10:38:30 +000055class SystemZElimCompare : public MachineFunctionPass {
56public:
57 static char ID;
58 SystemZElimCompare(const SystemZTargetMachine &tm)
Craig Topper062a2ba2014-04-25 05:30:21 +000059 : MachineFunctionPass(ID), TII(nullptr), TRI(nullptr) {}
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000060
Richard Sandifordb4d67b52014-03-06 12:03:36 +000061 const char *getPassName() const override {
Richard Sandifordc2312692014-03-06 10:38:30 +000062 return "SystemZ Comparison Elimination";
63 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000064
Richard Sandiford28c111e2014-03-06 11:00:15 +000065 bool processBlock(MachineBasicBlock &MBB);
Craig Topper9d74a5a2014-04-29 07:58:41 +000066 bool runOnMachineFunction(MachineFunction &F) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000067 MachineFunctionProperties getRequiredProperties() const override {
68 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000069 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000070 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000071
Richard Sandifordc2312692014-03-06 10:38:30 +000072private:
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000073 Reference getRegReferences(MachineInstr &MI, unsigned Reg);
74 bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare,
Richard Sandifordc2312692014-03-06 10:38:30 +000075 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000076 bool convertToLoadAndTest(MachineInstr &MI);
77 bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare,
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000078 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000079 bool optimizeCompareZero(MachineInstr &Compare,
Richard Sandifordc2312692014-03-06 10:38:30 +000080 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000081 bool fuseCompareOperations(MachineInstr &Compare,
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +000082 SmallVectorImpl<MachineInstr *> &CCUsers);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000083
Richard Sandifordc2312692014-03-06 10:38:30 +000084 const SystemZInstrInfo *TII;
85 const TargetRegisterInfo *TRI;
86};
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000087
Richard Sandifordc2312692014-03-06 10:38:30 +000088char SystemZElimCompare::ID = 0;
89} // end anonymous namespace
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000090
91FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) {
92 return new SystemZElimCompare(TM);
93}
94
95// Return true if CC is live out of MBB.
Richard Sandiford28c111e2014-03-06 11:00:15 +000096static bool isCCLiveOut(MachineBasicBlock &MBB) {
97 for (auto SI = MBB.succ_begin(), SE = MBB.succ_end(); SI != SE; ++SI)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000098 if ((*SI)->isLiveIn(SystemZ::CC))
99 return true;
100 return false;
101}
102
Jonas Paulsson2c96dd62015-10-08 07:40:11 +0000103// Return true if any CC result of MI would reflect the value of Reg.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000104static bool resultTests(MachineInstr &MI, unsigned Reg) {
105 if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() &&
106 MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000107 return true;
108
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000109 switch (MI.getOpcode()) {
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000110 case SystemZ::LR:
111 case SystemZ::LGR:
112 case SystemZ::LGFR:
113 case SystemZ::LTR:
114 case SystemZ::LTGR:
115 case SystemZ::LTGFR:
Richard Sandiford0897fce2013-08-07 11:10:06 +0000116 case SystemZ::LER:
117 case SystemZ::LDR:
118 case SystemZ::LXR:
119 case SystemZ::LTEBR:
120 case SystemZ::LTDBR:
121 case SystemZ::LTXBR:
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000122 if (MI.getOperand(1).getReg() == Reg)
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000123 return true;
124 }
125
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000126 return false;
127}
128
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000129// Describe the references to Reg or any of its aliases in MI.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000130Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000131 Reference Ref;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000132 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
133 const MachineOperand &MO = MI.getOperand(I);
Richard Sandifordc2121252013-08-05 11:23:46 +0000134 if (MO.isReg()) {
135 if (unsigned MOReg = MO.getReg()) {
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000136 if (TRI->regsOverlap(MOReg, Reg)) {
137 if (MO.isUse())
Richard Sandifordc2121252013-08-05 11:23:46 +0000138 Ref.Use = true;
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000139 else if (MO.isDef())
Richard Sandifordc2121252013-08-05 11:23:46 +0000140 Ref.Def = true;
Richard Sandifordc2121252013-08-05 11:23:46 +0000141 }
142 }
143 }
144 }
145 return Ref;
146}
147
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000148// Return true if this is a load and test which can be optimized the
149// same way as compare instruction.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000150static bool isLoadAndTestAsCmp(MachineInstr &MI) {
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000151 // If we during isel used a load-and-test as a compare with 0, the
152 // def operand is dead.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000153 return (MI.getOpcode() == SystemZ::LTEBR ||
154 MI.getOpcode() == SystemZ::LTDBR ||
155 MI.getOpcode() == SystemZ::LTXBR) &&
156 MI.getOperand(0).isDead();
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000157}
158
159// Return the source register of Compare, which is the unknown value
160// being tested.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000161static unsigned getCompareSourceReg(MachineInstr &Compare) {
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000162 unsigned reg = 0;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000163 if (Compare.isCompare())
164 reg = Compare.getOperand(0).getReg();
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000165 else if (isLoadAndTestAsCmp(Compare))
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000166 reg = Compare.getOperand(1).getReg();
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000167 assert (reg);
168
169 return reg;
170}
171
Richard Sandifordc2121252013-08-05 11:23:46 +0000172// Compare compares the result of MI against zero. If MI is an addition
173// of -1 and if CCUsers is a single branch on nonzero, eliminate the addition
174// and convert the branch to a BRCT(G). Return true on success.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000175bool SystemZElimCompare::convertToBRCT(
176 MachineInstr &MI, MachineInstr &Compare,
177 SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000178 // Check whether we have an addition of -1.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000179 unsigned Opcode = MI.getOpcode();
Richard Sandifordc2121252013-08-05 11:23:46 +0000180 unsigned BRCT;
181 if (Opcode == SystemZ::AHI)
182 BRCT = SystemZ::BRCT;
183 else if (Opcode == SystemZ::AGHI)
184 BRCT = SystemZ::BRCTG;
185 else
186 return false;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000187 if (MI.getOperand(2).getImm() != -1)
Richard Sandifordc2121252013-08-05 11:23:46 +0000188 return false;
189
190 // Check whether we have a single JLH.
191 if (CCUsers.size() != 1)
192 return false;
193 MachineInstr *Branch = CCUsers[0];
194 if (Branch->getOpcode() != SystemZ::BRC ||
195 Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
196 Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE)
197 return false;
198
199 // We already know that there are no references to the register between
200 // MI and Compare. Make sure that there are also no references between
201 // Compare and Branch.
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000202 unsigned SrcReg = getCompareSourceReg(Compare);
Richard Sandifordc2121252013-08-05 11:23:46 +0000203 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
204 for (++MBBI; MBBI != MBBE; ++MBBI)
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000205 if (getRegReferences(*MBBI, SrcReg))
Richard Sandifordc2121252013-08-05 11:23:46 +0000206 return false;
207
208 // The transformation is OK. Rebuild Branch as a BRCT(G).
209 MachineOperand Target(Branch->getOperand(2));
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000210 while (Branch->getNumOperands())
211 Branch->RemoveOperand(0);
Richard Sandifordc2121252013-08-05 11:23:46 +0000212 Branch->setDesc(TII->get(BRCT));
213 MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000214 .addOperand(MI.getOperand(0))
215 .addOperand(MI.getOperand(1))
216 .addOperand(Target)
217 .addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
218 MI.eraseFromParent();
Richard Sandifordc2121252013-08-05 11:23:46 +0000219 return true;
220}
221
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000222// If MI is a load instruction, try to convert it into a LOAD AND TEST.
223// Return true on success.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000224bool SystemZElimCompare::convertToLoadAndTest(MachineInstr &MI) {
225 unsigned Opcode = TII->getLoadAndTest(MI.getOpcode());
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000226 if (!Opcode)
227 return false;
228
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000229 MI.setDesc(TII->get(Opcode));
230 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
231 .addReg(SystemZ::CC, RegState::ImplicitDefine);
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000232 return true;
233}
234
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000235// The CC users in CCUsers are testing the result of a comparison of some
236// value X against zero and we know that any CC value produced by MI
237// would also reflect the value of X. Try to adjust CCUsers so that
238// they test the result of MI directly, returning true on success.
239// Leave everything unchanged on failure.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000240bool SystemZElimCompare::adjustCCMasksForInstr(
241 MachineInstr &MI, MachineInstr &Compare,
242 SmallVectorImpl<MachineInstr *> &CCUsers) {
243 int Opcode = MI.getOpcode();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000244 const MCInstrDesc &Desc = TII->get(Opcode);
245 unsigned MIFlags = Desc.TSFlags;
246
247 // See which compare-style condition codes are available.
Richard Sandiford0897fce2013-08-07 11:10:06 +0000248 unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000249
250 // For unsigned comparisons with zero, only equality makes sense.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000251 unsigned CompareFlags = Compare.getDesc().TSFlags;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000252 if (CompareFlags & SystemZII::IsLogical)
253 ReusableCCMask &= SystemZ::CCMASK_CMP_EQ;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000254
255 if (ReusableCCMask == 0)
256 return false;
257
258 unsigned CCValues = SystemZII::getCCValues(MIFlags);
259 assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues");
260
261 // Now check whether these flags are enough for all users.
262 SmallVector<MachineOperand *, 4> AlterMasks;
263 for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) {
264 MachineInstr *MI = CCUsers[I];
265
266 // Fail if this isn't a use of CC that we understand.
267 unsigned Flags = MI->getDesc().TSFlags;
268 unsigned FirstOpNum;
269 if (Flags & SystemZII::CCMaskFirst)
270 FirstOpNum = 0;
271 else if (Flags & SystemZII::CCMaskLast)
272 FirstOpNum = MI->getNumExplicitOperands() - 2;
273 else
274 return false;
275
276 // Check whether the instruction predicate treats all CC values
277 // outside of ReusableCCMask in the same way. In that case it
278 // doesn't matter what those CC values mean.
279 unsigned CCValid = MI->getOperand(FirstOpNum).getImm();
280 unsigned CCMask = MI->getOperand(FirstOpNum + 1).getImm();
281 unsigned OutValid = ~ReusableCCMask & CCValid;
282 unsigned OutMask = ~ReusableCCMask & CCMask;
283 if (OutMask != 0 && OutMask != OutValid)
284 return false;
285
286 AlterMasks.push_back(&MI->getOperand(FirstOpNum));
287 AlterMasks.push_back(&MI->getOperand(FirstOpNum + 1));
288 }
289
290 // All users are OK. Adjust the masks for MI.
291 for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) {
292 AlterMasks[I]->setImm(CCValues);
293 unsigned CCMask = AlterMasks[I + 1]->getImm();
294 if (CCMask & ~ReusableCCMask)
295 AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) |
296 (CCValues & ~ReusableCCMask));
297 }
298
299 // CC is now live after MI.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000300 int CCDef = MI.findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000301 assert(CCDef >= 0 && "Couldn't find CC set");
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000302 MI.getOperand(CCDef).setIsDead(false);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000303
304 // Clear any intervening kills of CC.
305 MachineBasicBlock::iterator MBBI = MI, MBBE = Compare;
306 for (++MBBI; MBBI != MBBE; ++MBBI)
307 MBBI->clearRegisterKills(SystemZ::CC, TRI);
308
309 return true;
310}
311
Richard Sandiford0897fce2013-08-07 11:10:06 +0000312// Return true if Compare is a comparison against zero.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000313static bool isCompareZero(MachineInstr &Compare) {
314 switch (Compare.getOpcode()) {
Richard Sandiford0897fce2013-08-07 11:10:06 +0000315 case SystemZ::LTEBRCompare:
316 case SystemZ::LTDBRCompare:
317 case SystemZ::LTXBRCompare:
318 return true;
319
320 default:
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000321
322 if (isLoadAndTestAsCmp(Compare))
323 return true;
324
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000325 return Compare.getNumExplicitOperands() == 2 &&
326 Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000327 }
328}
329
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000330// Try to optimize cases where comparison instruction Compare is testing
331// a value against zero. Return true on success and if Compare should be
332// deleted as dead. CCUsers is the list of instructions that use the CC
333// value produced by Compare.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000334bool SystemZElimCompare::optimizeCompareZero(
335 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandiford0897fce2013-08-07 11:10:06 +0000336 if (!isCompareZero(Compare))
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000337 return false;
338
339 // Search back for CC results that are based on the first operand.
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000340 unsigned SrcReg = getCompareSourceReg(Compare);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000341 MachineBasicBlock &MBB = *Compare.getParent();
Richard Sandiford28c111e2014-03-06 11:00:15 +0000342 MachineBasicBlock::iterator MBBI = Compare, MBBE = MBB.begin();
Richard Sandifordc2121252013-08-05 11:23:46 +0000343 Reference CCRefs;
344 Reference SrcRefs;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000345 while (MBBI != MBBE) {
346 --MBBI;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000347 MachineInstr &MI = *MBBI;
Jonas Paulsson2c96dd62015-10-08 07:40:11 +0000348 if (resultTests(MI, SrcReg)) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000349 // Try to remove both MI and Compare by converting a branch to BRCT(G).
350 // We don't care in this case whether CC is modified between MI and
351 // Compare.
352 if (!CCRefs.Use && !SrcRefs && convertToBRCT(MI, Compare, CCUsers)) {
353 BranchOnCounts += 1;
354 return true;
355 }
356 // Try to eliminate Compare by reusing a CC result from MI.
357 if ((!CCRefs && convertToLoadAndTest(MI)) ||
358 (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) {
359 EliminatedComparisons += 1;
360 return true;
361 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000362 }
Richard Sandifordc2121252013-08-05 11:23:46 +0000363 SrcRefs |= getRegReferences(MI, SrcReg);
364 if (SrcRefs.Def)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000365 return false;
Richard Sandifordc2121252013-08-05 11:23:46 +0000366 CCRefs |= getRegReferences(MI, SystemZ::CC);
367 if (CCRefs.Use && CCRefs.Def)
368 return false;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000369 }
370 return false;
371}
372
373// Try to fuse comparison instruction Compare into a later branch.
374// Return true on success and if Compare is therefore redundant.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000375bool SystemZElimCompare::fuseCompareOperations(
376 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000377 // See whether we have a single branch with which to fuse.
378 if (CCUsers.size() != 1)
379 return false;
380 MachineInstr *Branch = CCUsers[0];
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000381 SystemZII::FusedCompareType Type;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000382 switch (Branch->getOpcode()) {
383 case SystemZ::BRC:
384 Type = SystemZII::CompareAndBranch;
385 break;
386 case SystemZ::CondReturn:
387 Type = SystemZII::CompareAndReturn;
388 break;
Ulrich Weigand848a5132016-04-11 12:12:32 +0000389 case SystemZ::CallBCR:
390 Type = SystemZII::CompareAndSibcall;
391 break;
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000392 case SystemZ::CondTrap:
393 Type = SystemZII::CompareAndTrap;
394 break;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000395 default:
396 return false;
397 }
398
399 // See whether we have a comparison that can be fused.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000400 unsigned FusedOpcode =
401 TII->getFusedCompare(Compare.getOpcode(), Type, &Compare);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000402 if (!FusedOpcode)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000403 return false;
404
405 // Make sure that the operands are available at the branch.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000406 unsigned SrcReg = Compare.getOperand(0).getReg();
407 unsigned SrcReg2 =
408 Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : 0;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000409 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
410 for (++MBBI; MBBI != MBBE; ++MBBI)
411 if (MBBI->modifiesRegister(SrcReg, TRI) ||
412 (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
413 return false;
414
Ulrich Weigand848a5132016-04-11 12:12:32 +0000415 // Read the branch mask, target (if applicable), regmask (if applicable).
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000416 MachineOperand CCMask(MBBI->getOperand(1));
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000417 assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
418 "Invalid condition-code mask for integer comparison");
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000419 // This is only valid for CompareAndBranch.
420 MachineOperand Target(MBBI->getOperand(
421 Type == SystemZII::CompareAndBranch ? 2 : 0));
Ulrich Weigand848a5132016-04-11 12:12:32 +0000422 const uint32_t *RegMask;
423 if (Type == SystemZII::CompareAndSibcall)
424 RegMask = MBBI->getOperand(2).getRegMask();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000425
426 // Clear out all current operands.
427 int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000428 assert(CCUse >= 0 && "BRC/BCR must use CC");
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000429 Branch->RemoveOperand(CCUse);
Ulrich Weigand848a5132016-04-11 12:12:32 +0000430 // Remove target (branch) or regmask (sibcall).
431 if (Type == SystemZII::CompareAndBranch ||
432 Type == SystemZII::CompareAndSibcall)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000433 Branch->RemoveOperand(2);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000434 Branch->RemoveOperand(1);
435 Branch->RemoveOperand(0);
436
437 // Rebuild Branch as a fused compare and branch.
438 Branch->setDesc(TII->get(FusedOpcode));
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000439 MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000440 MIB.addOperand(Compare.getOperand(0))
441 .addOperand(Compare.getOperand(1))
442 .addOperand(CCMask);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000443
444 if (Type == SystemZII::CompareAndBranch) {
445 // Only conditional branches define CC, as they may be converted back
446 // to a non-fused branch because of a long displacement. Conditional
447 // returns don't have that problem.
448 MIB.addOperand(Target)
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000449 .addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000450 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000451
Ulrich Weigand848a5132016-04-11 12:12:32 +0000452 if (Type == SystemZII::CompareAndSibcall)
453 MIB.addRegMask(RegMask);
454
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000455 // Clear any intervening kills of SrcReg and SrcReg2.
456 MBBI = Compare;
457 for (++MBBI; MBBI != MBBE; ++MBBI) {
458 MBBI->clearRegisterKills(SrcReg, TRI);
459 if (SrcReg2)
460 MBBI->clearRegisterKills(SrcReg2, TRI);
461 }
462 FusedComparisons += 1;
463 return true;
464}
465
466// Process all comparison instructions in MBB. Return true if something
467// changed.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000468bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000469 bool Changed = false;
470
471 // Walk backwards through the block looking for comparisons, recording
472 // all CC users as we go. The subroutines can delete Compare and
473 // instructions before it.
474 bool CompleteCCUsers = !isCCLiveOut(MBB);
475 SmallVector<MachineInstr *, 4> CCUsers;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000476 MachineBasicBlock::iterator MBBI = MBB.end();
477 while (MBBI != MBB.begin()) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000478 MachineInstr &MI = *--MBBI;
479 if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) &&
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000480 (optimizeCompareZero(MI, CCUsers) ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000481 fuseCompareOperations(MI, CCUsers))) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000482 ++MBBI;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000483 MI.eraseFromParent();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000484 Changed = true;
485 CCUsers.clear();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000486 continue;
487 }
488
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000489 if (MI.definesRegister(SystemZ::CC)) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000490 CCUsers.clear();
Jonas Paulsson9e1f3bd2015-10-08 07:39:55 +0000491 CompleteCCUsers = true;
Richard Sandifordc2121252013-08-05 11:23:46 +0000492 }
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000493 if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers)
494 CCUsers.push_back(&MI);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000495 }
496 return Changed;
497}
498
499bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) {
Andrew Kaylord9974cc2016-04-26 23:49:41 +0000500 if (skipFunction(*F.getFunction()))
501 return false;
502
Eric Christopherfc6de422014-08-05 02:39:49 +0000503 TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000504 TRI = &TII->getRegisterInfo();
505
506 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000507 for (auto &MBB : F)
508 Changed |= processBlock(MBB);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000509
510 return Changed;
511}