blob: d2e2e27f397b4118e7032c4037ad729abae86577 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZInstrInfo.cpp - SystemZ instruction information ------------===//
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 file contains the SystemZ implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SystemZInstrInfo.h"
15#include "SystemZInstrBuilder.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "SystemZTargetMachine.h"
Richard Sandifordff6c5a52013-07-19 16:12:08 +000017#include "llvm/CodeGen/LiveVariables.h"
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +000018#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Richard Sandifordf6bae1e2013-07-02 15:28:56 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000020
Chandler Carruthd174b722014-04-22 02:03:14 +000021using namespace llvm;
22
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000023#define GET_INSTRINFO_CTOR_DTOR
Ulrich Weigand5f613df2013-05-06 16:15:19 +000024#define GET_INSTRMAP_INFO
25#include "SystemZGenInstrInfo.inc"
26
Richard Sandiford6a06ba32013-07-31 11:36:35 +000027// Return a mask with Count low bits set.
28static uint64_t allOnes(unsigned int Count) {
29 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
30}
31
Richard Sandiford0755c932013-10-01 11:26:28 +000032// Reg should be a 32-bit GPR. Return true if it is a high register rather
33// than a low register.
34static bool isHighReg(unsigned int Reg) {
35 if (SystemZ::GRH32BitRegClass.contains(Reg))
36 return true;
37 assert(SystemZ::GR32BitRegClass.contains(Reg) && "Invalid GRX32");
38 return false;
39}
40
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000041// Pin the vtable to this file.
42void SystemZInstrInfo::anchor() {}
43
Eric Christopher673b3af2014-06-27 07:01:17 +000044SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti)
Ulrich Weigand5f613df2013-05-06 16:15:19 +000045 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
Eric Christopher673b3af2014-06-27 07:01:17 +000046 RI(), STI(sti) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000047}
48
49// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
50// each having the opcode given by NewOpcode.
51void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
52 unsigned NewOpcode) const {
53 MachineBasicBlock *MBB = MI->getParent();
54 MachineFunction &MF = *MBB->getParent();
55
56 // Get two load or store instructions. Use the original instruction for one
Alp Tokercb402912014-01-24 17:20:08 +000057 // of them (arbitrarily the second here) and create a clone for the other.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000058 MachineInstr *EarlierMI = MF.CloneMachineInstr(MI);
59 MBB->insert(MI, EarlierMI);
60
61 // Set up the two 64-bit registers.
62 MachineOperand &HighRegOp = EarlierMI->getOperand(0);
63 MachineOperand &LowRegOp = MI->getOperand(0);
Richard Sandiford87a44362013-09-30 10:28:35 +000064 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64));
65 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64));
Ulrich Weigand5f613df2013-05-06 16:15:19 +000066
67 // The address in the first (high) instruction is already correct.
68 // Adjust the offset in the second (low) instruction.
69 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
70 MachineOperand &LowOffsetOp = MI->getOperand(2);
71 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
72
Jonas Paulsson2ba31522016-03-31 08:00:14 +000073 // Clear the kill flags for the base and index registers in the first
74 // instruction.
Jonas Paulsson63a2b682015-10-10 07:14:24 +000075 EarlierMI->getOperand(1).setIsKill(false);
Jonas Paulsson7da38202015-10-26 15:03:41 +000076 EarlierMI->getOperand(3).setIsKill(false);
Jonas Paulsson63a2b682015-10-10 07:14:24 +000077
Ulrich Weigand5f613df2013-05-06 16:15:19 +000078 // Set the opcodes.
79 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
80 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
81 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
82
83 EarlierMI->setDesc(get(HighOpcode));
84 MI->setDesc(get(LowOpcode));
85}
86
87// Split ADJDYNALLOC instruction MI.
88void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
89 MachineBasicBlock *MBB = MI->getParent();
90 MachineFunction &MF = *MBB->getParent();
91 MachineFrameInfo *MFFrame = MF.getFrameInfo();
92 MachineOperand &OffsetMO = MI->getOperand(2);
93
94 uint64_t Offset = (MFFrame->getMaxCallFrameSize() +
95 SystemZMC::CallFrameSize +
96 OffsetMO.getImm());
97 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
98 assert(NewOpcode && "No support for huge argument lists yet");
99 MI->setDesc(get(NewOpcode));
100 OffsetMO.setImm(Offset);
101}
102
Richard Sandiford01240232013-10-01 13:02:28 +0000103// MI is an RI-style pseudo instruction. Replace it with LowOpcode
104// if the first operand is a low GR32 and HighOpcode if the first operand
105// is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
106// and HighOpcode takes an unsigned 32-bit operand. In those cases,
107// MI has the same kind of operand as LowOpcode, so needs to be converted
108// if HighOpcode is used.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000109void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford01240232013-10-01 13:02:28 +0000110 unsigned HighOpcode,
111 bool ConvertHigh) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000112 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford01240232013-10-01 13:02:28 +0000113 bool IsHigh = isHighReg(Reg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000114 MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode));
Richard Sandiford01240232013-10-01 13:02:28 +0000115 if (IsHigh && ConvertHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000116 MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm()));
Richard Sandiford01240232013-10-01 13:02:28 +0000117}
118
Richard Sandiford42a694f2013-10-01 14:53:46 +0000119// MI is a three-operand RIE-style pseudo instruction. Replace it with
Jonas Paulsson18d877f2015-10-09 07:19:16 +0000120// LowOpcodeK if the registers are both low GR32s, otherwise use a move
Richard Sandiford42a694f2013-10-01 14:53:46 +0000121// followed by HighOpcode or LowOpcode, depending on whether the target
122// is a high or low GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000123void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford42a694f2013-10-01 14:53:46 +0000124 unsigned LowOpcodeK,
125 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000126 unsigned DestReg = MI.getOperand(0).getReg();
127 unsigned SrcReg = MI.getOperand(1).getReg();
Richard Sandiford42a694f2013-10-01 14:53:46 +0000128 bool DestIsHigh = isHighReg(DestReg);
129 bool SrcIsHigh = isHighReg(SrcReg);
130 if (!DestIsHigh && !SrcIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000131 MI.setDesc(get(LowOpcodeK));
Richard Sandiford42a694f2013-10-01 14:53:46 +0000132 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000133 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg,
134 SystemZ::LR, 32, MI.getOperand(1).isKill());
135 MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
136 MI.getOperand(1).setReg(DestReg);
137 MI.tieOperands(0, 1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000138 }
139}
140
Richard Sandiford0755c932013-10-01 11:26:28 +0000141// MI is an RXY-style pseudo instruction. Replace it with LowOpcode
142// if the first operand is a low GR32 and HighOpcode if the first operand
143// is a high GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000144void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford0755c932013-10-01 11:26:28 +0000145 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000146 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000147 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000148 MI.getOperand(2).getImm());
149 MI.setDesc(get(Opcode));
Richard Sandiford0755c932013-10-01 11:26:28 +0000150}
151
Richard Sandiford21235a22013-10-01 12:49:07 +0000152// MI is an RR-style pseudo instruction that zero-extends the low Size bits
153// of one GRX32 into another. Replace it with LowOpcode if both operands
154// are low registers, otherwise use RISB[LH]G.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000155void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford21235a22013-10-01 12:49:07 +0000156 unsigned Size) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000157 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(),
158 MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode,
159 Size, MI.getOperand(1).isKill());
160 MI.eraseFromParent();
Richard Sandiford21235a22013-10-01 12:49:07 +0000161}
162
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000163void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const {
164 MachineBasicBlock *MBB = MI->getParent();
165 MachineFunction &MF = *MBB->getParent();
166 const unsigned Reg = MI->getOperand(0).getReg();
167
168 // Conveniently, all 4 instructions are cloned from LOAD_STACK_GUARD,
169 // so they already have operand 0 set to reg.
170
171 // ear <reg>, %a0
172 MachineInstr *Ear1MI = MF.CloneMachineInstr(MI);
173 MBB->insert(MI, Ear1MI);
174 Ear1MI->setDesc(get(SystemZ::EAR));
175 MachineInstrBuilder(MF, Ear1MI).addImm(0);
176
177 // sllg <reg>, <reg>, 32
178 MachineInstr *SllgMI = MF.CloneMachineInstr(MI);
179 MBB->insert(MI, SllgMI);
180 SllgMI->setDesc(get(SystemZ::SLLG));
181 MachineInstrBuilder(MF, SllgMI).addReg(Reg).addReg(0).addImm(32);
182
183 // ear <reg>, %a1
184 MachineInstr *Ear2MI = MF.CloneMachineInstr(MI);
185 MBB->insert(MI, Ear2MI);
186 Ear2MI->setDesc(get(SystemZ::EAR));
187 MachineInstrBuilder(MF, Ear2MI).addImm(1);
188
189 // lg <reg>, 40(<reg>)
190 MI->setDesc(get(SystemZ::LG));
191 MachineInstrBuilder(MF, MI).addReg(Reg).addImm(40).addReg(0);
192}
193
Richard Sandiford0755c932013-10-01 11:26:28 +0000194// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
195// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
196// are low registers, otherwise use RISB[LH]G. Size is the number of bits
197// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
198// KillSrc is true if this move is the last use of SrcReg.
199void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
200 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000201 const DebugLoc &DL, unsigned DestReg,
Richard Sandiford0755c932013-10-01 11:26:28 +0000202 unsigned SrcReg, unsigned LowLowOpcode,
203 unsigned Size, bool KillSrc) const {
204 unsigned Opcode;
205 bool DestIsHigh = isHighReg(DestReg);
206 bool SrcIsHigh = isHighReg(SrcReg);
207 if (DestIsHigh && SrcIsHigh)
208 Opcode = SystemZ::RISBHH;
209 else if (DestIsHigh && !SrcIsHigh)
210 Opcode = SystemZ::RISBHL;
211 else if (!DestIsHigh && SrcIsHigh)
212 Opcode = SystemZ::RISBLH;
213 else {
214 BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
215 .addReg(SrcReg, getKillRegState(KillSrc));
216 return;
217 }
218 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
219 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
220 .addReg(DestReg, RegState::Undef)
221 .addReg(SrcReg, getKillRegState(KillSrc))
222 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
223}
224
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000225// If MI is a simple load or store for a frame object, return the register
226// it loads or stores and set FrameIndex to the index of the frame object.
227// Return 0 otherwise.
228//
229// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000230static int isSimpleMove(const MachineInstr &MI, int &FrameIndex,
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000231 unsigned Flag) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000232 const MCInstrDesc &MCID = MI.getDesc();
233 if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() &&
234 MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) {
235 FrameIndex = MI.getOperand(1).getIndex();
236 return MI.getOperand(0).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000237 }
238 return 0;
239}
240
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000241unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000242 int &FrameIndex) const {
243 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
244}
245
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000246unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000247 int &FrameIndex) const {
248 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
249}
250
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000251bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI,
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000252 int &DestFrameIndex,
253 int &SrcFrameIndex) const {
254 // Check for MVC 0(Length,FI1),0(FI2)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000255 const MachineFrameInfo *MFI = MI.getParent()->getParent()->getFrameInfo();
256 if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() ||
257 MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() ||
258 MI.getOperand(4).getImm() != 0)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000259 return false;
260
261 // Check that Length covers the full slots.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000262 int64_t Length = MI.getOperand(2).getImm();
263 unsigned FI1 = MI.getOperand(0).getIndex();
264 unsigned FI2 = MI.getOperand(3).getIndex();
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000265 if (MFI->getObjectSize(FI1) != Length ||
266 MFI->getObjectSize(FI2) != Length)
267 return false;
268
269 DestFrameIndex = FI1;
270 SrcFrameIndex = FI2;
271 return true;
272}
273
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000274bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
275 MachineBasicBlock *&TBB,
276 MachineBasicBlock *&FBB,
277 SmallVectorImpl<MachineOperand> &Cond,
278 bool AllowModify) const {
279 // Most of the code and comments here are boilerplate.
280
281 // Start from the bottom of the block and work up, examining the
282 // terminator instructions.
283 MachineBasicBlock::iterator I = MBB.end();
284 while (I != MBB.begin()) {
285 --I;
286 if (I->isDebugValue())
287 continue;
288
289 // Working from the bottom, when we see a non-terminator instruction, we're
290 // done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000291 if (!isUnpredicatedTerminator(*I))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000292 break;
293
294 // A terminator that isn't a branch can't easily be handled by this
295 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000296 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000297 return true;
298
299 // Can't handle indirect branches.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000300 SystemZII::Branch Branch(getBranchInfo(*I));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000301 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000302 return true;
303
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000304 // Punt on compound branches.
305 if (Branch.Type != SystemZII::BranchNormal)
306 return true;
307
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000308 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000309 // Handle unconditional branches.
310 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000311 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000312 continue;
313 }
314
315 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000316 while (std::next(I) != MBB.end())
317 std::next(I)->eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000318
319 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000320 FBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000321
322 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000323 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000324 TBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000325 I->eraseFromParent();
326 I = MBB.end();
327 continue;
328 }
329
330 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000331 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000332 continue;
333 }
334
335 // Working from the bottom, handle the first conditional branch.
336 if (Cond.empty()) {
337 // FIXME: add X86-style branch swap
338 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000339 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000340 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000341 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000342 continue;
343 }
344
345 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000346 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000347
348 // Only handle the case where all conditional branches branch to the same
349 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000350 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000351 return true;
352
353 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000354 unsigned OldCCValid = Cond[0].getImm();
355 unsigned OldCCMask = Cond[1].getImm();
356 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000357 continue;
358
359 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000360 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000361 }
362
363 return false;
364}
365
366unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
367 // Most of the code and comments here are boilerplate.
368 MachineBasicBlock::iterator I = MBB.end();
369 unsigned Count = 0;
370
371 while (I != MBB.begin()) {
372 --I;
373 if (I->isDebugValue())
374 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000375 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000376 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000377 if (!getBranchInfo(*I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000378 break;
379 // Remove the branch.
380 I->eraseFromParent();
381 I = MBB.end();
382 ++Count;
383 }
384
385 return Count;
386}
387
Richard Sandiford3d768e32013-07-31 12:30:20 +0000388bool SystemZInstrInfo::
389ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
390 assert(Cond.size() == 2 && "Invalid condition");
391 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
392 return false;
393}
394
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000395unsigned SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB,
396 MachineBasicBlock *TBB,
397 MachineBasicBlock *FBB,
398 ArrayRef<MachineOperand> Cond,
399 const DebugLoc &DL) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000400 // In this function we output 32-bit branches, which should always
401 // have enough range. They can be shortened and relaxed by later code
402 // in the pipeline, if desired.
403
404 // Shouldn't be a fall through.
405 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000406 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000407 "SystemZ branch conditions have one component!");
408
409 if (Cond.empty()) {
410 // Unconditional branch?
411 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000412 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000413 return 1;
414 }
415
416 // Conditional branch.
417 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000418 unsigned CCValid = Cond[0].getImm();
419 unsigned CCMask = Cond[1].getImm();
420 BuildMI(&MBB, DL, get(SystemZ::BRC))
421 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000422 ++Count;
423
424 if (FBB) {
425 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000426 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000427 ++Count;
428 }
429 return Count;
430}
431
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000432bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
433 unsigned &SrcReg2, int &Mask,
434 int &Value) const {
435 assert(MI.isCompare() && "Caller should have checked for a comparison");
Richard Sandiford564681c2013-08-12 10:28:10 +0000436
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000437 if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() &&
438 MI.getOperand(1).isImm()) {
439 SrcReg = MI.getOperand(0).getReg();
Richard Sandiford564681c2013-08-12 10:28:10 +0000440 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000441 Value = MI.getOperand(1).getImm();
Richard Sandiford564681c2013-08-12 10:28:10 +0000442 Mask = ~0;
443 return true;
444 }
445
446 return false;
447}
448
Richard Sandiforda5901252013-08-16 10:22:54 +0000449// If Reg is a virtual register, return its definition, otherwise return null.
450static MachineInstr *getDef(unsigned Reg,
451 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000452 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +0000453 return nullptr;
Richard Sandiford564681c2013-08-12 10:28:10 +0000454 return MRI->getUniqueVRegDef(Reg);
455}
456
457// Return true if MI is a shift of type Opcode by Imm bits.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000458static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000459 return (MI->getOpcode() == Opcode &&
460 !MI->getOperand(2).getReg() &&
461 MI->getOperand(3).getImm() == Imm);
462}
463
Richard Sandiforda5901252013-08-16 10:22:54 +0000464// If the destination of MI has no uses, delete it as dead.
465static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
466 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
467 MI->eraseFromParent();
468}
469
Richard Sandiford564681c2013-08-12 10:28:10 +0000470// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000471// the result of an IPM sequence whose input CC survives until Compare,
472// and whether Compare is therefore redundant. Delete it and return
473// true if so.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000474static bool removeIPMBasedCompare(MachineInstr &Compare, unsigned SrcReg,
Richard Sandiforda5901252013-08-16 10:22:54 +0000475 const MachineRegisterInfo *MRI,
476 const TargetRegisterInfo *TRI) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000477 MachineInstr *LGFR = nullptr;
Richard Sandiforda5901252013-08-16 10:22:54 +0000478 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000479 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
480 LGFR = RLL;
481 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
482 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000483 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000484 return false;
485
Richard Sandiforda5901252013-08-16 10:22:54 +0000486 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000487 if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
Richard Sandiford564681c2013-08-12 10:28:10 +0000488 return false;
489
Richard Sandiforda5901252013-08-16 10:22:54 +0000490 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000491 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
492 return false;
493
494 // Check that there are no assignments to CC between the IPM and Compare,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000495 if (IPM->getParent() != Compare.getParent())
Richard Sandiford564681c2013-08-12 10:28:10 +0000496 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000497 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare.getIterator();
Richard Sandiford564681c2013-08-12 10:28:10 +0000498 for (++MBBI; MBBI != MBBE; ++MBBI) {
499 MachineInstr *MI = MBBI;
Richard Sandiforda5901252013-08-16 10:22:54 +0000500 if (MI->modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000501 return false;
502 }
503
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000504 Compare.eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000505 if (LGFR)
506 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000507 eraseIfDead(RLL, MRI);
508 eraseIfDead(SRL, MRI);
509 eraseIfDead(IPM, MRI);
510
Richard Sandiford564681c2013-08-12 10:28:10 +0000511 return true;
512}
513
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000514bool SystemZInstrInfo::optimizeCompareInstr(
515 MachineInstr &Compare, unsigned SrcReg, unsigned SrcReg2, int Mask,
516 int Value, const MachineRegisterInfo *MRI) const {
Richard Sandiford564681c2013-08-12 10:28:10 +0000517 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000518 bool IsLogical = (Compare.getDesc().TSFlags & SystemZII::IsLogical) != 0;
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000519 return Value == 0 && !IsLogical &&
520 removeIPMBasedCompare(Compare, SrcReg, MRI, &RI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000521}
522
Richard Sandifordf2404162013-07-25 09:11:15 +0000523// If Opcode is a move that has a conditional variant, return that variant,
524// otherwise return 0.
525static unsigned getConditionalMove(unsigned Opcode) {
526 switch (Opcode) {
527 case SystemZ::LR: return SystemZ::LOCR;
528 case SystemZ::LGR: return SystemZ::LOCGR;
529 default: return 0;
530 }
531}
532
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000533bool SystemZInstrInfo::isPredicable(MachineInstr &MI) const {
534 unsigned Opcode = MI.getOpcode();
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000535 if (STI.hasLoadStoreOnCond() && getConditionalMove(Opcode))
536 return true;
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000537 if (Opcode == SystemZ::Return ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000538 Opcode == SystemZ::Trap ||
Ulrich Weigand848a5132016-04-11 12:12:32 +0000539 Opcode == SystemZ::CallJG ||
540 Opcode == SystemZ::CallBR)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000541 return true;
542 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000543}
544
545bool SystemZInstrInfo::
546isProfitableToIfCvt(MachineBasicBlock &MBB,
547 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000548 BranchProbability Probability) const {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000549 // Avoid using conditional returns at the end of a loop (since then
550 // we'd need to emit an unconditional branch to the beginning anyway,
551 // making the loop body longer). This doesn't apply for low-probability
552 // loops (eg. compare-and-swap retry), so just decide based on branch
553 // probability instead of looping structure.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000554 // However, since Compare and Trap instructions cost the same as a regular
555 // Compare instruction, we should allow the if conversion to convert this
556 // into a Conditional Compare regardless of the branch probability.
557 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap &&
558 MBB.succ_empty() && Probability < BranchProbability(1, 8))
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000559 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000560 // For now only convert single instructions.
561 return NumCycles == 1;
562}
563
564bool SystemZInstrInfo::
565isProfitableToIfCvt(MachineBasicBlock &TMBB,
566 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
567 MachineBasicBlock &FMBB,
568 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000569 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000570 // For now avoid converting mutually-exclusive cases.
571 return false;
572}
573
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000574bool SystemZInstrInfo::
575isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
576 BranchProbability Probability) const {
577 // For now only duplicate single instructions.
578 return NumCycles == 1;
579}
580
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000581bool SystemZInstrInfo::PredicateInstruction(
582 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000583 assert(Pred.size() == 2 && "Invalid condition");
584 unsigned CCValid = Pred[0].getImm();
585 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000586 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000587 unsigned Opcode = MI.getOpcode();
Eric Christopher673b3af2014-06-27 07:01:17 +0000588 if (STI.hasLoadStoreOnCond()) {
Richard Sandifordf2404162013-07-25 09:11:15 +0000589 if (unsigned CondOpcode = getConditionalMove(Opcode)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000590 MI.setDesc(get(CondOpcode));
591 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
592 .addImm(CCValid)
593 .addImm(CCMask)
594 .addReg(SystemZ::CC, RegState::Implicit);
Richard Sandifordf2404162013-07-25 09:11:15 +0000595 return true;
596 }
597 }
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000598 if (Opcode == SystemZ::Trap) {
599 MI.setDesc(get(SystemZ::CondTrap));
600 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
601 .addImm(CCValid).addImm(CCMask)
602 .addReg(SystemZ::CC, RegState::Implicit);
603 return true;
604 }
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000605 if (Opcode == SystemZ::Return) {
606 MI.setDesc(get(SystemZ::CondReturn));
607 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
608 .addImm(CCValid).addImm(CCMask)
609 .addReg(SystemZ::CC, RegState::Implicit);
610 return true;
611 }
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000612 if (Opcode == SystemZ::CallJG) {
Zhan Jun Liaua5d60af2016-07-07 15:34:46 +0000613 MachineOperand FirstOp = MI.getOperand(0);
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000614 const uint32_t *RegMask = MI.getOperand(1).getRegMask();
615 MI.RemoveOperand(1);
616 MI.RemoveOperand(0);
617 MI.setDesc(get(SystemZ::CallBRCL));
618 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
619 .addImm(CCValid).addImm(CCMask)
Zhan Jun Liaua5d60af2016-07-07 15:34:46 +0000620 .addOperand(FirstOp)
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000621 .addRegMask(RegMask)
622 .addReg(SystemZ::CC, RegState::Implicit);
623 return true;
624 }
Ulrich Weigand848a5132016-04-11 12:12:32 +0000625 if (Opcode == SystemZ::CallBR) {
626 const uint32_t *RegMask = MI.getOperand(0).getRegMask();
627 MI.RemoveOperand(0);
628 MI.setDesc(get(SystemZ::CallBCR));
629 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
630 .addImm(CCValid).addImm(CCMask)
631 .addRegMask(RegMask)
632 .addReg(SystemZ::CC, RegState::Implicit);
633 return true;
634 }
Richard Sandifordf2404162013-07-25 09:11:15 +0000635 return false;
636}
637
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000638void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
639 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000640 const DebugLoc &DL, unsigned DestReg,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000641 unsigned SrcReg, bool KillSrc) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000642 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
643 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000644 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
645 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
646 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
647 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000648 return;
649 }
650
Richard Sandiford0755c932013-10-01 11:26:28 +0000651 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
652 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
653 return;
654 }
655
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000656 // Everything else needs only one instruction.
657 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000658 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000659 Opcode = SystemZ::LGR;
660 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigandcdce0262016-03-14 13:50:03 +0000661 // For z13 we prefer LDR over LER to avoid partial register dependencies.
662 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000663 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
664 Opcode = SystemZ::LDR;
665 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
666 Opcode = SystemZ::LXR;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000667 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
668 Opcode = SystemZ::VLR32;
669 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
670 Opcode = SystemZ::VLR64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000671 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
672 Opcode = SystemZ::VLR;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000673 else
674 llvm_unreachable("Impossible reg-to-reg copy");
675
676 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
677 .addReg(SrcReg, getKillRegState(KillSrc));
678}
679
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000680void SystemZInstrInfo::storeRegToStackSlot(
681 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
682 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
683 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000684 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
685
686 // Callers may expect a single instruction, so keep 128-bit moves
687 // together for now and lower them after register allocation.
688 unsigned LoadOpcode, StoreOpcode;
689 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
690 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000691 .addReg(SrcReg, getKillRegState(isKill)),
692 FrameIdx);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000693}
694
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000695void SystemZInstrInfo::loadRegFromStackSlot(
696 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
697 int FrameIdx, const TargetRegisterClass *RC,
698 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000699 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
700
701 // Callers may expect a single instruction, so keep 128-bit moves
702 // together for now and lower them after register allocation.
703 unsigned LoadOpcode, StoreOpcode;
704 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
705 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
706 FrameIdx);
707}
708
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000709// Return true if MI is a simple load or store with a 12-bit displacement
710// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
711static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
712 const MCInstrDesc &MCID = MI->getDesc();
713 return ((MCID.TSFlags & Flag) &&
714 isUInt<12>(MI->getOperand(2).getImm()) &&
715 MI->getOperand(3).getReg() == 0);
716}
717
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000718namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +0000719struct LogicOp {
720 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
721 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
722 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000723
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000724 explicit operator bool() const { return RegSize; }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000725
Richard Sandifordc2312692014-03-06 10:38:30 +0000726 unsigned RegSize, ImmLSB, ImmSize;
727};
728} // end anonymous namespace
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000729
730static LogicOp interpretAndImmediate(unsigned Opcode) {
731 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000732 case SystemZ::NILMux: return LogicOp(32, 0, 16);
733 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000734 case SystemZ::NILL64: return LogicOp(64, 0, 16);
735 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000736 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
737 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
738 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000739 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000740 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000741 default: return LogicOp();
742 }
743}
744
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000745static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) {
746 if (OldMI->registerDefIsDead(SystemZ::CC)) {
747 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC);
748 if (CCDef != nullptr)
749 CCDef->setIsDead(true);
750 }
751}
752
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000753// Used to return from convertToThreeAddress after replacing two-address
754// instruction OldMI with three-address instruction NewMI.
755static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
756 MachineInstr *NewMI,
757 LiveVariables *LV) {
758 if (LV) {
759 unsigned NumOps = OldMI->getNumOperands();
760 for (unsigned I = 1; I < NumOps; ++I) {
761 MachineOperand &Op = OldMI->getOperand(I);
762 if (Op.isReg() && Op.isKill())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000763 LV->replaceKillInstruction(Op.getReg(), *OldMI, *NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000764 }
765 }
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000766 transferDeadCC(OldMI, NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000767 return NewMI;
768}
769
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000770MachineInstr *SystemZInstrInfo::convertToThreeAddress(
771 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
772 MachineBasicBlock *MBB = MI.getParent();
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000773 MachineFunction *MF = MBB->getParent();
774 MachineRegisterInfo &MRI = MF->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000775
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000776 unsigned Opcode = MI.getOpcode();
777 unsigned NumOps = MI.getNumOperands();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000778
779 // Try to convert something like SLL into SLLK, if supported.
780 // We prefer to keep the two-operand form where possible both
781 // because it tends to be shorter and because some instructions
782 // have memory forms that can be used during spilling.
Eric Christopher673b3af2014-06-27 07:01:17 +0000783 if (STI.hasDistinctOps()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000784 MachineOperand &Dest = MI.getOperand(0);
785 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000786 unsigned DestReg = Dest.getReg();
787 unsigned SrcReg = Src.getReg();
788 // AHIMux is only really a three-operand instruction when both operands
789 // are low registers. Try to constrain both operands to be low if
790 // possible.
791 if (Opcode == SystemZ::AHIMux &&
792 TargetRegisterInfo::isVirtualRegister(DestReg) &&
793 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
794 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
795 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
796 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
797 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
798 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000799 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
800 if (ThreeOperandOpcode >= 0) {
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000801 // Create three address instruction without adding the implicit
802 // operands. Those will instead be copied over from the original
803 // instruction by the loop below.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000804 MachineInstrBuilder MIB(
805 *MF, MF->CreateMachineInstr(get(ThreeOperandOpcode), MI.getDebugLoc(),
806 /*NoImplicit=*/true));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000807 MIB.addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000808 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000809 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000810 // Keep the remaining operands as-is.
811 for (unsigned I = 2; I < NumOps; ++I)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000812 MIB.addOperand(MI.getOperand(I));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000813 MBB->insert(MI, MIB);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000814 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000815 }
816 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000817
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000818 // Try to convert an AND into an RISBG-type instruction.
819 if (LogicOp And = interpretAndImmediate(Opcode)) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000820 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB;
Richard Sandiford70284282013-10-01 14:20:41 +0000821 // AND IMMEDIATE leaves the other bits of the register unchanged.
822 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
823 unsigned Start, End;
824 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
825 unsigned NewOpcode;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000826 if (And.RegSize == 64) {
Richard Sandiford70284282013-10-01 14:20:41 +0000827 NewOpcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000828 // Prefer RISBGN if available, since it does not clobber CC.
829 if (STI.hasMiscellaneousExtensions())
830 NewOpcode = SystemZ::RISBGN;
831 } else {
Richard Sandiford70284282013-10-01 14:20:41 +0000832 NewOpcode = SystemZ::RISBMux;
833 Start &= 31;
834 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000835 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000836 MachineOperand &Dest = MI.getOperand(0);
837 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford70284282013-10-01 14:20:41 +0000838 MachineInstrBuilder MIB =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000839 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode))
840 .addOperand(Dest)
841 .addReg(0)
842 .addReg(Src.getReg(), getKillRegState(Src.isKill()),
843 Src.getSubReg())
844 .addImm(Start)
845 .addImm(End + 128)
846 .addImm(0);
847 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000848 }
849 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000850 return nullptr;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000851}
852
Keno Fischere70b31f2015-06-08 20:09:58 +0000853MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000854 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000855 MachineBasicBlock::iterator InsertPt, int FrameIndex,
856 LiveIntervals *LIS) const {
857 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000858 const MachineFrameInfo *MFI = MF.getFrameInfo();
859 unsigned Size = MFI->getObjectSize(FrameIndex);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000860 unsigned Opcode = MI.getOpcode();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000861
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000862 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000863 if (LIS != nullptr && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
864 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) {
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000865
866 // Check CC liveness, since new instruction introduces a dead
867 // def of CC.
868 MCRegUnitIterator CCUnit(SystemZ::CC, TRI);
869 LiveRange &CCLiveRange = LIS->getRegUnit(*CCUnit);
870 ++CCUnit;
871 assert (!CCUnit.isValid() && "CC only has one reg unit.");
872 SlotIndex MISlot =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000873 LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot();
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000874 if (!CCLiveRange.liveAt(MISlot)) {
875 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000876 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt,
877 MI.getDebugLoc(), get(SystemZ::AGSI))
878 .addFrameIndex(FrameIndex)
879 .addImm(0)
880 .addImm(MI.getOperand(2).getImm());
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000881 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true);
882 CCLiveRange.createDeadDef(MISlot, LIS->getVNInfoAllocator());
883 return BuiltMI;
884 }
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000885 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000886 return nullptr;
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000887 }
888
889 // All other cases require a single operand.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000890 if (Ops.size() != 1)
Craig Topper062a2ba2014-04-25 05:30:21 +0000891 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000892
893 unsigned OpNum = Ops[0];
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000894 assert(Size ==
895 MF.getRegInfo()
896 .getRegClass(MI.getOperand(OpNum).getReg())
897 ->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000898 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000899
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000900 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 &&
901 isInt<8>(MI.getOperand(2).getImm())) {
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000902 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
903 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000904 MachineInstr *BuiltMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000905 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
906 .addFrameIndex(FrameIndex)
907 .addImm(0)
908 .addImm(MI.getOperand(2).getImm());
909 transferDeadCC(&MI, BuiltMI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000910 return BuiltMI;
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000911 }
912
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000913 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
914 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
915 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
916 // If we're spilling the destination of an LDGR or LGDR, store the
917 // source register instead.
918 if (OpNum == 0) {
919 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000920 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000921 get(StoreOpcode))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000922 .addOperand(MI.getOperand(1))
Keno Fischere70b31f2015-06-08 20:09:58 +0000923 .addFrameIndex(FrameIndex)
924 .addImm(0)
925 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000926 }
927 // If we're spilling the source of an LDGR or LGDR, load the
928 // destination register instead.
929 if (OpNum == 1) {
930 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000931 unsigned Dest = MI.getOperand(0).getReg();
932 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000933 get(LoadOpcode), Dest)
934 .addFrameIndex(FrameIndex)
935 .addImm(0)
936 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000937 }
938 }
939
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000940 // Look for cases where the source of a simple store or the destination
941 // of a simple load is being spilled. Try to use MVC instead.
942 //
943 // Although MVC is in practice a fast choice in these cases, it is still
944 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +0000945 // load or store is volatile. We also wouldn't be able to use MVC if
946 // the two memories partially overlap, but that case cannot occur here,
947 // because we know that one of the memories is a full frame index.
948 //
949 // For performance reasons, we also want to avoid using MVC if the addresses
950 // might be equal. We don't worry about that case here, because spill slot
951 // coloring happens later, and because we have special code to remove
952 // MVCs that turn out to be redundant.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000953 if (OpNum == 0 && MI.hasOneMemOperand()) {
954 MachineMemOperand *MMO = *MI.memoperands_begin();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000955 if (MMO->getSize() == Size && !MMO->isVolatile()) {
956 // Handle conversion of loads.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000957 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) {
958 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000959 get(SystemZ::MVC))
960 .addFrameIndex(FrameIndex)
961 .addImm(0)
962 .addImm(Size)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000963 .addOperand(MI.getOperand(1))
964 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +0000965 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000966 }
967 // Handle conversion of stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000968 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) {
969 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000970 get(SystemZ::MVC))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000971 .addOperand(MI.getOperand(1))
972 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +0000973 .addImm(Size)
974 .addFrameIndex(FrameIndex)
975 .addImm(0)
976 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000977 }
978 }
979 }
980
Richard Sandiforded1fab62013-07-03 10:10:02 +0000981 // If the spilled operand is the final one, try to change <INSN>R
982 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000983 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000984 if (MemOpcode >= 0) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000985 unsigned NumOps = MI.getNumExplicitOperands();
Richard Sandiforded1fab62013-07-03 10:10:02 +0000986 if (OpNum == NumOps - 1) {
987 const MCInstrDesc &MemDesc = get(MemOpcode);
988 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
989 assert(AccessBytes != 0 && "Size of access should be known");
990 assert(AccessBytes <= Size && "Access outside the frame index");
991 uint64_t Offset = Size - AccessBytes;
Keno Fischere70b31f2015-06-08 20:09:58 +0000992 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000993 MI.getDebugLoc(), get(MemOpcode));
Richard Sandiforded1fab62013-07-03 10:10:02 +0000994 for (unsigned I = 0; I < OpNum; ++I)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000995 MIB.addOperand(MI.getOperand(I));
Richard Sandiforded1fab62013-07-03 10:10:02 +0000996 MIB.addFrameIndex(FrameIndex).addImm(Offset);
997 if (MemDesc.TSFlags & SystemZII::HasIndex)
998 MIB.addReg(0);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000999 transferDeadCC(&MI, MIB);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001000 return MIB;
1001 }
1002 }
1003
Craig Topper062a2ba2014-04-25 05:30:21 +00001004 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001005}
1006
Keno Fischere70b31f2015-06-08 20:09:58 +00001007MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001008 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
1009 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001010 LiveIntervals *LIS) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001011 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001012}
1013
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001014bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1015 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001016 case SystemZ::L128:
1017 splitMove(MI, SystemZ::LG);
1018 return true;
1019
1020 case SystemZ::ST128:
1021 splitMove(MI, SystemZ::STG);
1022 return true;
1023
1024 case SystemZ::LX:
1025 splitMove(MI, SystemZ::LD);
1026 return true;
1027
1028 case SystemZ::STX:
1029 splitMove(MI, SystemZ::STD);
1030 return true;
1031
Richard Sandiford89e160d2013-10-01 12:11:47 +00001032 case SystemZ::LBMux:
1033 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
1034 return true;
1035
1036 case SystemZ::LHMux:
1037 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
1038 return true;
1039
Richard Sandiford21235a22013-10-01 12:49:07 +00001040 case SystemZ::LLCRMux:
1041 expandZExtPseudo(MI, SystemZ::LLCR, 8);
1042 return true;
1043
1044 case SystemZ::LLHRMux:
1045 expandZExtPseudo(MI, SystemZ::LLHR, 16);
1046 return true;
1047
Richard Sandiford0d46b1a2013-10-01 12:19:08 +00001048 case SystemZ::LLCMux:
1049 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
1050 return true;
1051
1052 case SystemZ::LLHMux:
1053 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
1054 return true;
1055
Richard Sandiford0755c932013-10-01 11:26:28 +00001056 case SystemZ::LMux:
1057 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
1058 return true;
1059
Richard Sandiford5469c392013-10-01 12:22:49 +00001060 case SystemZ::STCMux:
1061 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
1062 return true;
1063
1064 case SystemZ::STHMux:
1065 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
1066 return true;
1067
Richard Sandiford0755c932013-10-01 11:26:28 +00001068 case SystemZ::STMux:
1069 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
1070 return true;
1071
Richard Sandiford01240232013-10-01 13:02:28 +00001072 case SystemZ::LHIMux:
1073 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
1074 return true;
1075
1076 case SystemZ::IIFMux:
1077 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
1078 return true;
1079
Richard Sandiford1a569312013-10-01 13:18:56 +00001080 case SystemZ::IILMux:
1081 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
1082 return true;
1083
1084 case SystemZ::IIHMux:
1085 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
1086 return true;
1087
Richard Sandiford70284282013-10-01 14:20:41 +00001088 case SystemZ::NIFMux:
1089 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1090 return true;
1091
1092 case SystemZ::NILMux:
1093 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1094 return true;
1095
1096 case SystemZ::NIHMux:
1097 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1098 return true;
1099
Richard Sandiford6e96ac62013-10-01 13:22:41 +00001100 case SystemZ::OIFMux:
1101 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1102 return true;
1103
1104 case SystemZ::OILMux:
1105 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1106 return true;
1107
1108 case SystemZ::OIHMux:
1109 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1110 return true;
1111
Richard Sandiford5718dac2013-10-01 14:08:44 +00001112 case SystemZ::XIFMux:
1113 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1114 return true;
1115
Richard Sandiford2cac7632013-10-01 14:41:52 +00001116 case SystemZ::TMLMux:
1117 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1118 return true;
1119
1120 case SystemZ::TMHMux:
1121 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1122 return true;
1123
Richard Sandiford42a694f2013-10-01 14:53:46 +00001124 case SystemZ::AHIMux:
1125 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1126 return true;
1127
1128 case SystemZ::AHIMuxK:
1129 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1130 return true;
1131
1132 case SystemZ::AFIMux:
1133 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1134 return true;
1135
Richard Sandiforda9ac0e02013-10-01 14:56:23 +00001136 case SystemZ::CFIMux:
1137 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1138 return true;
1139
1140 case SystemZ::CLFIMux:
1141 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1142 return true;
1143
Richard Sandifordb63e3002013-10-01 15:00:44 +00001144 case SystemZ::CMux:
1145 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1146 return true;
1147
1148 case SystemZ::CLMux:
1149 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1150 return true;
1151
Richard Sandiford70284282013-10-01 14:20:41 +00001152 case SystemZ::RISBMux: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001153 bool DestIsHigh = isHighReg(MI.getOperand(0).getReg());
1154 bool SrcIsHigh = isHighReg(MI.getOperand(2).getReg());
Richard Sandiford70284282013-10-01 14:20:41 +00001155 if (SrcIsHigh == DestIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001156 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
Richard Sandiford70284282013-10-01 14:20:41 +00001157 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001158 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1159 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32);
Richard Sandiford70284282013-10-01 14:20:41 +00001160 }
1161 return true;
1162 }
1163
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001164 case SystemZ::ADJDYNALLOC:
1165 splitAdjDynAlloc(MI);
1166 return true;
1167
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001168 case TargetOpcode::LOAD_STACK_GUARD:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001169 expandLoadStackGuard(&MI);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001170 return true;
1171
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001172 default:
1173 return false;
1174 }
1175}
1176
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001177uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
1178 if (MI.getOpcode() == TargetOpcode::INLINEASM) {
1179 const MachineFunction *MF = MI.getParent()->getParent();
1180 const char *AsmStr = MI.getOperand(0).getSymbolName();
Richard Sandiford312425f2013-05-20 14:23:08 +00001181 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1182 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001183 return MI.getDesc().getSize();
Richard Sandiford312425f2013-05-20 14:23:08 +00001184}
1185
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001186SystemZII::Branch
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001187SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const {
1188 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001189 case SystemZ::BR:
1190 case SystemZ::J:
1191 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001192 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001193 SystemZ::CCMASK_ANY, &MI.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001194
1195 case SystemZ::BRC:
1196 case SystemZ::BRCL:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001197 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(),
1198 MI.getOperand(1).getImm(), &MI.getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001199
Richard Sandifordc2121252013-08-05 11:23:46 +00001200 case SystemZ::BRCT:
1201 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001202 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001203
1204 case SystemZ::BRCTG:
1205 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001206 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001207
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001208 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001209 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001210 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001211 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001212
Richard Sandiford93183ee2013-09-18 09:56:40 +00001213 case SystemZ::CLIJ:
1214 case SystemZ::CLRJ:
1215 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001216 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001217
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001218 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001219 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001220 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001221 MI.getOperand(2).getImm(), &MI.getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001222
Richard Sandiford93183ee2013-09-18 09:56:40 +00001223 case SystemZ::CLGIJ:
1224 case SystemZ::CLGRJ:
1225 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001226 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001227
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001228 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001229 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001230 }
1231}
1232
1233void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1234 unsigned &LoadOpcode,
1235 unsigned &StoreOpcode) const {
1236 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1237 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001238 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001239 } else if (RC == &SystemZ::GRH32BitRegClass) {
1240 LoadOpcode = SystemZ::LFH;
1241 StoreOpcode = SystemZ::STFH;
1242 } else if (RC == &SystemZ::GRX32BitRegClass) {
1243 LoadOpcode = SystemZ::LMux;
1244 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001245 } else if (RC == &SystemZ::GR64BitRegClass ||
1246 RC == &SystemZ::ADDR64BitRegClass) {
1247 LoadOpcode = SystemZ::LG;
1248 StoreOpcode = SystemZ::STG;
1249 } else if (RC == &SystemZ::GR128BitRegClass ||
1250 RC == &SystemZ::ADDR128BitRegClass) {
1251 LoadOpcode = SystemZ::L128;
1252 StoreOpcode = SystemZ::ST128;
1253 } else if (RC == &SystemZ::FP32BitRegClass) {
1254 LoadOpcode = SystemZ::LE;
1255 StoreOpcode = SystemZ::STE;
1256 } else if (RC == &SystemZ::FP64BitRegClass) {
1257 LoadOpcode = SystemZ::LD;
1258 StoreOpcode = SystemZ::STD;
1259 } else if (RC == &SystemZ::FP128BitRegClass) {
1260 LoadOpcode = SystemZ::LX;
1261 StoreOpcode = SystemZ::STX;
Ulrich Weigand49506d72015-05-05 19:28:34 +00001262 } else if (RC == &SystemZ::VR32BitRegClass) {
1263 LoadOpcode = SystemZ::VL32;
1264 StoreOpcode = SystemZ::VST32;
1265 } else if (RC == &SystemZ::VR64BitRegClass) {
1266 LoadOpcode = SystemZ::VL64;
1267 StoreOpcode = SystemZ::VST64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001268 } else if (RC == &SystemZ::VF128BitRegClass ||
1269 RC == &SystemZ::VR128BitRegClass) {
1270 LoadOpcode = SystemZ::VL;
1271 StoreOpcode = SystemZ::VST;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001272 } else
1273 llvm_unreachable("Unsupported regclass to load or store");
1274}
1275
1276unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1277 int64_t Offset) const {
1278 const MCInstrDesc &MCID = get(Opcode);
1279 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1280 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1281 // Get the instruction to use for unsigned 12-bit displacements.
1282 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1283 if (Disp12Opcode >= 0)
1284 return Disp12Opcode;
1285
1286 // All address-related instructions can use unsigned 12-bit
1287 // displacements.
1288 return Opcode;
1289 }
1290 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1291 // Get the instruction to use for signed 20-bit displacements.
1292 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1293 if (Disp20Opcode >= 0)
1294 return Disp20Opcode;
1295
1296 // Check whether Opcode allows signed 20-bit displacements.
1297 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1298 return Opcode;
1299 }
1300 return 0;
1301}
1302
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001303unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1304 switch (Opcode) {
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001305 case SystemZ::L: return SystemZ::LT;
1306 case SystemZ::LY: return SystemZ::LT;
1307 case SystemZ::LG: return SystemZ::LTG;
1308 case SystemZ::LGF: return SystemZ::LTGF;
1309 case SystemZ::LR: return SystemZ::LTR;
1310 case SystemZ::LGFR: return SystemZ::LTGFR;
1311 case SystemZ::LGR: return SystemZ::LTGR;
1312 case SystemZ::LER: return SystemZ::LTEBR;
1313 case SystemZ::LDR: return SystemZ::LTDBR;
1314 case SystemZ::LXR: return SystemZ::LTXBR;
Jonas Paulsson12629322015-10-01 18:12:28 +00001315 case SystemZ::LCDFR: return SystemZ::LCDBR;
1316 case SystemZ::LPDFR: return SystemZ::LPDBR;
1317 case SystemZ::LNDFR: return SystemZ::LNDBR;
1318 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1319 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1320 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001321 // On zEC12 we prefer to use RISBGN. But if there is a chance to
1322 // actually use the condition code, we may turn it back into RISGB.
1323 // Note that RISBG is not really a "load-and-test" instruction,
1324 // but sets the same condition code values, so is OK to use here.
1325 case SystemZ::RISBGN: return SystemZ::RISBG;
1326 default: return 0;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001327 }
1328}
1329
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001330// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1331// have already been filtered out. Store the first set bit in LSB and
1332// the number of set bits in Length if so.
1333static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1334 unsigned First = findFirstSet(Mask);
1335 uint64_t Top = (Mask >> First) + 1;
1336 if ((Top & -Top) == Top) {
1337 LSB = First;
1338 Length = findFirstSet(Top);
1339 return true;
1340 }
1341 return false;
1342}
1343
1344bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1345 unsigned &Start, unsigned &End) const {
1346 // Reject trivial all-zero masks.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001347 Mask &= allOnes(BitSize);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001348 if (Mask == 0)
1349 return false;
1350
1351 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1352 // the msb and End specifies the index of the lsb.
1353 unsigned LSB, Length;
1354 if (isStringOfOnes(Mask, LSB, Length)) {
1355 Start = 63 - (LSB + Length - 1);
1356 End = 63 - LSB;
1357 return true;
1358 }
1359
1360 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1361 // of the low 1s and End specifies the lsb of the high 1s.
1362 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1363 assert(LSB > 0 && "Bottom bit must be set");
1364 assert(LSB + Length < BitSize && "Top bit must be set");
1365 Start = 63 - (LSB - 1);
1366 End = 63 - (LSB + Length);
1367 return true;
1368 }
1369
1370 return false;
1371}
1372
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001373unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode,
1374 SystemZII::FusedCompareType Type,
1375 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001376 switch (Opcode) {
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001377 case SystemZ::CHI:
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001378 case SystemZ::CGHI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001379 if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
1380 return 0;
1381 break;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001382 case SystemZ::CLFI:
Richard Sandiford93183ee2013-09-18 09:56:40 +00001383 case SystemZ::CLGFI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001384 if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
1385 return 0;
1386 }
1387 switch (Type) {
1388 case SystemZII::CompareAndBranch:
1389 switch (Opcode) {
1390 case SystemZ::CR:
1391 return SystemZ::CRJ;
1392 case SystemZ::CGR:
1393 return SystemZ::CGRJ;
1394 case SystemZ::CHI:
1395 return SystemZ::CIJ;
1396 case SystemZ::CGHI:
1397 return SystemZ::CGIJ;
1398 case SystemZ::CLR:
1399 return SystemZ::CLRJ;
1400 case SystemZ::CLGR:
1401 return SystemZ::CLGRJ;
1402 case SystemZ::CLFI:
1403 return SystemZ::CLIJ;
1404 case SystemZ::CLGFI:
1405 return SystemZ::CLGIJ;
1406 default:
1407 return 0;
1408 }
1409 case SystemZII::CompareAndReturn:
1410 switch (Opcode) {
1411 case SystemZ::CR:
1412 return SystemZ::CRBReturn;
1413 case SystemZ::CGR:
1414 return SystemZ::CGRBReturn;
1415 case SystemZ::CHI:
1416 return SystemZ::CIBReturn;
1417 case SystemZ::CGHI:
1418 return SystemZ::CGIBReturn;
1419 case SystemZ::CLR:
1420 return SystemZ::CLRBReturn;
1421 case SystemZ::CLGR:
1422 return SystemZ::CLGRBReturn;
1423 case SystemZ::CLFI:
1424 return SystemZ::CLIBReturn;
1425 case SystemZ::CLGFI:
1426 return SystemZ::CLGIBReturn;
1427 default:
1428 return 0;
1429 }
Ulrich Weigand848a5132016-04-11 12:12:32 +00001430 case SystemZII::CompareAndSibcall:
1431 switch (Opcode) {
1432 case SystemZ::CR:
1433 return SystemZ::CRBCall;
1434 case SystemZ::CGR:
1435 return SystemZ::CGRBCall;
1436 case SystemZ::CHI:
1437 return SystemZ::CIBCall;
1438 case SystemZ::CGHI:
1439 return SystemZ::CGIBCall;
1440 case SystemZ::CLR:
1441 return SystemZ::CLRBCall;
1442 case SystemZ::CLGR:
1443 return SystemZ::CLGRBCall;
1444 case SystemZ::CLFI:
1445 return SystemZ::CLIBCall;
1446 case SystemZ::CLGFI:
1447 return SystemZ::CLGIBCall;
1448 default:
1449 return 0;
1450 }
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001451 case SystemZII::CompareAndTrap:
1452 switch (Opcode) {
1453 case SystemZ::CR:
1454 return SystemZ::CRT;
1455 case SystemZ::CGR:
1456 return SystemZ::CGRT;
1457 case SystemZ::CHI:
1458 return SystemZ::CIT;
1459 case SystemZ::CGHI:
1460 return SystemZ::CGIT;
1461 case SystemZ::CLR:
1462 return SystemZ::CLRT;
1463 case SystemZ::CLGR:
1464 return SystemZ::CLGRT;
1465 case SystemZ::CLFI:
1466 return SystemZ::CLFIT;
1467 case SystemZ::CLGFI:
1468 return SystemZ::CLGIT;
1469 default:
1470 return 0;
1471 }
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001472 }
Ulrich Weigand79391ee2016-04-07 16:33:25 +00001473 return 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001474}
1475
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001476void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1477 MachineBasicBlock::iterator MBBI,
1478 unsigned Reg, uint64_t Value) const {
1479 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1480 unsigned Opcode;
1481 if (isInt<16>(Value))
1482 Opcode = SystemZ::LGHI;
1483 else if (SystemZ::isImmLL(Value))
1484 Opcode = SystemZ::LLILL;
1485 else if (SystemZ::isImmLH(Value)) {
1486 Opcode = SystemZ::LLILH;
1487 Value >>= 16;
1488 } else {
1489 assert(isInt<32>(Value) && "Huge values not handled yet");
1490 Opcode = SystemZ::LGFI;
1491 }
1492 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1493}