blob: 668a77ac014ff63b5a370a9e2da4bec41b749816 [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"
David Blaikieb3bde2e2017-11-17 01:07:10 +000028#include "llvm/CodeGen/TargetRegisterInfo.h"
29#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000030#include "llvm/MC/MCInstrDesc.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000031#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);
Jonas Paulsson776a81a2018-01-15 15:41:26 +000089 bool convertToLoadAndTest(MachineInstr &MI, MachineInstr &Compare,
90 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000091 bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare,
Jonas Paulsson776a81a2018-01-15 15:41:26 +000092 SmallVectorImpl<MachineInstr *> &CCUsers,
93 unsigned ConvOpc = 0);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000094 bool optimizeCompareZero(MachineInstr &Compare,
Richard Sandifordc2312692014-03-06 10:38:30 +000095 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000096 bool fuseCompareOperations(MachineInstr &Compare,
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +000097 SmallVectorImpl<MachineInstr *> &CCUsers);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000098
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000099 const SystemZInstrInfo *TII = nullptr;
100 const TargetRegisterInfo *TRI = nullptr;
Richard Sandifordc2312692014-03-06 10:38:30 +0000101};
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000102
Richard Sandifordc2312692014-03-06 10:38:30 +0000103char SystemZElimCompare::ID = 0;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000104
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000105} // end anonymous namespace
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000106
107// Return true if CC is live out of MBB.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000108static bool isCCLiveOut(MachineBasicBlock &MBB) {
109 for (auto SI = MBB.succ_begin(), SE = MBB.succ_end(); SI != SE; ++SI)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000110 if ((*SI)->isLiveIn(SystemZ::CC))
111 return true;
112 return false;
113}
114
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000115// Returns true if MI is an instruction whose output equals the value in Reg.
116static bool preservesValueOf(MachineInstr &MI, unsigned Reg) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000117 switch (MI.getOpcode()) {
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000118 case SystemZ::LR:
119 case SystemZ::LGR:
120 case SystemZ::LGFR:
121 case SystemZ::LTR:
122 case SystemZ::LTGR:
123 case SystemZ::LTGFR:
Richard Sandiford0897fce2013-08-07 11:10:06 +0000124 case SystemZ::LER:
125 case SystemZ::LDR:
126 case SystemZ::LXR:
127 case SystemZ::LTEBR:
128 case SystemZ::LTDBR:
129 case SystemZ::LTXBR:
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000130 if (MI.getOperand(1).getReg() == Reg)
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000131 return true;
132 }
133
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000134 return false;
135}
136
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000137// Return true if any CC result of MI would (perhaps after conversion)
138// reflect the value of Reg.
139static bool resultTests(MachineInstr &MI, unsigned Reg) {
140 if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() &&
141 MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg)
142 return true;
143
144 return (preservesValueOf(MI, Reg));
145}
146
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000147// Describe the references to Reg or any of its aliases in MI.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000148Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000149 Reference Ref;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000150 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
151 const MachineOperand &MO = MI.getOperand(I);
Richard Sandifordc2121252013-08-05 11:23:46 +0000152 if (MO.isReg()) {
153 if (unsigned MOReg = MO.getReg()) {
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000154 if (TRI->regsOverlap(MOReg, Reg)) {
155 if (MO.isUse())
Richard Sandifordc2121252013-08-05 11:23:46 +0000156 Ref.Use = true;
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000157 else if (MO.isDef())
Richard Sandifordc2121252013-08-05 11:23:46 +0000158 Ref.Def = true;
Richard Sandifordc2121252013-08-05 11:23:46 +0000159 }
160 }
161 }
162 }
163 return Ref;
164}
165
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000166// Return true if this is a load and test which can be optimized the
167// same way as compare instruction.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000168static bool isLoadAndTestAsCmp(MachineInstr &MI) {
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000169 // If we during isel used a load-and-test as a compare with 0, the
170 // def operand is dead.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000171 return (MI.getOpcode() == SystemZ::LTEBR ||
172 MI.getOpcode() == SystemZ::LTDBR ||
173 MI.getOpcode() == SystemZ::LTXBR) &&
174 MI.getOperand(0).isDead();
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000175}
176
177// Return the source register of Compare, which is the unknown value
178// being tested.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000179static unsigned getCompareSourceReg(MachineInstr &Compare) {
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000180 unsigned reg = 0;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000181 if (Compare.isCompare())
182 reg = Compare.getOperand(0).getReg();
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000183 else if (isLoadAndTestAsCmp(Compare))
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000184 reg = Compare.getOperand(1).getReg();
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000185 assert(reg);
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000186
187 return reg;
188}
189
Richard Sandifordc2121252013-08-05 11:23:46 +0000190// Compare compares the result of MI against zero. If MI is an addition
191// of -1 and if CCUsers is a single branch on nonzero, eliminate the addition
Ulrich Weigand75839912016-11-28 13:40:08 +0000192// and convert the branch to a BRCT(G) or BRCTH. Return true on success.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000193bool SystemZElimCompare::convertToBRCT(
194 MachineInstr &MI, MachineInstr &Compare,
195 SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000196 // Check whether we have an addition of -1.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000197 unsigned Opcode = MI.getOpcode();
Richard Sandifordc2121252013-08-05 11:23:46 +0000198 unsigned BRCT;
199 if (Opcode == SystemZ::AHI)
200 BRCT = SystemZ::BRCT;
201 else if (Opcode == SystemZ::AGHI)
202 BRCT = SystemZ::BRCTG;
Ulrich Weigand75839912016-11-28 13:40:08 +0000203 else if (Opcode == SystemZ::AIH)
204 BRCT = SystemZ::BRCTH;
Richard Sandifordc2121252013-08-05 11:23:46 +0000205 else
206 return false;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000207 if (MI.getOperand(2).getImm() != -1)
Richard Sandifordc2121252013-08-05 11:23:46 +0000208 return false;
209
210 // Check whether we have a single JLH.
211 if (CCUsers.size() != 1)
212 return false;
213 MachineInstr *Branch = CCUsers[0];
214 if (Branch->getOpcode() != SystemZ::BRC ||
215 Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
216 Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE)
217 return false;
218
219 // We already know that there are no references to the register between
220 // MI and Compare. Make sure that there are also no references between
221 // Compare and Branch.
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000222 unsigned SrcReg = getCompareSourceReg(Compare);
Richard Sandifordc2121252013-08-05 11:23:46 +0000223 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
224 for (++MBBI; MBBI != MBBE; ++MBBI)
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000225 if (getRegReferences(*MBBI, SrcReg))
Richard Sandifordc2121252013-08-05 11:23:46 +0000226 return false;
227
Ulrich Weigand75839912016-11-28 13:40:08 +0000228 // The transformation is OK. Rebuild Branch as a BRCT(G) or BRCTH.
Richard Sandifordc2121252013-08-05 11:23:46 +0000229 MachineOperand Target(Branch->getOperand(2));
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000230 while (Branch->getNumOperands())
231 Branch->RemoveOperand(0);
Richard Sandifordc2121252013-08-05 11:23:46 +0000232 Branch->setDesc(TII->get(BRCT));
Ulrich Weigand75839912016-11-28 13:40:08 +0000233 MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
Diana Picus116bbab2017-01-13 09:58:52 +0000234 MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target);
Ulrich Weigand75839912016-11-28 13:40:08 +0000235 // Add a CC def to BRCT(G), since we may have to split them again if the
236 // branch displacement overflows. BRCTH has a 32-bit displacement, so
237 // this is not necessary there.
238 if (BRCT != SystemZ::BRCTH)
239 MIB.addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000240 MI.eraseFromParent();
Richard Sandifordc2121252013-08-05 11:23:46 +0000241 return true;
242}
243
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000244// Compare compares the result of MI against zero. If MI is a suitable load
245// instruction and if CCUsers is a single conditional trap on zero, eliminate
246// the load and convert the branch to a load-and-trap. Return true on success.
247bool SystemZElimCompare::convertToLoadAndTrap(
248 MachineInstr &MI, MachineInstr &Compare,
249 SmallVectorImpl<MachineInstr *> &CCUsers) {
250 unsigned LATOpcode = TII->getLoadAndTrap(MI.getOpcode());
251 if (!LATOpcode)
252 return false;
253
254 // Check whether we have a single CondTrap that traps on zero.
255 if (CCUsers.size() != 1)
256 return false;
257 MachineInstr *Branch = CCUsers[0];
258 if (Branch->getOpcode() != SystemZ::CondTrap ||
259 Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
260 Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_EQ)
261 return false;
262
263 // We already know that there are no references to the register between
264 // MI and Compare. Make sure that there are also no references between
265 // Compare and Branch.
266 unsigned SrcReg = getCompareSourceReg(Compare);
267 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
268 for (++MBBI; MBBI != MBBE; ++MBBI)
269 if (getRegReferences(*MBBI, SrcReg))
270 return false;
271
272 // The transformation is OK. Rebuild Branch as a load-and-trap.
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000273 while (Branch->getNumOperands())
274 Branch->RemoveOperand(0);
275 Branch->setDesc(TII->get(LATOpcode));
276 MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
Diana Picus116bbab2017-01-13 09:58:52 +0000277 .add(MI.getOperand(0))
278 .add(MI.getOperand(1))
279 .add(MI.getOperand(2))
280 .add(MI.getOperand(3));
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000281 MI.eraseFromParent();
282 return true;
283}
284
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000285// If MI is a load instruction, try to convert it into a LOAD AND TEST.
286// Return true on success.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000287bool SystemZElimCompare::convertToLoadAndTest(
288 MachineInstr &MI, MachineInstr &Compare,
289 SmallVectorImpl<MachineInstr *> &CCUsers) {
290
291 // Try to adjust CC masks for the LOAD AND TEST opcode that could replace MI.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000292 unsigned Opcode = TII->getLoadAndTest(MI.getOpcode());
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000293 if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode))
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000294 return false;
295
Jonas Paulssone80d4052018-06-07 05:59:07 +0000296 // Rebuild to get the CC operand in the right place.
Chandler Carruthc73c0302018-08-16 21:30:05 +0000297 auto MIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode));
Jonas Paulssone80d4052018-06-07 05:59:07 +0000298 for (const auto &MO : MI.operands())
Chandler Carruthc73c0302018-08-16 21:30:05 +0000299 MIB.add(MO);
300 MIB.setMemRefs(MI.memoperands());
Jonas Paulssone80d4052018-06-07 05:59:07 +0000301 MI.eraseFromParent();
302
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000303 return true;
304}
305
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000306// The CC users in CCUsers are testing the result of a comparison of some
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000307// 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.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000312bool SystemZElimCompare::adjustCCMasksForInstr(
313 MachineInstr &MI, MachineInstr &Compare,
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000314 SmallVectorImpl<MachineInstr *> &CCUsers,
315 unsigned ConvOpc) {
316 int Opcode = (ConvOpc ? ConvOpc : MI.getOpcode());
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000317 const MCInstrDesc &Desc = TII->get(Opcode);
318 unsigned MIFlags = Desc.TSFlags;
319
320 // See which compare-style condition codes are available.
Richard Sandiford0897fce2013-08-07 11:10:06 +0000321 unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000322
323 // For unsigned comparisons with zero, only equality makes sense.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000324 unsigned CompareFlags = Compare.getDesc().TSFlags;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000325 if (CompareFlags & SystemZII::IsLogical)
326 ReusableCCMask &= SystemZ::CCMASK_CMP_EQ;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000327
328 if (ReusableCCMask == 0)
329 return false;
330
331 unsigned CCValues = SystemZII::getCCValues(MIFlags);
332 assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues");
333
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000334 bool MIEquivalentToCmp =
335 (ReusableCCMask == CCValues &&
336 CCValues == SystemZII::getCCValues(CompareFlags));
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000337
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000338 if (!MIEquivalentToCmp) {
339 // Now check whether these flags are enough for all users.
340 SmallVector<MachineOperand *, 4> AlterMasks;
341 for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) {
342 MachineInstr *MI = CCUsers[I];
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000343
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000344 // Fail if this isn't a use of CC that we understand.
345 unsigned Flags = MI->getDesc().TSFlags;
346 unsigned FirstOpNum;
347 if (Flags & SystemZII::CCMaskFirst)
348 FirstOpNum = 0;
349 else if (Flags & SystemZII::CCMaskLast)
350 FirstOpNum = MI->getNumExplicitOperands() - 2;
351 else
352 return false;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000353
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000354 // Check whether the instruction predicate treats all CC values
355 // outside of ReusableCCMask in the same way. In that case it
356 // doesn't matter what those CC values mean.
357 unsigned CCValid = MI->getOperand(FirstOpNum).getImm();
358 unsigned CCMask = MI->getOperand(FirstOpNum + 1).getImm();
359 unsigned OutValid = ~ReusableCCMask & CCValid;
360 unsigned OutMask = ~ReusableCCMask & CCMask;
361 if (OutMask != 0 && OutMask != OutValid)
362 return false;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000363
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000364 AlterMasks.push_back(&MI->getOperand(FirstOpNum));
365 AlterMasks.push_back(&MI->getOperand(FirstOpNum + 1));
366 }
367
368 // All users are OK. Adjust the masks for MI.
369 for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) {
370 AlterMasks[I]->setImm(CCValues);
371 unsigned CCMask = AlterMasks[I + 1]->getImm();
372 if (CCMask & ~ReusableCCMask)
373 AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) |
374 (CCValues & ~ReusableCCMask));
375 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000376 }
377
378 // CC is now live after MI.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000379 if (!ConvOpc) {
380 int CCDef = MI.findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI);
381 assert(CCDef >= 0 && "Couldn't find CC set");
382 MI.getOperand(CCDef).setIsDead(false);
383 }
384
385 // Check if MI lies before Compare.
386 bool BeforeCmp = false;
387 MachineBasicBlock::iterator MBBI = MI, MBBE = MI.getParent()->end();
388 for (++MBBI; MBBI != MBBE; ++MBBI)
389 if (MBBI == Compare) {
390 BeforeCmp = true;
391 break;
392 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000393
394 // Clear any intervening kills of CC.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000395 if (BeforeCmp) {
396 MachineBasicBlock::iterator MBBI = MI, MBBE = Compare;
397 for (++MBBI; MBBI != MBBE; ++MBBI)
398 MBBI->clearRegisterKills(SystemZ::CC, TRI);
399 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000400
401 return true;
402}
403
Richard Sandiford0897fce2013-08-07 11:10:06 +0000404// Return true if Compare is a comparison against zero.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000405static bool isCompareZero(MachineInstr &Compare) {
406 switch (Compare.getOpcode()) {
Richard Sandiford0897fce2013-08-07 11:10:06 +0000407 case SystemZ::LTEBRCompare:
408 case SystemZ::LTDBRCompare:
409 case SystemZ::LTXBRCompare:
410 return true;
411
412 default:
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000413 if (isLoadAndTestAsCmp(Compare))
414 return true;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000415 return Compare.getNumExplicitOperands() == 2 &&
416 Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000417 }
418}
419
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000420// Try to optimize cases where comparison instruction Compare is testing
421// a value against zero. Return true on success and if Compare should be
422// deleted as dead. CCUsers is the list of instructions that use the CC
423// value produced by Compare.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000424bool SystemZElimCompare::optimizeCompareZero(
425 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandiford0897fce2013-08-07 11:10:06 +0000426 if (!isCompareZero(Compare))
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000427 return false;
428
429 // Search back for CC results that are based on the first operand.
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000430 unsigned SrcReg = getCompareSourceReg(Compare);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000431 MachineBasicBlock &MBB = *Compare.getParent();
Richard Sandifordc2121252013-08-05 11:23:46 +0000432 Reference CCRefs;
433 Reference SrcRefs;
Jonas Paulssone80d4052018-06-07 05:59:07 +0000434 for (MachineBasicBlock::reverse_iterator MBBI =
435 std::next(MachineBasicBlock::reverse_iterator(&Compare)),
436 MBBE = MBB.rend(); MBBI != MBBE;) {
437 MachineInstr &MI = *MBBI++;
Jonas Paulsson2c96dd62015-10-08 07:40:11 +0000438 if (resultTests(MI, SrcReg)) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000439 // Try to remove both MI and Compare by converting a branch to BRCT(G).
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000440 // or a load-and-trap instruction. We don't care in this case whether
441 // CC is modified between MI and Compare.
442 if (!CCRefs.Use && !SrcRefs) {
443 if (convertToBRCT(MI, Compare, CCUsers)) {
444 BranchOnCounts += 1;
445 return true;
446 }
447 if (convertToLoadAndTrap(MI, Compare, CCUsers)) {
448 LoadAndTraps += 1;
449 return true;
450 }
Richard Sandifordc2121252013-08-05 11:23:46 +0000451 }
452 // Try to eliminate Compare by reusing a CC result from MI.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000453 if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) ||
Richard Sandifordc2121252013-08-05 11:23:46 +0000454 (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) {
455 EliminatedComparisons += 1;
456 return true;
457 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000458 }
Richard Sandifordc2121252013-08-05 11:23:46 +0000459 SrcRefs |= getRegReferences(MI, SrcReg);
460 if (SrcRefs.Def)
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000461 break;
Richard Sandifordc2121252013-08-05 11:23:46 +0000462 CCRefs |= getRegReferences(MI, SystemZ::CC);
463 if (CCRefs.Use && CCRefs.Def)
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000464 break;
465 }
466
467 // Also do a forward search to handle cases where an instruction after the
Jonas Paulsson22f208f2018-01-08 12:52:40 +0000468 // compare can be converted, like
469 // LTEBRCompare %f0s, %f0s; %f2s = LER %f0s => LTEBRCompare %f2s, %f0s
Jonas Paulssone80d4052018-06-07 05:59:07 +0000470 for (MachineBasicBlock::iterator MBBI =
471 std::next(MachineBasicBlock::iterator(&Compare)), MBBE = MBB.end();
472 MBBI != MBBE;) {
473 MachineInstr &MI = *MBBI++;
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000474 if (preservesValueOf(MI, SrcReg)) {
475 // Try to eliminate Compare by reusing a CC result from MI.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000476 if (convertToLoadAndTest(MI, Compare, CCUsers)) {
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000477 EliminatedComparisons += 1;
478 return true;
479 }
480 }
481 if (getRegReferences(MI, SrcReg).Def)
482 return false;
483 if (getRegReferences(MI, SystemZ::CC))
Richard Sandifordc2121252013-08-05 11:23:46 +0000484 return false;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000485 }
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000486
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000487 return false;
488}
489
490// Try to fuse comparison instruction Compare into a later branch.
491// Return true on success and if Compare is therefore redundant.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000492bool SystemZElimCompare::fuseCompareOperations(
493 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000494 // See whether we have a single branch with which to fuse.
495 if (CCUsers.size() != 1)
496 return false;
497 MachineInstr *Branch = CCUsers[0];
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000498 SystemZII::FusedCompareType Type;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000499 switch (Branch->getOpcode()) {
500 case SystemZ::BRC:
501 Type = SystemZII::CompareAndBranch;
502 break;
503 case SystemZ::CondReturn:
504 Type = SystemZII::CompareAndReturn;
505 break;
Ulrich Weigand848a5132016-04-11 12:12:32 +0000506 case SystemZ::CallBCR:
507 Type = SystemZII::CompareAndSibcall;
508 break;
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000509 case SystemZ::CondTrap:
510 Type = SystemZII::CompareAndTrap;
511 break;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000512 default:
513 return false;
514 }
515
516 // See whether we have a comparison that can be fused.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000517 unsigned FusedOpcode =
518 TII->getFusedCompare(Compare.getOpcode(), Type, &Compare);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000519 if (!FusedOpcode)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000520 return false;
521
522 // Make sure that the operands are available at the branch.
Ulrich Weiganda0e73252016-11-11 12:48:26 +0000523 // SrcReg2 is the register if the source operand is a register,
524 // 0 if the source operand is immediate, and the base register
525 // if the source operand is memory (index is not supported).
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000526 unsigned SrcReg = Compare.getOperand(0).getReg();
527 unsigned SrcReg2 =
528 Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : 0;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000529 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
530 for (++MBBI; MBBI != MBBE; ++MBBI)
531 if (MBBI->modifiesRegister(SrcReg, TRI) ||
532 (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
533 return false;
534
Ulrich Weigand848a5132016-04-11 12:12:32 +0000535 // Read the branch mask, target (if applicable), regmask (if applicable).
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000536 MachineOperand CCMask(MBBI->getOperand(1));
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000537 assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
538 "Invalid condition-code mask for integer comparison");
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000539 // This is only valid for CompareAndBranch.
540 MachineOperand Target(MBBI->getOperand(
541 Type == SystemZII::CompareAndBranch ? 2 : 0));
Ulrich Weigand848a5132016-04-11 12:12:32 +0000542 const uint32_t *RegMask;
543 if (Type == SystemZII::CompareAndSibcall)
544 RegMask = MBBI->getOperand(2).getRegMask();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000545
546 // Clear out all current operands.
547 int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000548 assert(CCUse >= 0 && "BRC/BCR must use CC");
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000549 Branch->RemoveOperand(CCUse);
Ulrich Weigand848a5132016-04-11 12:12:32 +0000550 // Remove target (branch) or regmask (sibcall).
551 if (Type == SystemZII::CompareAndBranch ||
552 Type == SystemZII::CompareAndSibcall)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000553 Branch->RemoveOperand(2);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000554 Branch->RemoveOperand(1);
555 Branch->RemoveOperand(0);
556
557 // Rebuild Branch as a fused compare and branch.
Ulrich Weiganda0e73252016-11-11 12:48:26 +0000558 // SrcNOps is the number of MI operands of the compare instruction
559 // that we need to copy over.
560 unsigned SrcNOps = 2;
561 if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT)
562 SrcNOps = 3;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000563 Branch->setDesc(TII->get(FusedOpcode));
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000564 MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
Ulrich Weiganda0e73252016-11-11 12:48:26 +0000565 for (unsigned I = 0; I < SrcNOps; I++)
Diana Picus116bbab2017-01-13 09:58:52 +0000566 MIB.add(Compare.getOperand(I));
567 MIB.add(CCMask);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000568
569 if (Type == SystemZII::CompareAndBranch) {
570 // Only conditional branches define CC, as they may be converted back
571 // to a non-fused branch because of a long displacement. Conditional
572 // returns don't have that problem.
Diana Picus116bbab2017-01-13 09:58:52 +0000573 MIB.add(Target).addReg(SystemZ::CC,
574 RegState::ImplicitDefine | RegState::Dead);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000575 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000576
Ulrich Weigand848a5132016-04-11 12:12:32 +0000577 if (Type == SystemZII::CompareAndSibcall)
578 MIB.addRegMask(RegMask);
579
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000580 // Clear any intervening kills of SrcReg and SrcReg2.
581 MBBI = Compare;
582 for (++MBBI; MBBI != MBBE; ++MBBI) {
583 MBBI->clearRegisterKills(SrcReg, TRI);
584 if (SrcReg2)
585 MBBI->clearRegisterKills(SrcReg2, TRI);
586 }
587 FusedComparisons += 1;
588 return true;
589}
590
591// Process all comparison instructions in MBB. Return true if something
592// changed.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000593bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000594 bool Changed = false;
595
596 // Walk backwards through the block looking for comparisons, recording
597 // all CC users as we go. The subroutines can delete Compare and
598 // instructions before it.
599 bool CompleteCCUsers = !isCCLiveOut(MBB);
600 SmallVector<MachineInstr *, 4> CCUsers;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000601 MachineBasicBlock::iterator MBBI = MBB.end();
602 while (MBBI != MBB.begin()) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000603 MachineInstr &MI = *--MBBI;
604 if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) &&
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000605 (optimizeCompareZero(MI, CCUsers) ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000606 fuseCompareOperations(MI, CCUsers))) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000607 ++MBBI;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000608 MI.eraseFromParent();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000609 Changed = true;
610 CCUsers.clear();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000611 continue;
612 }
613
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000614 if (MI.definesRegister(SystemZ::CC)) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000615 CCUsers.clear();
Jonas Paulsson9e1f3bd2015-10-08 07:39:55 +0000616 CompleteCCUsers = true;
Richard Sandifordc2121252013-08-05 11:23:46 +0000617 }
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000618 if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers)
619 CCUsers.push_back(&MI);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000620 }
621 return Changed;
622}
623
624bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000625 if (skipFunction(F.getFunction()))
Andrew Kaylord9974cc2016-04-26 23:49:41 +0000626 return false;
627
Eric Christopherfc6de422014-08-05 02:39:49 +0000628 TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000629 TRI = &TII->getRegisterInfo();
630
631 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000632 for (auto &MBB : F)
633 Changed |= processBlock(MBB);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000634
635 return Changed;
636}
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000637
638FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) {
639 return new SystemZElimCompare(TM);
640}