blob: 9cbf6b320504debb23cfe03ba334e15907ed6b2b [file] [log] [blame]
Richard Sandifordbdbb8af2013-08-05 10:58:53 +00001//===-- SystemZElimCompare.cpp - Eliminate comparison instructions --------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Richard Sandifordbdbb8af2013-08-05 10:58:53 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This pass:
10// (1) tries to remove compares if CC already contains the required information
11// (2) fuses compares and branches into COMPARE AND BRANCH instructions
12//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000015#include "SystemZ.h"
16#include "SystemZInstrInfo.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000017#include "SystemZTargetMachine.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000018#include "llvm/ADT/SmallVector.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000019#include "llvm/ADT/Statistic.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000020#include "llvm/ADT/StringRef.h"
21#include "llvm/CodeGen/MachineBasicBlock.h"
22#include "llvm/CodeGen/MachineFunction.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000024#include "llvm/CodeGen/MachineInstr.h"
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000026#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000027#include "llvm/CodeGen/TargetRegisterInfo.h"
28#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000029#include "llvm/MC/MCInstrDesc.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000030#include <cassert>
31#include <cstdint>
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000032
33using namespace llvm;
34
Chandler Carruth84e68b22014-04-22 02:41:26 +000035#define DEBUG_TYPE "systemz-elim-compare"
36
Richard Sandifordc2121252013-08-05 11:23:46 +000037STATISTIC(BranchOnCounts, "Number of branch-on-count instructions");
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +000038STATISTIC(LoadAndTraps, "Number of load-and-trap instructions");
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000039STATISTIC(EliminatedComparisons, "Number of eliminated comparisons");
40STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions");
41
42namespace {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000043
Richard Sandifordc2312692014-03-06 10:38:30 +000044// Represents the references to a particular register in one or more
45// instructions.
46struct Reference {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000047 Reference() = default;
Richard Sandifordc2121252013-08-05 11:23:46 +000048
Richard Sandifordc2312692014-03-06 10:38:30 +000049 Reference &operator|=(const Reference &Other) {
50 Def |= Other.Def;
Richard Sandifordc2312692014-03-06 10:38:30 +000051 Use |= Other.Use;
Richard Sandifordc2312692014-03-06 10:38:30 +000052 return *this;
53 }
Richard Sandifordc2121252013-08-05 11:23:46 +000054
Aaron Ballmanb46962f2015-02-15 22:00:20 +000055 explicit operator bool() const { return Def || Use; }
Richard Sandifordc2121252013-08-05 11:23:46 +000056
Richard Sandifordc2312692014-03-06 10:38:30 +000057 // True if the register is defined or used in some form, either directly or
58 // via a sub- or super-register.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000059 bool Def = false;
60 bool Use = false;
Richard Sandifordc2312692014-03-06 10:38:30 +000061};
Richard Sandifordc2121252013-08-05 11:23:46 +000062
Richard Sandifordc2312692014-03-06 10:38:30 +000063class SystemZElimCompare : public MachineFunctionPass {
64public:
65 static char ID;
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000066
Richard Sandifordc2312692014-03-06 10:38:30 +000067 SystemZElimCompare(const SystemZTargetMachine &tm)
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000068 : MachineFunctionPass(ID) {}
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000069
Mehdi Amini117296c2016-10-01 02:56:57 +000070 StringRef getPassName() const override {
Richard Sandifordc2312692014-03-06 10:38:30 +000071 return "SystemZ Comparison Elimination";
72 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000073
Richard Sandiford28c111e2014-03-06 11:00:15 +000074 bool processBlock(MachineBasicBlock &MBB);
Craig Topper9d74a5a2014-04-29 07:58:41 +000075 bool runOnMachineFunction(MachineFunction &F) override;
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000076
Derek Schuff1dbf7a52016-04-04 17:09:25 +000077 MachineFunctionProperties getRequiredProperties() const override {
78 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000079 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000080 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000081
Richard Sandifordc2312692014-03-06 10:38:30 +000082private:
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000083 Reference getRegReferences(MachineInstr &MI, unsigned Reg);
84 bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare,
Richard Sandifordc2312692014-03-06 10:38:30 +000085 SmallVectorImpl<MachineInstr *> &CCUsers);
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +000086 bool convertToLoadAndTrap(MachineInstr &MI, MachineInstr &Compare,
87 SmallVectorImpl<MachineInstr *> &CCUsers);
Jonas Paulsson776a81a2018-01-15 15:41:26 +000088 bool convertToLoadAndTest(MachineInstr &MI, MachineInstr &Compare,
89 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000090 bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare,
Jonas Paulsson776a81a2018-01-15 15:41:26 +000091 SmallVectorImpl<MachineInstr *> &CCUsers,
92 unsigned ConvOpc = 0);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000093 bool optimizeCompareZero(MachineInstr &Compare,
Richard Sandifordc2312692014-03-06 10:38:30 +000094 SmallVectorImpl<MachineInstr *> &CCUsers);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000095 bool fuseCompareOperations(MachineInstr &Compare,
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +000096 SmallVectorImpl<MachineInstr *> &CCUsers);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +000097
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000098 const SystemZInstrInfo *TII = nullptr;
99 const TargetRegisterInfo *TRI = nullptr;
Richard Sandifordc2312692014-03-06 10:38:30 +0000100};
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000101
Richard Sandifordc2312692014-03-06 10:38:30 +0000102char SystemZElimCompare::ID = 0;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000103
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000104} // end anonymous namespace
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000105
106// Return true if CC is live out of MBB.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000107static bool isCCLiveOut(MachineBasicBlock &MBB) {
108 for (auto SI = MBB.succ_begin(), SE = MBB.succ_end(); SI != SE; ++SI)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000109 if ((*SI)->isLiveIn(SystemZ::CC))
110 return true;
111 return false;
112}
113
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000114// Returns true if MI is an instruction whose output equals the value in Reg.
115static bool preservesValueOf(MachineInstr &MI, unsigned Reg) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000116 switch (MI.getOpcode()) {
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000117 case SystemZ::LR:
118 case SystemZ::LGR:
119 case SystemZ::LGFR:
120 case SystemZ::LTR:
121 case SystemZ::LTGR:
122 case SystemZ::LTGFR:
Richard Sandiford0897fce2013-08-07 11:10:06 +0000123 case SystemZ::LER:
124 case SystemZ::LDR:
125 case SystemZ::LXR:
126 case SystemZ::LTEBR:
127 case SystemZ::LTDBR:
128 case SystemZ::LTXBR:
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000129 if (MI.getOperand(1).getReg() == Reg)
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000130 return true;
131 }
132
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000133 return false;
134}
135
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000136// Return true if any CC result of MI would (perhaps after conversion)
137// reflect the value of Reg.
138static bool resultTests(MachineInstr &MI, unsigned Reg) {
139 if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() &&
140 MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg)
141 return true;
142
143 return (preservesValueOf(MI, Reg));
144}
145
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000146// Describe the references to Reg or any of its aliases in MI.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000147Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000148 Reference Ref;
Jonas Paulsson961c47e2019-01-23 07:42:26 +0000149 if (MI.isDebugInstr())
150 return Ref;
151
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000152 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
153 const MachineOperand &MO = MI.getOperand(I);
Richard Sandifordc2121252013-08-05 11:23:46 +0000154 if (MO.isReg()) {
155 if (unsigned MOReg = MO.getReg()) {
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000156 if (TRI->regsOverlap(MOReg, Reg)) {
157 if (MO.isUse())
Richard Sandifordc2121252013-08-05 11:23:46 +0000158 Ref.Use = true;
Jonas Paulssonee3685f2015-10-09 11:27:44 +0000159 else if (MO.isDef())
Richard Sandifordc2121252013-08-05 11:23:46 +0000160 Ref.Def = true;
Richard Sandifordc2121252013-08-05 11:23:46 +0000161 }
162 }
163 }
164 }
165 return Ref;
166}
167
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000168// Return true if this is a load and test which can be optimized the
169// same way as compare instruction.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000170static bool isLoadAndTestAsCmp(MachineInstr &MI) {
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000171 // If we during isel used a load-and-test as a compare with 0, the
172 // def operand is dead.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000173 return (MI.getOpcode() == SystemZ::LTEBR ||
174 MI.getOpcode() == SystemZ::LTDBR ||
175 MI.getOpcode() == SystemZ::LTXBR) &&
176 MI.getOperand(0).isDead();
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000177}
178
179// Return the source register of Compare, which is the unknown value
180// being tested.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000181static unsigned getCompareSourceReg(MachineInstr &Compare) {
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000182 unsigned reg = 0;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000183 if (Compare.isCompare())
184 reg = Compare.getOperand(0).getReg();
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000185 else if (isLoadAndTestAsCmp(Compare))
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000186 reg = Compare.getOperand(1).getReg();
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000187 assert(reg);
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000188
189 return reg;
190}
191
Richard Sandifordc2121252013-08-05 11:23:46 +0000192// Compare compares the result of MI against zero. If MI is an addition
193// of -1 and if CCUsers is a single branch on nonzero, eliminate the addition
Ulrich Weigand75839912016-11-28 13:40:08 +0000194// and convert the branch to a BRCT(G) or BRCTH. Return true on success.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000195bool SystemZElimCompare::convertToBRCT(
196 MachineInstr &MI, MachineInstr &Compare,
197 SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000198 // Check whether we have an addition of -1.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000199 unsigned Opcode = MI.getOpcode();
Richard Sandifordc2121252013-08-05 11:23:46 +0000200 unsigned BRCT;
201 if (Opcode == SystemZ::AHI)
202 BRCT = SystemZ::BRCT;
203 else if (Opcode == SystemZ::AGHI)
204 BRCT = SystemZ::BRCTG;
Ulrich Weigand75839912016-11-28 13:40:08 +0000205 else if (Opcode == SystemZ::AIH)
206 BRCT = SystemZ::BRCTH;
Richard Sandifordc2121252013-08-05 11:23:46 +0000207 else
208 return false;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000209 if (MI.getOperand(2).getImm() != -1)
Richard Sandifordc2121252013-08-05 11:23:46 +0000210 return false;
211
212 // Check whether we have a single JLH.
213 if (CCUsers.size() != 1)
214 return false;
215 MachineInstr *Branch = CCUsers[0];
216 if (Branch->getOpcode() != SystemZ::BRC ||
217 Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
218 Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE)
219 return false;
220
221 // We already know that there are no references to the register between
222 // MI and Compare. Make sure that there are also no references between
223 // Compare and Branch.
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000224 unsigned SrcReg = getCompareSourceReg(Compare);
Richard Sandifordc2121252013-08-05 11:23:46 +0000225 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
226 for (++MBBI; MBBI != MBBE; ++MBBI)
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000227 if (getRegReferences(*MBBI, SrcReg))
Richard Sandifordc2121252013-08-05 11:23:46 +0000228 return false;
229
Ulrich Weigand75839912016-11-28 13:40:08 +0000230 // The transformation is OK. Rebuild Branch as a BRCT(G) or BRCTH.
Richard Sandifordc2121252013-08-05 11:23:46 +0000231 MachineOperand Target(Branch->getOperand(2));
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000232 while (Branch->getNumOperands())
233 Branch->RemoveOperand(0);
Richard Sandifordc2121252013-08-05 11:23:46 +0000234 Branch->setDesc(TII->get(BRCT));
Ulrich Weigand75839912016-11-28 13:40:08 +0000235 MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
Diana Picus116bbab2017-01-13 09:58:52 +0000236 MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target);
Ulrich Weigand75839912016-11-28 13:40:08 +0000237 // Add a CC def to BRCT(G), since we may have to split them again if the
238 // branch displacement overflows. BRCTH has a 32-bit displacement, so
239 // this is not necessary there.
240 if (BRCT != SystemZ::BRCTH)
241 MIB.addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000242 MI.eraseFromParent();
Richard Sandifordc2121252013-08-05 11:23:46 +0000243 return true;
244}
245
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000246// Compare compares the result of MI against zero. If MI is a suitable load
247// instruction and if CCUsers is a single conditional trap on zero, eliminate
248// the load and convert the branch to a load-and-trap. Return true on success.
249bool SystemZElimCompare::convertToLoadAndTrap(
250 MachineInstr &MI, MachineInstr &Compare,
251 SmallVectorImpl<MachineInstr *> &CCUsers) {
252 unsigned LATOpcode = TII->getLoadAndTrap(MI.getOpcode());
253 if (!LATOpcode)
254 return false;
255
256 // Check whether we have a single CondTrap that traps on zero.
257 if (CCUsers.size() != 1)
258 return false;
259 MachineInstr *Branch = CCUsers[0];
260 if (Branch->getOpcode() != SystemZ::CondTrap ||
261 Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
262 Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_EQ)
263 return false;
264
265 // We already know that there are no references to the register between
266 // MI and Compare. Make sure that there are also no references between
267 // Compare and Branch.
268 unsigned SrcReg = getCompareSourceReg(Compare);
269 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
270 for (++MBBI; MBBI != MBBE; ++MBBI)
271 if (getRegReferences(*MBBI, SrcReg))
272 return false;
273
274 // The transformation is OK. Rebuild Branch as a load-and-trap.
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000275 while (Branch->getNumOperands())
276 Branch->RemoveOperand(0);
277 Branch->setDesc(TII->get(LATOpcode));
278 MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
Diana Picus116bbab2017-01-13 09:58:52 +0000279 .add(MI.getOperand(0))
280 .add(MI.getOperand(1))
281 .add(MI.getOperand(2))
282 .add(MI.getOperand(3));
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000283 MI.eraseFromParent();
284 return true;
285}
286
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000287// If MI is a load instruction, try to convert it into a LOAD AND TEST.
288// Return true on success.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000289bool SystemZElimCompare::convertToLoadAndTest(
290 MachineInstr &MI, MachineInstr &Compare,
291 SmallVectorImpl<MachineInstr *> &CCUsers) {
292
293 // 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 +0000294 unsigned Opcode = TII->getLoadAndTest(MI.getOpcode());
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000295 if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode))
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000296 return false;
297
Jonas Paulssone80d4052018-06-07 05:59:07 +0000298 // Rebuild to get the CC operand in the right place.
Chandler Carruthc73c0302018-08-16 21:30:05 +0000299 auto MIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode));
Jonas Paulssone80d4052018-06-07 05:59:07 +0000300 for (const auto &MO : MI.operands())
Chandler Carruthc73c0302018-08-16 21:30:05 +0000301 MIB.add(MO);
302 MIB.setMemRefs(MI.memoperands());
Jonas Paulssone80d4052018-06-07 05:59:07 +0000303 MI.eraseFromParent();
304
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000305 return true;
306}
307
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000308// The CC users in CCUsers are testing the result of a comparison of some
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000309// value X against zero and we know that any CC value produced by MI would
310// also reflect the value of X. ConvOpc may be used to pass the transfomed
311// opcode MI will have if this succeeds. Try to adjust CCUsers so that they
312// test the result of MI directly, returning true on success. Leave
313// everything unchanged on failure.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000314bool SystemZElimCompare::adjustCCMasksForInstr(
315 MachineInstr &MI, MachineInstr &Compare,
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000316 SmallVectorImpl<MachineInstr *> &CCUsers,
317 unsigned ConvOpc) {
318 int Opcode = (ConvOpc ? ConvOpc : MI.getOpcode());
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000319 const MCInstrDesc &Desc = TII->get(Opcode);
320 unsigned MIFlags = Desc.TSFlags;
321
322 // See which compare-style condition codes are available.
Richard Sandiford0897fce2013-08-07 11:10:06 +0000323 unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000324
325 // For unsigned comparisons with zero, only equality makes sense.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000326 unsigned CompareFlags = Compare.getDesc().TSFlags;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000327 if (CompareFlags & SystemZII::IsLogical)
328 ReusableCCMask &= SystemZ::CCMASK_CMP_EQ;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000329
330 if (ReusableCCMask == 0)
331 return false;
332
333 unsigned CCValues = SystemZII::getCCValues(MIFlags);
334 assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues");
335
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000336 bool MIEquivalentToCmp =
337 (ReusableCCMask == CCValues &&
338 CCValues == SystemZII::getCCValues(CompareFlags));
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000339
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000340 if (!MIEquivalentToCmp) {
341 // Now check whether these flags are enough for all users.
342 SmallVector<MachineOperand *, 4> AlterMasks;
343 for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) {
344 MachineInstr *MI = CCUsers[I];
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000345
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000346 // Fail if this isn't a use of CC that we understand.
347 unsigned Flags = MI->getDesc().TSFlags;
348 unsigned FirstOpNum;
349 if (Flags & SystemZII::CCMaskFirst)
350 FirstOpNum = 0;
351 else if (Flags & SystemZII::CCMaskLast)
352 FirstOpNum = MI->getNumExplicitOperands() - 2;
353 else
354 return false;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000355
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000356 // Check whether the instruction predicate treats all CC values
357 // outside of ReusableCCMask in the same way. In that case it
358 // doesn't matter what those CC values mean.
359 unsigned CCValid = MI->getOperand(FirstOpNum).getImm();
360 unsigned CCMask = MI->getOperand(FirstOpNum + 1).getImm();
361 unsigned OutValid = ~ReusableCCMask & CCValid;
362 unsigned OutMask = ~ReusableCCMask & CCMask;
363 if (OutMask != 0 && OutMask != OutValid)
364 return false;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000365
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000366 AlterMasks.push_back(&MI->getOperand(FirstOpNum));
367 AlterMasks.push_back(&MI->getOperand(FirstOpNum + 1));
368 }
369
370 // All users are OK. Adjust the masks for MI.
371 for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) {
372 AlterMasks[I]->setImm(CCValues);
373 unsigned CCMask = AlterMasks[I + 1]->getImm();
374 if (CCMask & ~ReusableCCMask)
375 AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) |
376 (CCValues & ~ReusableCCMask));
377 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000378 }
379
380 // CC is now live after MI.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000381 if (!ConvOpc) {
382 int CCDef = MI.findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI);
383 assert(CCDef >= 0 && "Couldn't find CC set");
384 MI.getOperand(CCDef).setIsDead(false);
385 }
386
387 // Check if MI lies before Compare.
388 bool BeforeCmp = false;
389 MachineBasicBlock::iterator MBBI = MI, MBBE = MI.getParent()->end();
390 for (++MBBI; MBBI != MBBE; ++MBBI)
391 if (MBBI == Compare) {
392 BeforeCmp = true;
393 break;
394 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000395
396 // Clear any intervening kills of CC.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000397 if (BeforeCmp) {
398 MachineBasicBlock::iterator MBBI = MI, MBBE = Compare;
399 for (++MBBI; MBBI != MBBE; ++MBBI)
400 MBBI->clearRegisterKills(SystemZ::CC, TRI);
401 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000402
403 return true;
404}
405
Richard Sandiford0897fce2013-08-07 11:10:06 +0000406// Return true if Compare is a comparison against zero.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000407static bool isCompareZero(MachineInstr &Compare) {
408 switch (Compare.getOpcode()) {
Richard Sandiford0897fce2013-08-07 11:10:06 +0000409 case SystemZ::LTEBRCompare:
410 case SystemZ::LTDBRCompare:
411 case SystemZ::LTXBRCompare:
412 return true;
413
414 default:
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000415 if (isLoadAndTestAsCmp(Compare))
416 return true;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000417 return Compare.getNumExplicitOperands() == 2 &&
418 Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000419 }
420}
421
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000422// Try to optimize cases where comparison instruction Compare is testing
423// a value against zero. Return true on success and if Compare should be
424// deleted as dead. CCUsers is the list of instructions that use the CC
425// value produced by Compare.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000426bool SystemZElimCompare::optimizeCompareZero(
427 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandiford0897fce2013-08-07 11:10:06 +0000428 if (!isCompareZero(Compare))
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000429 return false;
430
431 // Search back for CC results that are based on the first operand.
Jonas Paulsson5d3fbd32015-10-08 07:40:23 +0000432 unsigned SrcReg = getCompareSourceReg(Compare);
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000433 MachineBasicBlock &MBB = *Compare.getParent();
Richard Sandifordc2121252013-08-05 11:23:46 +0000434 Reference CCRefs;
435 Reference SrcRefs;
Jonas Paulssone80d4052018-06-07 05:59:07 +0000436 for (MachineBasicBlock::reverse_iterator MBBI =
437 std::next(MachineBasicBlock::reverse_iterator(&Compare)),
438 MBBE = MBB.rend(); MBBI != MBBE;) {
439 MachineInstr &MI = *MBBI++;
Jonas Paulsson2c96dd62015-10-08 07:40:11 +0000440 if (resultTests(MI, SrcReg)) {
Richard Sandifordc2121252013-08-05 11:23:46 +0000441 // Try to remove both MI and Compare by converting a branch to BRCT(G).
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +0000442 // or a load-and-trap instruction. We don't care in this case whether
443 // CC is modified between MI and Compare.
444 if (!CCRefs.Use && !SrcRefs) {
445 if (convertToBRCT(MI, Compare, CCUsers)) {
446 BranchOnCounts += 1;
447 return true;
448 }
449 if (convertToLoadAndTrap(MI, Compare, CCUsers)) {
450 LoadAndTraps += 1;
451 return true;
452 }
Richard Sandifordc2121252013-08-05 11:23:46 +0000453 }
454 // Try to eliminate Compare by reusing a CC result from MI.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000455 if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) ||
Richard Sandifordc2121252013-08-05 11:23:46 +0000456 (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) {
457 EliminatedComparisons += 1;
458 return true;
459 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000460 }
Richard Sandifordc2121252013-08-05 11:23:46 +0000461 SrcRefs |= getRegReferences(MI, SrcReg);
462 if (SrcRefs.Def)
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000463 break;
Richard Sandifordc2121252013-08-05 11:23:46 +0000464 CCRefs |= getRegReferences(MI, SystemZ::CC);
465 if (CCRefs.Use && CCRefs.Def)
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000466 break;
467 }
468
469 // Also do a forward search to handle cases where an instruction after the
Jonas Paulsson22f208f2018-01-08 12:52:40 +0000470 // compare can be converted, like
471 // LTEBRCompare %f0s, %f0s; %f2s = LER %f0s => LTEBRCompare %f2s, %f0s
Jonas Paulssone80d4052018-06-07 05:59:07 +0000472 for (MachineBasicBlock::iterator MBBI =
473 std::next(MachineBasicBlock::iterator(&Compare)), MBBE = MBB.end();
474 MBBI != MBBE;) {
475 MachineInstr &MI = *MBBI++;
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000476 if (preservesValueOf(MI, SrcReg)) {
477 // Try to eliminate Compare by reusing a CC result from MI.
Jonas Paulsson776a81a2018-01-15 15:41:26 +0000478 if (convertToLoadAndTest(MI, Compare, CCUsers)) {
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000479 EliminatedComparisons += 1;
480 return true;
481 }
482 }
483 if (getRegReferences(MI, SrcReg).Def)
484 return false;
485 if (getRegReferences(MI, SystemZ::CC))
Richard Sandifordc2121252013-08-05 11:23:46 +0000486 return false;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000487 }
Jonas Paulssonb0e8a2e2017-09-21 13:52:24 +0000488
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000489 return false;
490}
491
492// Try to fuse comparison instruction Compare into a later branch.
493// Return true on success and if Compare is therefore redundant.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000494bool SystemZElimCompare::fuseCompareOperations(
495 MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000496 // See whether we have a single branch with which to fuse.
497 if (CCUsers.size() != 1)
498 return false;
499 MachineInstr *Branch = CCUsers[0];
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000500 SystemZII::FusedCompareType Type;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000501 switch (Branch->getOpcode()) {
502 case SystemZ::BRC:
503 Type = SystemZII::CompareAndBranch;
504 break;
505 case SystemZ::CondReturn:
506 Type = SystemZII::CompareAndReturn;
507 break;
Ulrich Weigand848a5132016-04-11 12:12:32 +0000508 case SystemZ::CallBCR:
509 Type = SystemZII::CompareAndSibcall;
510 break;
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000511 case SystemZ::CondTrap:
512 Type = SystemZII::CompareAndTrap;
513 break;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000514 default:
515 return false;
516 }
517
518 // See whether we have a comparison that can be fused.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000519 unsigned FusedOpcode =
520 TII->getFusedCompare(Compare.getOpcode(), Type, &Compare);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000521 if (!FusedOpcode)
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000522 return false;
523
524 // Make sure that the operands are available at the branch.
Ulrich Weiganda0e73252016-11-11 12:48:26 +0000525 // SrcReg2 is the register if the source operand is a register,
526 // 0 if the source operand is immediate, and the base register
527 // if the source operand is memory (index is not supported).
Matt Arsenaulte3a676e2019-06-24 15:50:29 +0000528 Register SrcReg = Compare.getOperand(0).getReg();
529 Register SrcReg2 =
530 Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : Register();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000531 MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
532 for (++MBBI; MBBI != MBBE; ++MBBI)
533 if (MBBI->modifiesRegister(SrcReg, TRI) ||
534 (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
535 return false;
536
Ulrich Weigand848a5132016-04-11 12:12:32 +0000537 // Read the branch mask, target (if applicable), regmask (if applicable).
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000538 MachineOperand CCMask(MBBI->getOperand(1));
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000539 assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
540 "Invalid condition-code mask for integer comparison");
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000541 // This is only valid for CompareAndBranch.
542 MachineOperand Target(MBBI->getOperand(
543 Type == SystemZII::CompareAndBranch ? 2 : 0));
Ulrich Weigand848a5132016-04-11 12:12:32 +0000544 const uint32_t *RegMask;
545 if (Type == SystemZII::CompareAndSibcall)
546 RegMask = MBBI->getOperand(2).getRegMask();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000547
548 // Clear out all current operands.
549 int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000550 assert(CCUse >= 0 && "BRC/BCR must use CC");
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000551 Branch->RemoveOperand(CCUse);
Ulrich Weigand848a5132016-04-11 12:12:32 +0000552 // Remove target (branch) or regmask (sibcall).
553 if (Type == SystemZII::CompareAndBranch ||
554 Type == SystemZII::CompareAndSibcall)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000555 Branch->RemoveOperand(2);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000556 Branch->RemoveOperand(1);
557 Branch->RemoveOperand(0);
558
559 // Rebuild Branch as a fused compare and branch.
Ulrich Weiganda0e73252016-11-11 12:48:26 +0000560 // SrcNOps is the number of MI operands of the compare instruction
561 // that we need to copy over.
562 unsigned SrcNOps = 2;
563 if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT)
564 SrcNOps = 3;
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000565 Branch->setDesc(TII->get(FusedOpcode));
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000566 MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
Ulrich Weiganda0e73252016-11-11 12:48:26 +0000567 for (unsigned I = 0; I < SrcNOps; I++)
Diana Picus116bbab2017-01-13 09:58:52 +0000568 MIB.add(Compare.getOperand(I));
569 MIB.add(CCMask);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000570
571 if (Type == SystemZII::CompareAndBranch) {
572 // Only conditional branches define CC, as they may be converted back
573 // to a non-fused branch because of a long displacement. Conditional
574 // returns don't have that problem.
Diana Picus116bbab2017-01-13 09:58:52 +0000575 MIB.add(Target).addReg(SystemZ::CC,
576 RegState::ImplicitDefine | RegState::Dead);
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000577 }
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000578
Ulrich Weigand848a5132016-04-11 12:12:32 +0000579 if (Type == SystemZII::CompareAndSibcall)
580 MIB.addRegMask(RegMask);
581
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000582 // Clear any intervening kills of SrcReg and SrcReg2.
583 MBBI = Compare;
584 for (++MBBI; MBBI != MBBE; ++MBBI) {
585 MBBI->clearRegisterKills(SrcReg, TRI);
586 if (SrcReg2)
587 MBBI->clearRegisterKills(SrcReg2, TRI);
588 }
589 FusedComparisons += 1;
590 return true;
591}
592
593// Process all comparison instructions in MBB. Return true if something
594// changed.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000595bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000596 bool Changed = false;
597
598 // Walk backwards through the block looking for comparisons, recording
599 // all CC users as we go. The subroutines can delete Compare and
600 // instructions before it.
601 bool CompleteCCUsers = !isCCLiveOut(MBB);
602 SmallVector<MachineInstr *, 4> CCUsers;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000603 MachineBasicBlock::iterator MBBI = MBB.end();
604 while (MBBI != MBB.begin()) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000605 MachineInstr &MI = *--MBBI;
606 if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) &&
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000607 (optimizeCompareZero(MI, CCUsers) ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000608 fuseCompareOperations(MI, CCUsers))) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000609 ++MBBI;
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000610 MI.eraseFromParent();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000611 Changed = true;
612 CCUsers.clear();
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000613 continue;
614 }
615
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000616 if (MI.definesRegister(SystemZ::CC)) {
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000617 CCUsers.clear();
Jonas Paulsson9e1f3bd2015-10-08 07:39:55 +0000618 CompleteCCUsers = true;
Richard Sandifordc2121252013-08-05 11:23:46 +0000619 }
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000620 if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers)
621 CCUsers.push_back(&MI);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000622 }
623 return Changed;
624}
625
626bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000627 if (skipFunction(F.getFunction()))
Andrew Kaylord9974cc2016-04-26 23:49:41 +0000628 return false;
629
Eric Christopherfc6de422014-08-05 02:39:49 +0000630 TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000631 TRI = &TII->getRegisterInfo();
632
633 bool Changed = false;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000634 for (auto &MBB : F)
635 Changed |= processBlock(MBB);
Richard Sandifordbdbb8af2013-08-05 10:58:53 +0000636
637 return Changed;
638}
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000639
640FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) {
641 return new SystemZElimCompare(TM);
642}