blob: 3938db256e792666228643cc15ccbed97a0cc8d5 [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"
Richard Sandifordf6bae1e2013-07-02 15:28:56 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000019
Chandler Carruthd174b722014-04-22 02:03:14 +000020using namespace llvm;
21
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000022#define GET_INSTRINFO_CTOR_DTOR
Ulrich Weigand5f613df2013-05-06 16:15:19 +000023#define GET_INSTRMAP_INFO
24#include "SystemZGenInstrInfo.inc"
25
Richard Sandiford6a06ba32013-07-31 11:36:35 +000026// Return a mask with Count low bits set.
27static uint64_t allOnes(unsigned int Count) {
28 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
29}
30
Richard Sandiford0755c932013-10-01 11:26:28 +000031// Reg should be a 32-bit GPR. Return true if it is a high register rather
32// than a low register.
33static bool isHighReg(unsigned int Reg) {
34 if (SystemZ::GRH32BitRegClass.contains(Reg))
35 return true;
36 assert(SystemZ::GR32BitRegClass.contains(Reg) && "Invalid GRX32");
37 return false;
38}
39
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000040// Pin the vtable to this file.
41void SystemZInstrInfo::anchor() {}
42
Eric Christopher673b3af2014-06-27 07:01:17 +000043SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti)
Ulrich Weigand5f613df2013-05-06 16:15:19 +000044 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
Eric Christopher673b3af2014-06-27 07:01:17 +000045 RI(), STI(sti) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000046}
47
48// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
49// each having the opcode given by NewOpcode.
50void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
51 unsigned NewOpcode) const {
52 MachineBasicBlock *MBB = MI->getParent();
53 MachineFunction &MF = *MBB->getParent();
54
55 // Get two load or store instructions. Use the original instruction for one
Alp Tokercb402912014-01-24 17:20:08 +000056 // of them (arbitrarily the second here) and create a clone for the other.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000057 MachineInstr *EarlierMI = MF.CloneMachineInstr(MI);
58 MBB->insert(MI, EarlierMI);
59
60 // Set up the two 64-bit registers.
61 MachineOperand &HighRegOp = EarlierMI->getOperand(0);
62 MachineOperand &LowRegOp = MI->getOperand(0);
Richard Sandiford87a44362013-09-30 10:28:35 +000063 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64));
64 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64));
Ulrich Weigand5f613df2013-05-06 16:15:19 +000065
66 // The address in the first (high) instruction is already correct.
67 // Adjust the offset in the second (low) instruction.
68 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
69 MachineOperand &LowOffsetOp = MI->getOperand(2);
70 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
71
Jonas Paulsson2ba31522016-03-31 08:00:14 +000072 // Clear the kill flags for the base and index registers in the first
73 // instruction.
Jonas Paulsson63a2b682015-10-10 07:14:24 +000074 EarlierMI->getOperand(1).setIsKill(false);
Jonas Paulsson7da38202015-10-26 15:03:41 +000075 EarlierMI->getOperand(3).setIsKill(false);
Jonas Paulsson63a2b682015-10-10 07:14:24 +000076
Ulrich Weigand5f613df2013-05-06 16:15:19 +000077 // Set the opcodes.
78 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
79 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
80 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
81
82 EarlierMI->setDesc(get(HighOpcode));
83 MI->setDesc(get(LowOpcode));
84}
85
86// Split ADJDYNALLOC instruction MI.
87void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
88 MachineBasicBlock *MBB = MI->getParent();
89 MachineFunction &MF = *MBB->getParent();
90 MachineFrameInfo *MFFrame = MF.getFrameInfo();
91 MachineOperand &OffsetMO = MI->getOperand(2);
92
93 uint64_t Offset = (MFFrame->getMaxCallFrameSize() +
94 SystemZMC::CallFrameSize +
95 OffsetMO.getImm());
96 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
97 assert(NewOpcode && "No support for huge argument lists yet");
98 MI->setDesc(get(NewOpcode));
99 OffsetMO.setImm(Offset);
100}
101
Richard Sandiford01240232013-10-01 13:02:28 +0000102// MI is an RI-style pseudo instruction. Replace it with LowOpcode
103// if the first operand is a low GR32 and HighOpcode if the first operand
104// is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
105// and HighOpcode takes an unsigned 32-bit operand. In those cases,
106// MI has the same kind of operand as LowOpcode, so needs to be converted
107// if HighOpcode is used.
108void SystemZInstrInfo::expandRIPseudo(MachineInstr *MI, unsigned LowOpcode,
109 unsigned HighOpcode,
110 bool ConvertHigh) const {
111 unsigned Reg = MI->getOperand(0).getReg();
112 bool IsHigh = isHighReg(Reg);
113 MI->setDesc(get(IsHigh ? HighOpcode : LowOpcode));
114 if (IsHigh && ConvertHigh)
115 MI->getOperand(1).setImm(uint32_t(MI->getOperand(1).getImm()));
116}
117
Richard Sandiford42a694f2013-10-01 14:53:46 +0000118// MI is a three-operand RIE-style pseudo instruction. Replace it with
Jonas Paulsson18d877f2015-10-09 07:19:16 +0000119// LowOpcodeK if the registers are both low GR32s, otherwise use a move
Richard Sandiford42a694f2013-10-01 14:53:46 +0000120// followed by HighOpcode or LowOpcode, depending on whether the target
121// is a high or low GR32.
122void SystemZInstrInfo::expandRIEPseudo(MachineInstr *MI, unsigned LowOpcode,
123 unsigned LowOpcodeK,
124 unsigned HighOpcode) const {
125 unsigned DestReg = MI->getOperand(0).getReg();
126 unsigned SrcReg = MI->getOperand(1).getReg();
127 bool DestIsHigh = isHighReg(DestReg);
128 bool SrcIsHigh = isHighReg(SrcReg);
129 if (!DestIsHigh && !SrcIsHigh)
130 MI->setDesc(get(LowOpcodeK));
131 else {
132 emitGRX32Move(*MI->getParent(), MI, MI->getDebugLoc(),
133 DestReg, SrcReg, SystemZ::LR, 32,
134 MI->getOperand(1).isKill());
135 MI->setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
136 MI->getOperand(1).setReg(DestReg);
Jonas Paulsson18d877f2015-10-09 07:19:16 +0000137 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.
144void SystemZInstrInfo::expandRXYPseudo(MachineInstr *MI, unsigned LowOpcode,
145 unsigned HighOpcode) const {
146 unsigned Reg = MI->getOperand(0).getReg();
147 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
148 MI->getOperand(2).getImm());
149 MI->setDesc(get(Opcode));
150}
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.
155void SystemZInstrInfo::expandZExtPseudo(MachineInstr *MI, unsigned LowOpcode,
156 unsigned Size) const {
157 emitGRX32Move(*MI->getParent(), MI, MI->getDebugLoc(),
158 MI->getOperand(0).getReg(), MI->getOperand(1).getReg(),
159 LowOpcode, Size, MI->getOperand(1).isKill());
160 MI->eraseFromParent();
161}
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,
201 DebugLoc DL, unsigned DestReg,
202 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.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000230static int isSimpleMove(const MachineInstr *MI, int &FrameIndex,
231 unsigned Flag) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000232 const MCInstrDesc &MCID = MI->getDesc();
233 if ((MCID.TSFlags & Flag) &&
234 MI->getOperand(1).isFI() &&
235 MI->getOperand(2).getImm() == 0 &&
236 MI->getOperand(3).getReg() == 0) {
237 FrameIndex = MI->getOperand(1).getIndex();
238 return MI->getOperand(0).getReg();
239 }
240 return 0;
241}
242
243unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
244 int &FrameIndex) const {
245 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
246}
247
248unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
249 int &FrameIndex) const {
250 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
251}
252
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000253bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr *MI,
254 int &DestFrameIndex,
255 int &SrcFrameIndex) const {
256 // Check for MVC 0(Length,FI1),0(FI2)
257 const MachineFrameInfo *MFI = MI->getParent()->getParent()->getFrameInfo();
258 if (MI->getOpcode() != SystemZ::MVC ||
259 !MI->getOperand(0).isFI() ||
260 MI->getOperand(1).getImm() != 0 ||
261 !MI->getOperand(3).isFI() ||
262 MI->getOperand(4).getImm() != 0)
263 return false;
264
265 // Check that Length covers the full slots.
266 int64_t Length = MI->getOperand(2).getImm();
267 unsigned FI1 = MI->getOperand(0).getIndex();
268 unsigned FI2 = MI->getOperand(3).getIndex();
269 if (MFI->getObjectSize(FI1) != Length ||
270 MFI->getObjectSize(FI2) != Length)
271 return false;
272
273 DestFrameIndex = FI1;
274 SrcFrameIndex = FI2;
275 return true;
276}
277
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000278bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
279 MachineBasicBlock *&TBB,
280 MachineBasicBlock *&FBB,
281 SmallVectorImpl<MachineOperand> &Cond,
282 bool AllowModify) const {
283 // Most of the code and comments here are boilerplate.
284
285 // Start from the bottom of the block and work up, examining the
286 // terminator instructions.
287 MachineBasicBlock::iterator I = MBB.end();
288 while (I != MBB.begin()) {
289 --I;
290 if (I->isDebugValue())
291 continue;
292
293 // Working from the bottom, when we see a non-terminator instruction, we're
294 // done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000295 if (!isUnpredicatedTerminator(*I))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000296 break;
297
298 // A terminator that isn't a branch can't easily be handled by this
299 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000300 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000301 return true;
302
303 // Can't handle indirect branches.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000304 SystemZII::Branch Branch(getBranchInfo(I));
305 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000306 return true;
307
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000308 // Punt on compound branches.
309 if (Branch.Type != SystemZII::BranchNormal)
310 return true;
311
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000312 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000313 // Handle unconditional branches.
314 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000315 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000316 continue;
317 }
318
319 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000320 while (std::next(I) != MBB.end())
321 std::next(I)->eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000322
323 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000324 FBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000325
326 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000327 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000328 TBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000329 I->eraseFromParent();
330 I = MBB.end();
331 continue;
332 }
333
334 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000335 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000336 continue;
337 }
338
339 // Working from the bottom, handle the first conditional branch.
340 if (Cond.empty()) {
341 // FIXME: add X86-style branch swap
342 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000343 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000344 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000345 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000346 continue;
347 }
348
349 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000350 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000351
352 // Only handle the case where all conditional branches branch to the same
353 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000354 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000355 return true;
356
357 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000358 unsigned OldCCValid = Cond[0].getImm();
359 unsigned OldCCMask = Cond[1].getImm();
360 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000361 continue;
362
363 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000364 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000365 }
366
367 return false;
368}
369
370unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
371 // Most of the code and comments here are boilerplate.
372 MachineBasicBlock::iterator I = MBB.end();
373 unsigned Count = 0;
374
375 while (I != MBB.begin()) {
376 --I;
377 if (I->isDebugValue())
378 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000379 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000380 break;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000381 if (!getBranchInfo(I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000382 break;
383 // Remove the branch.
384 I->eraseFromParent();
385 I = MBB.end();
386 ++Count;
387 }
388
389 return Count;
390}
391
Richard Sandiford3d768e32013-07-31 12:30:20 +0000392bool SystemZInstrInfo::
393ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
394 assert(Cond.size() == 2 && "Invalid condition");
395 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
396 return false;
397}
398
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000399unsigned
400SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
401 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000402 ArrayRef<MachineOperand> Cond,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000403 DebugLoc DL) const {
404 // In this function we output 32-bit branches, which should always
405 // have enough range. They can be shortened and relaxed by later code
406 // in the pipeline, if desired.
407
408 // Shouldn't be a fall through.
409 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000410 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000411 "SystemZ branch conditions have one component!");
412
413 if (Cond.empty()) {
414 // Unconditional branch?
415 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000416 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000417 return 1;
418 }
419
420 // Conditional branch.
421 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000422 unsigned CCValid = Cond[0].getImm();
423 unsigned CCMask = Cond[1].getImm();
424 BuildMI(&MBB, DL, get(SystemZ::BRC))
425 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000426 ++Count;
427
428 if (FBB) {
429 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000430 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000431 ++Count;
432 }
433 return Count;
434}
435
Richard Sandiford564681c2013-08-12 10:28:10 +0000436bool SystemZInstrInfo::analyzeCompare(const MachineInstr *MI,
437 unsigned &SrcReg, unsigned &SrcReg2,
438 int &Mask, int &Value) const {
439 assert(MI->isCompare() && "Caller should have checked for a comparison");
440
441 if (MI->getNumExplicitOperands() == 2 &&
442 MI->getOperand(0).isReg() &&
443 MI->getOperand(1).isImm()) {
444 SrcReg = MI->getOperand(0).getReg();
445 SrcReg2 = 0;
446 Value = MI->getOperand(1).getImm();
447 Mask = ~0;
448 return true;
449 }
450
451 return false;
452}
453
Richard Sandiforda5901252013-08-16 10:22:54 +0000454// If Reg is a virtual register, return its definition, otherwise return null.
455static MachineInstr *getDef(unsigned Reg,
456 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000457 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +0000458 return nullptr;
Richard Sandiford564681c2013-08-12 10:28:10 +0000459 return MRI->getUniqueVRegDef(Reg);
460}
461
462// Return true if MI is a shift of type Opcode by Imm bits.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000463static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000464 return (MI->getOpcode() == Opcode &&
465 !MI->getOperand(2).getReg() &&
466 MI->getOperand(3).getImm() == Imm);
467}
468
Richard Sandiforda5901252013-08-16 10:22:54 +0000469// If the destination of MI has no uses, delete it as dead.
470static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
471 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
472 MI->eraseFromParent();
473}
474
Richard Sandiford564681c2013-08-12 10:28:10 +0000475// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000476// the result of an IPM sequence whose input CC survives until Compare,
477// and whether Compare is therefore redundant. Delete it and return
478// true if so.
479static bool removeIPMBasedCompare(MachineInstr *Compare, unsigned SrcReg,
480 const MachineRegisterInfo *MRI,
481 const TargetRegisterInfo *TRI) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000482 MachineInstr *LGFR = nullptr;
Richard Sandiforda5901252013-08-16 10:22:54 +0000483 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000484 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
485 LGFR = RLL;
486 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
487 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000488 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000489 return false;
490
Richard Sandiforda5901252013-08-16 10:22:54 +0000491 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000492 if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
Richard Sandiford564681c2013-08-12 10:28:10 +0000493 return false;
494
Richard Sandiforda5901252013-08-16 10:22:54 +0000495 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000496 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
497 return false;
498
499 // Check that there are no assignments to CC between the IPM and Compare,
Richard Sandiford564681c2013-08-12 10:28:10 +0000500 if (IPM->getParent() != Compare->getParent())
501 return false;
502 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare;
503 for (++MBBI; MBBI != MBBE; ++MBBI) {
504 MachineInstr *MI = MBBI;
Richard Sandiforda5901252013-08-16 10:22:54 +0000505 if (MI->modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000506 return false;
507 }
508
Richard Sandiford564681c2013-08-12 10:28:10 +0000509 Compare->eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000510 if (LGFR)
511 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000512 eraseIfDead(RLL, MRI);
513 eraseIfDead(SRL, MRI);
514 eraseIfDead(IPM, MRI);
515
Richard Sandiford564681c2013-08-12 10:28:10 +0000516 return true;
517}
518
519bool
520SystemZInstrInfo::optimizeCompareInstr(MachineInstr *Compare,
521 unsigned SrcReg, unsigned SrcReg2,
522 int Mask, int Value,
523 const MachineRegisterInfo *MRI) const {
524 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
525 bool IsLogical = (Compare->getDesc().TSFlags & SystemZII::IsLogical) != 0;
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000526 return Value == 0 && !IsLogical &&
527 removeIPMBasedCompare(Compare, SrcReg, MRI, &RI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000528}
529
Richard Sandifordf2404162013-07-25 09:11:15 +0000530// If Opcode is a move that has a conditional variant, return that variant,
531// otherwise return 0.
532static unsigned getConditionalMove(unsigned Opcode) {
533 switch (Opcode) {
534 case SystemZ::LR: return SystemZ::LOCR;
535 case SystemZ::LGR: return SystemZ::LOCGR;
536 default: return 0;
537 }
538}
539
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000540bool SystemZInstrInfo::isPredicable(MachineInstr &MI) const {
541 unsigned Opcode = MI.getOpcode();
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000542 if (STI.hasLoadStoreOnCond() && getConditionalMove(Opcode))
543 return true;
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000544 if (Opcode == SystemZ::Return ||
Ulrich Weigand848a5132016-04-11 12:12:32 +0000545 Opcode == SystemZ::CallJG ||
546 Opcode == SystemZ::CallBR)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000547 return true;
548 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000549}
550
551bool SystemZInstrInfo::
552isProfitableToIfCvt(MachineBasicBlock &MBB,
553 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000554 BranchProbability Probability) const {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000555 // Avoid using conditional returns at the end of a loop (since then
556 // we'd need to emit an unconditional branch to the beginning anyway,
557 // making the loop body longer). This doesn't apply for low-probability
558 // loops (eg. compare-and-swap retry), so just decide based on branch
559 // probability instead of looping structure.
560 if (MBB.succ_empty() && Probability < BranchProbability(1, 8))
561 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000562 // For now only convert single instructions.
563 return NumCycles == 1;
564}
565
566bool SystemZInstrInfo::
567isProfitableToIfCvt(MachineBasicBlock &TMBB,
568 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
569 MachineBasicBlock &FMBB,
570 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000571 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000572 // For now avoid converting mutually-exclusive cases.
573 return false;
574}
575
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000576bool SystemZInstrInfo::
577isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
578 BranchProbability Probability) const {
579 // For now only duplicate single instructions.
580 return NumCycles == 1;
581}
582
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000583bool SystemZInstrInfo::PredicateInstruction(
584 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000585 assert(Pred.size() == 2 && "Invalid condition");
586 unsigned CCValid = Pred[0].getImm();
587 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000588 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000589 unsigned Opcode = MI.getOpcode();
Eric Christopher673b3af2014-06-27 07:01:17 +0000590 if (STI.hasLoadStoreOnCond()) {
Richard Sandifordf2404162013-07-25 09:11:15 +0000591 if (unsigned CondOpcode = getConditionalMove(Opcode)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000592 MI.setDesc(get(CondOpcode));
593 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
594 .addImm(CCValid)
595 .addImm(CCMask)
596 .addReg(SystemZ::CC, RegState::Implicit);
Richard Sandifordf2404162013-07-25 09:11:15 +0000597 return true;
598 }
599 }
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000600 if (Opcode == SystemZ::Return) {
601 MI.setDesc(get(SystemZ::CondReturn));
602 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
603 .addImm(CCValid).addImm(CCMask)
604 .addReg(SystemZ::CC, RegState::Implicit);
605 return true;
606 }
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000607 if (Opcode == SystemZ::CallJG) {
608 const GlobalValue *Global = MI.getOperand(0).getGlobal();
609 const uint32_t *RegMask = MI.getOperand(1).getRegMask();
610 MI.RemoveOperand(1);
611 MI.RemoveOperand(0);
612 MI.setDesc(get(SystemZ::CallBRCL));
613 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
614 .addImm(CCValid).addImm(CCMask)
615 .addGlobalAddress(Global)
616 .addRegMask(RegMask)
617 .addReg(SystemZ::CC, RegState::Implicit);
618 return true;
619 }
Ulrich Weigand848a5132016-04-11 12:12:32 +0000620 if (Opcode == SystemZ::CallBR) {
621 const uint32_t *RegMask = MI.getOperand(0).getRegMask();
622 MI.RemoveOperand(0);
623 MI.setDesc(get(SystemZ::CallBCR));
624 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
625 .addImm(CCValid).addImm(CCMask)
626 .addRegMask(RegMask)
627 .addReg(SystemZ::CC, RegState::Implicit);
628 return true;
629 }
Richard Sandifordf2404162013-07-25 09:11:15 +0000630 return false;
631}
632
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000633void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
634 MachineBasicBlock::iterator MBBI,
635 DebugLoc DL, unsigned DestReg,
636 unsigned SrcReg, bool KillSrc) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000637 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
638 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000639 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
640 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
641 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
642 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000643 return;
644 }
645
Richard Sandiford0755c932013-10-01 11:26:28 +0000646 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
647 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
648 return;
649 }
650
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000651 // Everything else needs only one instruction.
652 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000653 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000654 Opcode = SystemZ::LGR;
655 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigandcdce0262016-03-14 13:50:03 +0000656 // For z13 we prefer LDR over LER to avoid partial register dependencies.
657 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000658 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
659 Opcode = SystemZ::LDR;
660 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
661 Opcode = SystemZ::LXR;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000662 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
663 Opcode = SystemZ::VLR32;
664 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
665 Opcode = SystemZ::VLR64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000666 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
667 Opcode = SystemZ::VLR;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000668 else
669 llvm_unreachable("Impossible reg-to-reg copy");
670
671 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
672 .addReg(SrcReg, getKillRegState(KillSrc));
673}
674
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000675void SystemZInstrInfo::storeRegToStackSlot(
676 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
677 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
678 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000679 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
680
681 // Callers may expect a single instruction, so keep 128-bit moves
682 // together for now and lower them after register allocation.
683 unsigned LoadOpcode, StoreOpcode;
684 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
685 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000686 .addReg(SrcReg, getKillRegState(isKill)),
687 FrameIdx);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000688}
689
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000690void SystemZInstrInfo::loadRegFromStackSlot(
691 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
692 int FrameIdx, const TargetRegisterClass *RC,
693 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000694 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
695
696 // Callers may expect a single instruction, so keep 128-bit moves
697 // together for now and lower them after register allocation.
698 unsigned LoadOpcode, StoreOpcode;
699 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
700 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
701 FrameIdx);
702}
703
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000704// Return true if MI is a simple load or store with a 12-bit displacement
705// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
706static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
707 const MCInstrDesc &MCID = MI->getDesc();
708 return ((MCID.TSFlags & Flag) &&
709 isUInt<12>(MI->getOperand(2).getImm()) &&
710 MI->getOperand(3).getReg() == 0);
711}
712
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000713namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +0000714struct LogicOp {
715 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
716 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
717 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000718
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000719 explicit operator bool() const { return RegSize; }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000720
Richard Sandifordc2312692014-03-06 10:38:30 +0000721 unsigned RegSize, ImmLSB, ImmSize;
722};
723} // end anonymous namespace
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000724
725static LogicOp interpretAndImmediate(unsigned Opcode) {
726 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000727 case SystemZ::NILMux: return LogicOp(32, 0, 16);
728 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000729 case SystemZ::NILL64: return LogicOp(64, 0, 16);
730 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000731 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
732 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
733 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000734 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000735 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000736 default: return LogicOp();
737 }
738}
739
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000740static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) {
741 if (OldMI->registerDefIsDead(SystemZ::CC)) {
742 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC);
743 if (CCDef != nullptr)
744 CCDef->setIsDead(true);
745 }
746}
747
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000748// Used to return from convertToThreeAddress after replacing two-address
749// instruction OldMI with three-address instruction NewMI.
750static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
751 MachineInstr *NewMI,
752 LiveVariables *LV) {
753 if (LV) {
754 unsigned NumOps = OldMI->getNumOperands();
755 for (unsigned I = 1; I < NumOps; ++I) {
756 MachineOperand &Op = OldMI->getOperand(I);
757 if (Op.isReg() && Op.isKill())
758 LV->replaceKillInstruction(Op.getReg(), OldMI, NewMI);
759 }
760 }
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000761 transferDeadCC(OldMI, NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000762 return NewMI;
763}
764
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000765MachineInstr *
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000766SystemZInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
767 MachineBasicBlock::iterator &MBBI,
768 LiveVariables *LV) const {
769 MachineInstr *MI = MBBI;
770 MachineBasicBlock *MBB = MI->getParent();
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000771 MachineFunction *MF = MBB->getParent();
772 MachineRegisterInfo &MRI = MF->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000773
774 unsigned Opcode = MI->getOpcode();
775 unsigned NumOps = MI->getNumOperands();
776
777 // Try to convert something like SLL into SLLK, if supported.
778 // We prefer to keep the two-operand form where possible both
779 // because it tends to be shorter and because some instructions
780 // have memory forms that can be used during spilling.
Eric Christopher673b3af2014-06-27 07:01:17 +0000781 if (STI.hasDistinctOps()) {
Richard Sandiford42a694f2013-10-01 14:53:46 +0000782 MachineOperand &Dest = MI->getOperand(0);
783 MachineOperand &Src = MI->getOperand(1);
784 unsigned DestReg = Dest.getReg();
785 unsigned SrcReg = Src.getReg();
786 // AHIMux is only really a three-operand instruction when both operands
787 // are low registers. Try to constrain both operands to be low if
788 // possible.
789 if (Opcode == SystemZ::AHIMux &&
790 TargetRegisterInfo::isVirtualRegister(DestReg) &&
791 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
792 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
793 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
794 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
795 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
796 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000797 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
798 if (ThreeOperandOpcode >= 0) {
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000799 // Create three address instruction without adding the implicit
800 // operands. Those will instead be copied over from the original
801 // instruction by the loop below.
802 MachineInstrBuilder MIB(*MF,
803 MF->CreateMachineInstr(get(ThreeOperandOpcode),
804 MI->getDebugLoc(), /*NoImplicit=*/true));
805 MIB.addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000806 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000807 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000808 // Keep the remaining operands as-is.
809 for (unsigned I = 2; I < NumOps; ++I)
810 MIB.addOperand(MI->getOperand(I));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000811 MBB->insert(MI, MIB);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000812 return finishConvertToThreeAddress(MI, MIB, LV);
813 }
814 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000815
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000816 // Try to convert an AND into an RISBG-type instruction.
817 if (LogicOp And = interpretAndImmediate(Opcode)) {
Richard Sandiford70284282013-10-01 14:20:41 +0000818 uint64_t Imm = MI->getOperand(2).getImm() << And.ImmLSB;
819 // AND IMMEDIATE leaves the other bits of the register unchanged.
820 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
821 unsigned Start, End;
822 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
823 unsigned NewOpcode;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000824 if (And.RegSize == 64) {
Richard Sandiford70284282013-10-01 14:20:41 +0000825 NewOpcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000826 // Prefer RISBGN if available, since it does not clobber CC.
827 if (STI.hasMiscellaneousExtensions())
828 NewOpcode = SystemZ::RISBGN;
829 } else {
Richard Sandiford70284282013-10-01 14:20:41 +0000830 NewOpcode = SystemZ::RISBMux;
831 Start &= 31;
832 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000833 }
Richard Sandiford70284282013-10-01 14:20:41 +0000834 MachineOperand &Dest = MI->getOperand(0);
835 MachineOperand &Src = MI->getOperand(1);
836 MachineInstrBuilder MIB =
837 BuildMI(*MBB, MI, MI->getDebugLoc(), get(NewOpcode))
838 .addOperand(Dest).addReg(0)
839 .addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg())
840 .addImm(Start).addImm(End + 128).addImm(0);
841 return finishConvertToThreeAddress(MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000842 }
843 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000844 return nullptr;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000845}
846
Keno Fischere70b31f2015-06-08 20:09:58 +0000847MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
848 MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
849 MachineBasicBlock::iterator InsertPt, int FrameIndex) const {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000850 const MachineFrameInfo *MFI = MF.getFrameInfo();
851 unsigned Size = MFI->getObjectSize(FrameIndex);
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000852 unsigned Opcode = MI->getOpcode();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000853
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000854// XXX This is an introduction of a CC def and is illegal! Reactivate
855// with a check of liveness of CC reg.
856#if 0
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000857 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
858 if ((Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
859 isInt<8>(MI->getOperand(2).getImm()) &&
860 !MI->getOperand(3).getReg()) {
861 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000862 MachineInstr *BuiltMI =
863 BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000864 get(SystemZ::AGSI))
865 .addFrameIndex(FrameIndex)
866 .addImm(0)
867 .addImm(MI->getOperand(2).getImm());
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000868 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true);
869 return BuiltMI;
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000870 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000871 return nullptr;
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000872 }
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000873#endif
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000874
875 // All other cases require a single operand.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000876 if (Ops.size() != 1)
Craig Topper062a2ba2014-04-25 05:30:21 +0000877 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000878
879 unsigned OpNum = Ops[0];
NAKAMURA Takumiddcba562013-07-03 02:20:49 +0000880 assert(Size == MF.getRegInfo()
881 .getRegClass(MI->getOperand(OpNum).getReg())->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000882 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000883
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000884 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) &&
885 OpNum == 0 &&
886 isInt<8>(MI->getOperand(2).getImm())) {
887 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
888 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000889 MachineInstr *BuiltMI =
890 BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000891 get(Opcode))
892 .addFrameIndex(FrameIndex)
893 .addImm(0)
894 .addImm(MI->getOperand(2).getImm());
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000895 transferDeadCC(MI, BuiltMI);
896 return BuiltMI;
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000897 }
898
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000899 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
900 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
901 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
902 // If we're spilling the destination of an LDGR or LGDR, store the
903 // source register instead.
904 if (OpNum == 0) {
905 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
Keno Fischere70b31f2015-06-08 20:09:58 +0000906 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
907 get(StoreOpcode))
908 .addOperand(MI->getOperand(1))
909 .addFrameIndex(FrameIndex)
910 .addImm(0)
911 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000912 }
913 // If we're spilling the source of an LDGR or LGDR, load the
914 // destination register instead.
915 if (OpNum == 1) {
916 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
917 unsigned Dest = MI->getOperand(0).getReg();
Keno Fischere70b31f2015-06-08 20:09:58 +0000918 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
919 get(LoadOpcode), Dest)
920 .addFrameIndex(FrameIndex)
921 .addImm(0)
922 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000923 }
924 }
925
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000926 // Look for cases where the source of a simple store or the destination
927 // of a simple load is being spilled. Try to use MVC instead.
928 //
929 // Although MVC is in practice a fast choice in these cases, it is still
930 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +0000931 // load or store is volatile. We also wouldn't be able to use MVC if
932 // the two memories partially overlap, but that case cannot occur here,
933 // because we know that one of the memories is a full frame index.
934 //
935 // For performance reasons, we also want to avoid using MVC if the addresses
936 // might be equal. We don't worry about that case here, because spill slot
937 // coloring happens later, and because we have special code to remove
938 // MVCs that turn out to be redundant.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000939 if (OpNum == 0 && MI->hasOneMemOperand()) {
940 MachineMemOperand *MMO = *MI->memoperands_begin();
941 if (MMO->getSize() == Size && !MMO->isVolatile()) {
942 // Handle conversion of loads.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000943 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXLoad)) {
Keno Fischere70b31f2015-06-08 20:09:58 +0000944 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
945 get(SystemZ::MVC))
946 .addFrameIndex(FrameIndex)
947 .addImm(0)
948 .addImm(Size)
949 .addOperand(MI->getOperand(1))
950 .addImm(MI->getOperand(2).getImm())
951 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000952 }
953 // Handle conversion of stores.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000954 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXStore)) {
Keno Fischere70b31f2015-06-08 20:09:58 +0000955 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
956 get(SystemZ::MVC))
957 .addOperand(MI->getOperand(1))
958 .addImm(MI->getOperand(2).getImm())
959 .addImm(Size)
960 .addFrameIndex(FrameIndex)
961 .addImm(0)
962 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000963 }
964 }
965 }
966
Richard Sandiforded1fab62013-07-03 10:10:02 +0000967 // If the spilled operand is the final one, try to change <INSN>R
968 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000969 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000970 if (MemOpcode >= 0) {
971 unsigned NumOps = MI->getNumExplicitOperands();
972 if (OpNum == NumOps - 1) {
973 const MCInstrDesc &MemDesc = get(MemOpcode);
974 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
975 assert(AccessBytes != 0 && "Size of access should be known");
976 assert(AccessBytes <= Size && "Access outside the frame index");
977 uint64_t Offset = Size - AccessBytes;
Keno Fischere70b31f2015-06-08 20:09:58 +0000978 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
979 MI->getDebugLoc(), get(MemOpcode));
Richard Sandiforded1fab62013-07-03 10:10:02 +0000980 for (unsigned I = 0; I < OpNum; ++I)
981 MIB.addOperand(MI->getOperand(I));
982 MIB.addFrameIndex(FrameIndex).addImm(Offset);
983 if (MemDesc.TSFlags & SystemZII::HasIndex)
984 MIB.addReg(0);
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000985 transferDeadCC(MI, MIB);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000986 return MIB;
987 }
988 }
989
Craig Topper062a2ba2014-04-25 05:30:21 +0000990 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000991}
992
Keno Fischere70b31f2015-06-08 20:09:58 +0000993MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
994 MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
995 MachineBasicBlock::iterator InsertPt, MachineInstr *LoadMI) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000996 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000997}
998
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000999bool
1000SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1001 switch (MI->getOpcode()) {
1002 case SystemZ::L128:
1003 splitMove(MI, SystemZ::LG);
1004 return true;
1005
1006 case SystemZ::ST128:
1007 splitMove(MI, SystemZ::STG);
1008 return true;
1009
1010 case SystemZ::LX:
1011 splitMove(MI, SystemZ::LD);
1012 return true;
1013
1014 case SystemZ::STX:
1015 splitMove(MI, SystemZ::STD);
1016 return true;
1017
Richard Sandiford89e160d2013-10-01 12:11:47 +00001018 case SystemZ::LBMux:
1019 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
1020 return true;
1021
1022 case SystemZ::LHMux:
1023 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
1024 return true;
1025
Richard Sandiford21235a22013-10-01 12:49:07 +00001026 case SystemZ::LLCRMux:
1027 expandZExtPseudo(MI, SystemZ::LLCR, 8);
1028 return true;
1029
1030 case SystemZ::LLHRMux:
1031 expandZExtPseudo(MI, SystemZ::LLHR, 16);
1032 return true;
1033
Richard Sandiford0d46b1a2013-10-01 12:19:08 +00001034 case SystemZ::LLCMux:
1035 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
1036 return true;
1037
1038 case SystemZ::LLHMux:
1039 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
1040 return true;
1041
Richard Sandiford0755c932013-10-01 11:26:28 +00001042 case SystemZ::LMux:
1043 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
1044 return true;
1045
Richard Sandiford5469c392013-10-01 12:22:49 +00001046 case SystemZ::STCMux:
1047 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
1048 return true;
1049
1050 case SystemZ::STHMux:
1051 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
1052 return true;
1053
Richard Sandiford0755c932013-10-01 11:26:28 +00001054 case SystemZ::STMux:
1055 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
1056 return true;
1057
Richard Sandiford01240232013-10-01 13:02:28 +00001058 case SystemZ::LHIMux:
1059 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
1060 return true;
1061
1062 case SystemZ::IIFMux:
1063 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
1064 return true;
1065
Richard Sandiford1a569312013-10-01 13:18:56 +00001066 case SystemZ::IILMux:
1067 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
1068 return true;
1069
1070 case SystemZ::IIHMux:
1071 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
1072 return true;
1073
Richard Sandiford70284282013-10-01 14:20:41 +00001074 case SystemZ::NIFMux:
1075 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1076 return true;
1077
1078 case SystemZ::NILMux:
1079 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1080 return true;
1081
1082 case SystemZ::NIHMux:
1083 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1084 return true;
1085
Richard Sandiford6e96ac62013-10-01 13:22:41 +00001086 case SystemZ::OIFMux:
1087 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1088 return true;
1089
1090 case SystemZ::OILMux:
1091 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1092 return true;
1093
1094 case SystemZ::OIHMux:
1095 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1096 return true;
1097
Richard Sandiford5718dac2013-10-01 14:08:44 +00001098 case SystemZ::XIFMux:
1099 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1100 return true;
1101
Richard Sandiford2cac7632013-10-01 14:41:52 +00001102 case SystemZ::TMLMux:
1103 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1104 return true;
1105
1106 case SystemZ::TMHMux:
1107 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1108 return true;
1109
Richard Sandiford42a694f2013-10-01 14:53:46 +00001110 case SystemZ::AHIMux:
1111 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1112 return true;
1113
1114 case SystemZ::AHIMuxK:
1115 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1116 return true;
1117
1118 case SystemZ::AFIMux:
1119 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1120 return true;
1121
Richard Sandiforda9ac0e02013-10-01 14:56:23 +00001122 case SystemZ::CFIMux:
1123 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1124 return true;
1125
1126 case SystemZ::CLFIMux:
1127 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1128 return true;
1129
Richard Sandifordb63e3002013-10-01 15:00:44 +00001130 case SystemZ::CMux:
1131 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1132 return true;
1133
1134 case SystemZ::CLMux:
1135 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1136 return true;
1137
Richard Sandiford70284282013-10-01 14:20:41 +00001138 case SystemZ::RISBMux: {
1139 bool DestIsHigh = isHighReg(MI->getOperand(0).getReg());
1140 bool SrcIsHigh = isHighReg(MI->getOperand(2).getReg());
1141 if (SrcIsHigh == DestIsHigh)
1142 MI->setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
1143 else {
1144 MI->setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1145 MI->getOperand(5).setImm(MI->getOperand(5).getImm() ^ 32);
1146 }
1147 return true;
1148 }
1149
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001150 case SystemZ::ADJDYNALLOC:
1151 splitAdjDynAlloc(MI);
1152 return true;
1153
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001154 case TargetOpcode::LOAD_STACK_GUARD:
1155 expandLoadStackGuard(MI);
1156 return true;
1157
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001158 default:
1159 return false;
1160 }
1161}
1162
Richard Sandiford312425f2013-05-20 14:23:08 +00001163uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
1164 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
1165 const MachineFunction *MF = MI->getParent()->getParent();
1166 const char *AsmStr = MI->getOperand(0).getSymbolName();
1167 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1168 }
1169 return MI->getDesc().getSize();
1170}
1171
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001172SystemZII::Branch
1173SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001174 switch (MI->getOpcode()) {
1175 case SystemZ::BR:
1176 case SystemZ::J:
1177 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001178 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Richard Sandiford3d768e32013-07-31 12:30:20 +00001179 SystemZ::CCMASK_ANY, &MI->getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001180
1181 case SystemZ::BRC:
1182 case SystemZ::BRCL:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001183 return SystemZII::Branch(SystemZII::BranchNormal,
Richard Sandiford3d768e32013-07-31 12:30:20 +00001184 MI->getOperand(0).getImm(),
1185 MI->getOperand(1).getImm(), &MI->getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001186
Richard Sandifordc2121252013-08-05 11:23:46 +00001187 case SystemZ::BRCT:
1188 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
1189 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
1190
1191 case SystemZ::BRCTG:
1192 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
1193 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
1194
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001195 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001196 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001197 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
1198 MI->getOperand(2).getImm(), &MI->getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001199
Richard Sandiford93183ee2013-09-18 09:56:40 +00001200 case SystemZ::CLIJ:
1201 case SystemZ::CLRJ:
1202 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
1203 MI->getOperand(2).getImm(), &MI->getOperand(3));
1204
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001205 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001206 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001207 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
1208 MI->getOperand(2).getImm(), &MI->getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001209
Richard Sandiford93183ee2013-09-18 09:56:40 +00001210 case SystemZ::CLGIJ:
1211 case SystemZ::CLGRJ:
1212 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
1213 MI->getOperand(2).getImm(), &MI->getOperand(3));
1214
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001215 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001216 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001217 }
1218}
1219
1220void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1221 unsigned &LoadOpcode,
1222 unsigned &StoreOpcode) const {
1223 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1224 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001225 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001226 } else if (RC == &SystemZ::GRH32BitRegClass) {
1227 LoadOpcode = SystemZ::LFH;
1228 StoreOpcode = SystemZ::STFH;
1229 } else if (RC == &SystemZ::GRX32BitRegClass) {
1230 LoadOpcode = SystemZ::LMux;
1231 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001232 } else if (RC == &SystemZ::GR64BitRegClass ||
1233 RC == &SystemZ::ADDR64BitRegClass) {
1234 LoadOpcode = SystemZ::LG;
1235 StoreOpcode = SystemZ::STG;
1236 } else if (RC == &SystemZ::GR128BitRegClass ||
1237 RC == &SystemZ::ADDR128BitRegClass) {
1238 LoadOpcode = SystemZ::L128;
1239 StoreOpcode = SystemZ::ST128;
1240 } else if (RC == &SystemZ::FP32BitRegClass) {
1241 LoadOpcode = SystemZ::LE;
1242 StoreOpcode = SystemZ::STE;
1243 } else if (RC == &SystemZ::FP64BitRegClass) {
1244 LoadOpcode = SystemZ::LD;
1245 StoreOpcode = SystemZ::STD;
1246 } else if (RC == &SystemZ::FP128BitRegClass) {
1247 LoadOpcode = SystemZ::LX;
1248 StoreOpcode = SystemZ::STX;
Ulrich Weigand49506d72015-05-05 19:28:34 +00001249 } else if (RC == &SystemZ::VR32BitRegClass) {
1250 LoadOpcode = SystemZ::VL32;
1251 StoreOpcode = SystemZ::VST32;
1252 } else if (RC == &SystemZ::VR64BitRegClass) {
1253 LoadOpcode = SystemZ::VL64;
1254 StoreOpcode = SystemZ::VST64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001255 } else if (RC == &SystemZ::VF128BitRegClass ||
1256 RC == &SystemZ::VR128BitRegClass) {
1257 LoadOpcode = SystemZ::VL;
1258 StoreOpcode = SystemZ::VST;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001259 } else
1260 llvm_unreachable("Unsupported regclass to load or store");
1261}
1262
1263unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1264 int64_t Offset) const {
1265 const MCInstrDesc &MCID = get(Opcode);
1266 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1267 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1268 // Get the instruction to use for unsigned 12-bit displacements.
1269 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1270 if (Disp12Opcode >= 0)
1271 return Disp12Opcode;
1272
1273 // All address-related instructions can use unsigned 12-bit
1274 // displacements.
1275 return Opcode;
1276 }
1277 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1278 // Get the instruction to use for signed 20-bit displacements.
1279 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1280 if (Disp20Opcode >= 0)
1281 return Disp20Opcode;
1282
1283 // Check whether Opcode allows signed 20-bit displacements.
1284 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1285 return Opcode;
1286 }
1287 return 0;
1288}
1289
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001290unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1291 switch (Opcode) {
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001292 case SystemZ::L: return SystemZ::LT;
1293 case SystemZ::LY: return SystemZ::LT;
1294 case SystemZ::LG: return SystemZ::LTG;
1295 case SystemZ::LGF: return SystemZ::LTGF;
1296 case SystemZ::LR: return SystemZ::LTR;
1297 case SystemZ::LGFR: return SystemZ::LTGFR;
1298 case SystemZ::LGR: return SystemZ::LTGR;
1299 case SystemZ::LER: return SystemZ::LTEBR;
1300 case SystemZ::LDR: return SystemZ::LTDBR;
1301 case SystemZ::LXR: return SystemZ::LTXBR;
Jonas Paulsson12629322015-10-01 18:12:28 +00001302 case SystemZ::LCDFR: return SystemZ::LCDBR;
1303 case SystemZ::LPDFR: return SystemZ::LPDBR;
1304 case SystemZ::LNDFR: return SystemZ::LNDBR;
1305 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1306 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1307 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001308 // On zEC12 we prefer to use RISBGN. But if there is a chance to
1309 // actually use the condition code, we may turn it back into RISGB.
1310 // Note that RISBG is not really a "load-and-test" instruction,
1311 // but sets the same condition code values, so is OK to use here.
1312 case SystemZ::RISBGN: return SystemZ::RISBG;
1313 default: return 0;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001314 }
1315}
1316
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001317// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1318// have already been filtered out. Store the first set bit in LSB and
1319// the number of set bits in Length if so.
1320static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1321 unsigned First = findFirstSet(Mask);
1322 uint64_t Top = (Mask >> First) + 1;
1323 if ((Top & -Top) == Top) {
1324 LSB = First;
1325 Length = findFirstSet(Top);
1326 return true;
1327 }
1328 return false;
1329}
1330
1331bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1332 unsigned &Start, unsigned &End) const {
1333 // Reject trivial all-zero masks.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001334 Mask &= allOnes(BitSize);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001335 if (Mask == 0)
1336 return false;
1337
1338 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1339 // the msb and End specifies the index of the lsb.
1340 unsigned LSB, Length;
1341 if (isStringOfOnes(Mask, LSB, Length)) {
1342 Start = 63 - (LSB + Length - 1);
1343 End = 63 - LSB;
1344 return true;
1345 }
1346
1347 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1348 // of the low 1s and End specifies the lsb of the high 1s.
1349 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1350 assert(LSB > 0 && "Bottom bit must be set");
1351 assert(LSB + Length < BitSize && "Top bit must be set");
1352 Start = 63 - (LSB - 1);
1353 End = 63 - (LSB + Length);
1354 return true;
1355 }
1356
1357 return false;
1358}
1359
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001360unsigned SystemZInstrInfo::getCompareAndBranch(unsigned Opcode,
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001361 SystemZII::CompareAndBranchType Type,
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001362 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001363 switch (Opcode) {
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001364 case SystemZ::CHI:
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001365 case SystemZ::CGHI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001366 if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
1367 return 0;
1368 break;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001369 case SystemZ::CLFI:
Richard Sandiford93183ee2013-09-18 09:56:40 +00001370 case SystemZ::CLGFI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001371 if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
1372 return 0;
1373 }
1374 switch (Type) {
1375 case SystemZII::CompareAndBranch:
1376 switch (Opcode) {
1377 case SystemZ::CR:
1378 return SystemZ::CRJ;
1379 case SystemZ::CGR:
1380 return SystemZ::CGRJ;
1381 case SystemZ::CHI:
1382 return SystemZ::CIJ;
1383 case SystemZ::CGHI:
1384 return SystemZ::CGIJ;
1385 case SystemZ::CLR:
1386 return SystemZ::CLRJ;
1387 case SystemZ::CLGR:
1388 return SystemZ::CLGRJ;
1389 case SystemZ::CLFI:
1390 return SystemZ::CLIJ;
1391 case SystemZ::CLGFI:
1392 return SystemZ::CLGIJ;
1393 default:
1394 return 0;
1395 }
1396 case SystemZII::CompareAndReturn:
1397 switch (Opcode) {
1398 case SystemZ::CR:
1399 return SystemZ::CRBReturn;
1400 case SystemZ::CGR:
1401 return SystemZ::CGRBReturn;
1402 case SystemZ::CHI:
1403 return SystemZ::CIBReturn;
1404 case SystemZ::CGHI:
1405 return SystemZ::CGIBReturn;
1406 case SystemZ::CLR:
1407 return SystemZ::CLRBReturn;
1408 case SystemZ::CLGR:
1409 return SystemZ::CLGRBReturn;
1410 case SystemZ::CLFI:
1411 return SystemZ::CLIBReturn;
1412 case SystemZ::CLGFI:
1413 return SystemZ::CLGIBReturn;
1414 default:
1415 return 0;
1416 }
Ulrich Weigand848a5132016-04-11 12:12:32 +00001417 case SystemZII::CompareAndSibcall:
1418 switch (Opcode) {
1419 case SystemZ::CR:
1420 return SystemZ::CRBCall;
1421 case SystemZ::CGR:
1422 return SystemZ::CGRBCall;
1423 case SystemZ::CHI:
1424 return SystemZ::CIBCall;
1425 case SystemZ::CGHI:
1426 return SystemZ::CGIBCall;
1427 case SystemZ::CLR:
1428 return SystemZ::CLRBCall;
1429 case SystemZ::CLGR:
1430 return SystemZ::CLGRBCall;
1431 case SystemZ::CLFI:
1432 return SystemZ::CLIBCall;
1433 case SystemZ::CLGFI:
1434 return SystemZ::CLGIBCall;
1435 default:
1436 return 0;
1437 }
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001438 }
Ulrich Weigand79391ee2016-04-07 16:33:25 +00001439 return 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001440}
1441
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001442void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1443 MachineBasicBlock::iterator MBBI,
1444 unsigned Reg, uint64_t Value) const {
1445 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1446 unsigned Opcode;
1447 if (isInt<16>(Value))
1448 Opcode = SystemZ::LGHI;
1449 else if (SystemZ::isImmLL(Value))
1450 Opcode = SystemZ::LLILL;
1451 else if (SystemZ::isImmLH(Value)) {
1452 Opcode = SystemZ::LLILH;
1453 Value >>= 16;
1454 } else {
1455 assert(isInt<32>(Value) && "Huge values not handled yet");
1456 Opcode = SystemZ::LGFI;
1457 }
1458 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1459}