blob: 5f75b13b3efb4e9fe2261659c125c044c94ce498 [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
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000016#include "SystemZ.h"
17#include "SystemZInstrInfo.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000018#include "SystemZTargetMachine.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000019#include "llvm/ADT/SmallVector.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000020#include "llvm/ADT/Statistic.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000021#include "llvm/ADT/StringRef.h"
22#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineFunction.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000025#include "llvm/CodeGen/MachineInstr.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000027#include "llvm/CodeGen/MachineOperand.h"
28#include "llvm/MC/MCInstrDesc.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000029#include "llvm/Target/TargetRegisterInfo.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000030#include "llvm/Target/TargetSubtargetInfo.h"
31#include <cassert>
32#include <cstdint>
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000033
34using namespace llvm;
35
Chandler Carruth84e68b22014-04-22 02:41:26 +000036#define DEBUG_TYPE "systemz-elim-compare"
37
Richard Sandifordc2121252013-08-05 11:23:46 +000038STATISTIC(BranchOnCounts, "Number of branch-on-count instructions");
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +000039STATISTIC(LoadAndTraps, "Number of load-and-trap instructions");
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000040STATISTIC(EliminatedComparisons, "Number of eliminated comparisons");
41STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions");
42
43namespace {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000044
Richard Sandifordc2312692014-03-06 10:38:30 +000045// Represents the references to a particular register in one or more
46// instructions.
47struct Reference {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000048 Reference() = default;
Richard Sandifordc2121252013-08-05 11:23:46 +000049
Richard Sandifordc2312692014-03-06 10:38:30 +000050 Reference &operator|=(const Reference &Other) {
51 Def |= Other.Def;
Richard Sandifordc2312692014-03-06 10:38:30 +000052 Use |= Other.Use;
Richard Sandifordc2312692014-03-06 10:38:30 +000053 return *this;
54 }
Richard Sandifordc2121252013-08-05 11:23:46 +000055
Aaron Ballmanb46962f2015-02-15 22:00:20 +000056 explicit operator bool() const { return Def || Use; }
Richard Sandifordc2121252013-08-05 11:23:46 +000057
Richard Sandifordc2312692014-03-06 10:38:30 +000058 // True if the register is defined or used in some form, either directly or
59 // via a sub- or super-register.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000060 bool Def = false;
61 bool Use = false;
Richard Sandifordc2312692014-03-06 10:38:30 +000062};
Richard Sandifordc2121252013-08-05 11:23:46 +000063
Richard Sandifordc2312692014-03-06 10:38:30 +000064class SystemZElimCompare : public MachineFunctionPass {
65public:
66 static char ID;
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000067
Richard Sandifordc2312692014-03-06 10:38:30 +000068 SystemZElimCompare(const SystemZTargetMachine &tm)
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000069 : MachineFunctionPass(ID) {}
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000070
Mehdi Amini117296c2016-10-01 02:56:57 +000071 StringRef getPassName() const override {
Richard Sandifordc2312692014-03-06 10:38:30 +000072 return "SystemZ Comparison Elimination";
73 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000074
Richard Sandiford28c111e2014-03-06 11:00:15 +000075 bool processBlock(MachineBasicBlock &MBB);
Craig Topper9d74a5a2014-04-29 07:58:41 +000076 bool runOnMachineFunction(MachineFunction &F) override;
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000077
Derek Schuff1dbf7a52016-04-04 17:09:25 +000078 MachineFunctionProperties getRequiredProperties() const override {
79 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000080 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000081 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000082
Richard Sandifordc2312692014-03-06 10:38:30 +000083private:
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000084 Reference getRegReferences(MachineInstr &MI, unsigned Reg);
85 bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare,
Richard Sandifordc2312692014-03-06 10:38:30 +000086 SmallVectorImpl<MachineInstr *> &CCUsers);
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +000087 bool convertToLoadAndTrap(MachineInstr &MI, MachineInstr &Compare,
88 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000089 bool convertToLoadAndTest(MachineInstr &MI);
90 bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare,
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000091 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000092 bool optimizeCompareZero(MachineInstr &Compare,
Richard Sandifordc2312692014-03-06 10:38:30 +000093 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000094 bool fuseCompareOperations(MachineInstr &Compare,
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +000095 SmallVectorImpl<MachineInstr *> &CCUsers);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000096
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000097 const SystemZInstrInfo *TII = nullptr;
98 const TargetRegisterInfo *TRI = nullptr;
Richard Sandifordc2312692014-03-06 10:38:30 +000099};
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000100
Richard Sandifordc2312692014-03-06 10:38:30 +0000101char SystemZElimCompare::ID = 0;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000102
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000103} // end anonymous namespace
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000104
105// Return true if CC is live out of MBB.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000106static bool isCCLiveOut(MachineBasicBlock &MBB) {
107 for (auto SI = MBB.succ_begin(), SE = MBB.succ_end(); SI != SE; ++SI)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000108 if ((*SI)->isLiveIn(SystemZ::CC))
109 return true;
110 return false;
111}
112
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000113// Returns true if MI is an instruction whose output equals the value in Reg.
114static bool preservesValueOf(MachineInstr &MI, unsigned Reg) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000115 switch (MI.getOpcode()) {
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000116 case SystemZ::LR:
117 case SystemZ::LGR:
118 case SystemZ::LGFR:
119 case SystemZ::LTR:
120 case SystemZ::LTGR:
121 case SystemZ::LTGFR:
Richard Sandiford0897fce2013-08-07 11:10:06 +0000122 case SystemZ::LER:
123 case SystemZ::LDR:
124 case SystemZ::LXR:
125 case SystemZ::LTEBR:
126 case SystemZ::LTDBR:
127 case SystemZ::LTXBR:
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000128 if (MI.getOperand(1).getReg() == Reg)
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000129 return true;
130 }
131
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000132 return false;
133}
134
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000135// Return true if any CC result of MI would (perhaps after conversion)
136// reflect the value of Reg.
137static 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
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000145// Describe the references to Reg or any of its aliases in MI.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000146Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000147 Reference Ref;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000148 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
149 const MachineOperand &MO = MI.getOperand(I);
Richard Sandifordc2121252013-08-05 11:23:46 +0000150 if (MO.isReg()) {
151 if (unsigned MOReg = MO.getReg()) {
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000152 if (TRI->regsOverlap(MOReg, Reg)) {
153 if (MO.isUse())
Richard Sandifordc2121252013-08-05 11:23:46 +0000154 Ref.Use = true;
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000155 else if (MO.isDef())
Richard Sandifordc2121252013-08-05 11:23:46 +0000156 Ref.Def = true;
Richard Sandifordc2121252013-08-05 11:23:46 +0000157 }
158 }
159 }
160 }
161 return Ref;
162}
163
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000164// Return true if this is a load and test which can be optimized the
165// same way as compare instruction.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000166static bool isLoadAndTestAsCmp(MachineInstr &MI) {
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000167 // If we during isel used a load-and-test as a compare with 0, the
168 // def operand is dead.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000169 return (MI.getOpcode() == SystemZ::LTEBR ||
170 MI.getOpcode() == SystemZ::LTDBR ||
171 MI.getOpcode() == SystemZ::LTXBR) &&
172 MI.getOperand(0).isDead();
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000173}
174
175// Return the source register of Compare, which is the unknown value
176// being tested.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000177static unsigned getCompareSourceReg(MachineInstr &Compare) {
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000178 unsigned reg = 0;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000179 if (Compare.isCompare())
180 reg = Compare.getOperand(0).getReg();
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000181 else if (isLoadAndTestAsCmp(Compare))
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000182 reg = Compare.getOperand(1).getReg();
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000183 assert(reg);
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000184
185 return reg;
186}
187
Richard Sandifordc2121252013-08-05 11:23:46 +0000188// 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
Ulrich Weigand75839912016-11-28 13:40:08 +0000190// and convert the branch to a BRCT(G) or BRCTH. Return true on success.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000191bool SystemZElimCompare::convertToBRCT(
192 MachineInstr &MI, MachineInstr &Compare,
193 SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000194 // Check whether we have an addition of -1.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000195 unsigned Opcode = MI.getOpcode();
Richard Sandifordc2121252013-08-05 11:23:46 +0000196 unsigned BRCT;
197 if (Opcode == SystemZ::AHI)
198 BRCT = SystemZ::BRCT;
199 else if (Opcode == SystemZ::AGHI)
200 BRCT = SystemZ::BRCTG;
Ulrich Weigand75839912016-11-28 13:40:08 +0000201 else if (Opcode == SystemZ::AIH)
202 BRCT = SystemZ::BRCTH;
Richard Sandifordc2121252013-08-05 11:23:46 +0000203 else
204 return false;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000205 if (MI.getOperand(2).getImm() != -1)
Richard Sandifordc2121252013-08-05 11:23:46 +0000206 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.
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000220 unsigned SrcReg = getCompareSourceReg(Compare);
Richard Sandifordc2121252013-08-05 11:23:46 +0000221 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
222 for (++MBBI; MBBI != MBBE; ++MBBI)
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000223 if (getRegReferences(*MBBI, SrcReg))
Richard Sandifordc2121252013-08-05 11:23:46 +0000224 return false;
225
Ulrich Weigand75839912016-11-28 13:40:08 +0000226 // The transformation is OK. Rebuild Branch as a BRCT(G) or BRCTH.
Richard Sandifordc2121252013-08-05 11:23:46 +0000227 MachineOperand Target(Branch->getOperand(2));
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000228 while (Branch->getNumOperands())
229 Branch->RemoveOperand(0);
Richard Sandifordc2121252013-08-05 11:23:46 +0000230 Branch->setDesc(TII->get(BRCT));
Ulrich Weigand75839912016-11-28 13:40:08 +0000231 MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
Diana Picus116bbab2017-01-13 09:58:52 +0000232 MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target);
Ulrich Weigand75839912016-11-28 13:40:08 +0000233 // 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);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000238 MI.eraseFromParent();
Richard Sandifordc2121252013-08-05 11:23:46 +0000239 return true;
240}
241
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000242// 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.
245bool 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.
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000271 while (Branch->getNumOperands())
272 Branch->RemoveOperand(0);
273 Branch->setDesc(TII->get(LATOpcode));
274 MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
Diana Picus116bbab2017-01-13 09:58:52 +0000275 .add(MI.getOperand(0))
276 .add(MI.getOperand(1))
277 .add(MI.getOperand(2))
278 .add(MI.getOperand(3));
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000279 MI.eraseFromParent();
280 return true;
281}
282
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000283// If MI is a load instruction, try to convert it into a LOAD AND TEST.
284// Return true on success.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000285bool SystemZElimCompare::convertToLoadAndTest(MachineInstr &MI) {
286 unsigned Opcode = TII->getLoadAndTest(MI.getOpcode());
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000287 if (!Opcode)
288 return false;
289
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000290 MI.setDesc(TII->get(Opcode));
291 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
292 .addReg(SystemZ::CC, RegState::ImplicitDefine);
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000293 return true;
294}
295
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000296// 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.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000301bool SystemZElimCompare::adjustCCMasksForInstr(
302 MachineInstr &MI, MachineInstr &Compare,
303 SmallVectorImpl<MachineInstr *> &CCUsers) {
304 int Opcode = MI.getOpcode();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000305 const MCInstrDesc &Desc = TII->get(Opcode);
306 unsigned MIFlags = Desc.TSFlags;
307
308 // See which compare-style condition codes are available.
Richard Sandiford0897fce2013-08-07 11:10:06 +0000309 unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000310
311 // For unsigned comparisons with zero, only equality makes sense.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000312 unsigned CompareFlags = Compare.getDesc().TSFlags;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000313 if (CompareFlags & SystemZII::IsLogical)
314 ReusableCCMask &= SystemZ::CCMASK_CMP_EQ;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000315
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.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000361 int CCDef = MI.findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000362 assert(CCDef >= 0 && "Couldn't find CC set");
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000363 MI.getOperand(CCDef).setIsDead(false);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000364
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
Richard Sandiford0897fce2013-08-07 11:10:06 +0000373// Return true if Compare is a comparison against zero.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000374static bool isCompareZero(MachineInstr &Compare) {
375 switch (Compare.getOpcode()) {
Richard Sandiford0897fce2013-08-07 11:10:06 +0000376 case SystemZ::LTEBRCompare:
377 case SystemZ::LTDBRCompare:
378 case SystemZ::LTXBRCompare:
379 return true;
380
381 default:
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000382 if (isLoadAndTestAsCmp(Compare))
383 return true;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000384 return Compare.getNumExplicitOperands() == 2 &&
385 Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000386 }
387}
388
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000389// 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.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000393bool SystemZElimCompare::optimizeCompareZero(
394 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandiford0897fce2013-08-07 11:10:06 +0000395 if (!isCompareZero(Compare))
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000396 return false;
397
398 // Search back for CC results that are based on the first operand.
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000399 unsigned SrcReg = getCompareSourceReg(Compare);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000400 MachineBasicBlock &MBB = *Compare.getParent();
Richard Sandiford28c111e2014-03-06 11:00:15 +0000401 MachineBasicBlock::iterator MBBI = Compare, MBBE = MBB.begin();
Richard Sandifordc2121252013-08-05 11:23:46 +0000402 Reference CCRefs;
403 Reference SrcRefs;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000404 while (MBBI != MBBE) {
405 --MBBI;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000406 MachineInstr &MI = *MBBI;
Jonas Paulsson2c96dd62015-10-08 07:40:11 +0000407 if (resultTests(MI, SrcReg)) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000408 // Try to remove both MI and Compare by converting a branch to BRCT(G).
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000409 // 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 }
Richard Sandifordc2121252013-08-05 11:23:46 +0000420 }
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 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000427 }
Richard Sandifordc2121252013-08-05 11:23:46 +0000428 SrcRefs |= getRegReferences(MI, SrcReg);
429 if (SrcRefs.Def)
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000430 break;
Richard Sandifordc2121252013-08-05 11:23:46 +0000431 CCRefs |= getRegReferences(MI, SystemZ::CC);
432 if (CCRefs.Use && CCRefs.Def)
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000433 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))
Richard Sandifordc2121252013-08-05 11:23:46 +0000455 return false;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000456 }
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000457
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000458 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.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000463bool SystemZElimCompare::fuseCompareOperations(
464 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000465 // See whether we have a single branch with which to fuse.
466 if (CCUsers.size() != 1)
467 return false;
468 MachineInstr *Branch = CCUsers[0];
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000469 SystemZII::FusedCompareType Type;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000470 switch (Branch->getOpcode()) {
471 case SystemZ::BRC:
472 Type = SystemZII::CompareAndBranch;
473 break;
474 case SystemZ::CondReturn:
475 Type = SystemZII::CompareAndReturn;
476 break;
Ulrich Weigand848a5132016-04-11 12:12:32 +0000477 case SystemZ::CallBCR:
478 Type = SystemZII::CompareAndSibcall;
479 break;
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000480 case SystemZ::CondTrap:
481 Type = SystemZII::CompareAndTrap;
482 break;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000483 default:
484 return false;
485 }
486
487 // See whether we have a comparison that can be fused.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000488 unsigned FusedOpcode =
489 TII->getFusedCompare(Compare.getOpcode(), Type, &Compare);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000490 if (!FusedOpcode)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000491 return false;
492
493 // Make sure that the operands are available at the branch.
Ulrich Weiganda0e73252016-11-11 12:48:26 +0000494 // 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).
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000497 unsigned SrcReg = Compare.getOperand(0).getReg();
498 unsigned SrcReg2 =
499 Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : 0;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000500 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
Ulrich Weigand848a5132016-04-11 12:12:32 +0000506 // Read the branch mask, target (if applicable), regmask (if applicable).
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000507 MachineOperand CCMask(MBBI->getOperand(1));
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000508 assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
509 "Invalid condition-code mask for integer comparison");
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000510 // This is only valid for CompareAndBranch.
511 MachineOperand Target(MBBI->getOperand(
512 Type == SystemZII::CompareAndBranch ? 2 : 0));
Ulrich Weigand848a5132016-04-11 12:12:32 +0000513 const uint32_t *RegMask;
514 if (Type == SystemZII::CompareAndSibcall)
515 RegMask = MBBI->getOperand(2).getRegMask();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000516
517 // Clear out all current operands.
518 int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000519 assert(CCUse >= 0 && "BRC/BCR must use CC");
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000520 Branch->RemoveOperand(CCUse);
Ulrich Weigand848a5132016-04-11 12:12:32 +0000521 // Remove target (branch) or regmask (sibcall).
522 if (Type == SystemZII::CompareAndBranch ||
523 Type == SystemZII::CompareAndSibcall)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000524 Branch->RemoveOperand(2);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000525 Branch->RemoveOperand(1);
526 Branch->RemoveOperand(0);
527
528 // Rebuild Branch as a fused compare and branch.
Ulrich Weiganda0e73252016-11-11 12:48:26 +0000529 // 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;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000534 Branch->setDesc(TII->get(FusedOpcode));
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000535 MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
Ulrich Weiganda0e73252016-11-11 12:48:26 +0000536 for (unsigned I = 0; I < SrcNOps; I++)
Diana Picus116bbab2017-01-13 09:58:52 +0000537 MIB.add(Compare.getOperand(I));
538 MIB.add(CCMask);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000539
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.
Diana Picus116bbab2017-01-13 09:58:52 +0000544 MIB.add(Target).addReg(SystemZ::CC,
545 RegState::ImplicitDefine | RegState::Dead);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000546 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000547
Ulrich Weigand848a5132016-04-11 12:12:32 +0000548 if (Type == SystemZII::CompareAndSibcall)
549 MIB.addRegMask(RegMask);
550
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000551 // 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.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000564bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000565 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;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000572 MachineBasicBlock::iterator MBBI = MBB.end();
573 while (MBBI != MBB.begin()) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000574 MachineInstr &MI = *--MBBI;
575 if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) &&
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000576 (optimizeCompareZero(MI, CCUsers) ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000577 fuseCompareOperations(MI, CCUsers))) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000578 ++MBBI;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000579 MI.eraseFromParent();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000580 Changed = true;
581 CCUsers.clear();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000582 continue;
583 }
584
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000585 if (MI.definesRegister(SystemZ::CC)) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000586 CCUsers.clear();
Jonas Paulsson9e1f3bd2015-10-08 07:39:55 +0000587 CompleteCCUsers = true;
Richard Sandifordc2121252013-08-05 11:23:46 +0000588 }
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000589 if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers)
590 CCUsers.push_back(&MI);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000591 }
592 return Changed;
593}
594
595bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) {
Andrew Kaylord9974cc2016-04-26 23:49:41 +0000596 if (skipFunction(*F.getFunction()))
597 return false;
598
Eric Christopherfc6de422014-08-05 02:39:49 +0000599 TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000600 TRI = &TII->getRegisterInfo();
601
602 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000603 for (auto &MBB : F)
604 Changed |= processBlock(MBB);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000605
606 return Changed;
607}
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000608
609FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) {
610 return new SystemZElimCompare(TM);
611}