blob: 0dc32b502f3434aabbeda1d023ea09f55eb1e9d2 [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
Richard Sandiford0755c932013-10-01 11:26:28 +0000163// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
164// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
165// are low registers, otherwise use RISB[LH]G. Size is the number of bits
166// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
167// KillSrc is true if this move is the last use of SrcReg.
168void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
169 MachineBasicBlock::iterator MBBI,
170 DebugLoc DL, unsigned DestReg,
171 unsigned SrcReg, unsigned LowLowOpcode,
172 unsigned Size, bool KillSrc) const {
173 unsigned Opcode;
174 bool DestIsHigh = isHighReg(DestReg);
175 bool SrcIsHigh = isHighReg(SrcReg);
176 if (DestIsHigh && SrcIsHigh)
177 Opcode = SystemZ::RISBHH;
178 else if (DestIsHigh && !SrcIsHigh)
179 Opcode = SystemZ::RISBHL;
180 else if (!DestIsHigh && SrcIsHigh)
181 Opcode = SystemZ::RISBLH;
182 else {
183 BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
184 .addReg(SrcReg, getKillRegState(KillSrc));
185 return;
186 }
187 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
188 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
189 .addReg(DestReg, RegState::Undef)
190 .addReg(SrcReg, getKillRegState(KillSrc))
191 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
192}
193
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000194// If MI is a simple load or store for a frame object, return the register
195// it loads or stores and set FrameIndex to the index of the frame object.
196// Return 0 otherwise.
197//
198// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000199static int isSimpleMove(const MachineInstr *MI, int &FrameIndex,
200 unsigned Flag) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000201 const MCInstrDesc &MCID = MI->getDesc();
202 if ((MCID.TSFlags & Flag) &&
203 MI->getOperand(1).isFI() &&
204 MI->getOperand(2).getImm() == 0 &&
205 MI->getOperand(3).getReg() == 0) {
206 FrameIndex = MI->getOperand(1).getIndex();
207 return MI->getOperand(0).getReg();
208 }
209 return 0;
210}
211
212unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
213 int &FrameIndex) const {
214 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
215}
216
217unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
218 int &FrameIndex) const {
219 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
220}
221
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000222bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr *MI,
223 int &DestFrameIndex,
224 int &SrcFrameIndex) const {
225 // Check for MVC 0(Length,FI1),0(FI2)
226 const MachineFrameInfo *MFI = MI->getParent()->getParent()->getFrameInfo();
227 if (MI->getOpcode() != SystemZ::MVC ||
228 !MI->getOperand(0).isFI() ||
229 MI->getOperand(1).getImm() != 0 ||
230 !MI->getOperand(3).isFI() ||
231 MI->getOperand(4).getImm() != 0)
232 return false;
233
234 // Check that Length covers the full slots.
235 int64_t Length = MI->getOperand(2).getImm();
236 unsigned FI1 = MI->getOperand(0).getIndex();
237 unsigned FI2 = MI->getOperand(3).getIndex();
238 if (MFI->getObjectSize(FI1) != Length ||
239 MFI->getObjectSize(FI2) != Length)
240 return false;
241
242 DestFrameIndex = FI1;
243 SrcFrameIndex = FI2;
244 return true;
245}
246
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000247bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
248 MachineBasicBlock *&TBB,
249 MachineBasicBlock *&FBB,
250 SmallVectorImpl<MachineOperand> &Cond,
251 bool AllowModify) const {
252 // Most of the code and comments here are boilerplate.
253
254 // Start from the bottom of the block and work up, examining the
255 // terminator instructions.
256 MachineBasicBlock::iterator I = MBB.end();
257 while (I != MBB.begin()) {
258 --I;
259 if (I->isDebugValue())
260 continue;
261
262 // Working from the bottom, when we see a non-terminator instruction, we're
263 // done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000264 if (!isUnpredicatedTerminator(*I))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000265 break;
266
267 // A terminator that isn't a branch can't easily be handled by this
268 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000269 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000270 return true;
271
272 // Can't handle indirect branches.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000273 SystemZII::Branch Branch(getBranchInfo(I));
274 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000275 return true;
276
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000277 // Punt on compound branches.
278 if (Branch.Type != SystemZII::BranchNormal)
279 return true;
280
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000281 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000282 // Handle unconditional branches.
283 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000284 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000285 continue;
286 }
287
288 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000289 while (std::next(I) != MBB.end())
290 std::next(I)->eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000291
292 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000293 FBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000294
295 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000296 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000297 TBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000298 I->eraseFromParent();
299 I = MBB.end();
300 continue;
301 }
302
303 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000304 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000305 continue;
306 }
307
308 // Working from the bottom, handle the first conditional branch.
309 if (Cond.empty()) {
310 // FIXME: add X86-style branch swap
311 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000312 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000313 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000314 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000315 continue;
316 }
317
318 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000319 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000320
321 // Only handle the case where all conditional branches branch to the same
322 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000323 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000324 return true;
325
326 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000327 unsigned OldCCValid = Cond[0].getImm();
328 unsigned OldCCMask = Cond[1].getImm();
329 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000330 continue;
331
332 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000333 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000334 }
335
336 return false;
337}
338
339unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
340 // Most of the code and comments here are boilerplate.
341 MachineBasicBlock::iterator I = MBB.end();
342 unsigned Count = 0;
343
344 while (I != MBB.begin()) {
345 --I;
346 if (I->isDebugValue())
347 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000348 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000349 break;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000350 if (!getBranchInfo(I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000351 break;
352 // Remove the branch.
353 I->eraseFromParent();
354 I = MBB.end();
355 ++Count;
356 }
357
358 return Count;
359}
360
Richard Sandiford3d768e32013-07-31 12:30:20 +0000361bool SystemZInstrInfo::
362ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
363 assert(Cond.size() == 2 && "Invalid condition");
364 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
365 return false;
366}
367
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000368unsigned
369SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
370 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000371 ArrayRef<MachineOperand> Cond,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000372 DebugLoc DL) const {
373 // In this function we output 32-bit branches, which should always
374 // have enough range. They can be shortened and relaxed by later code
375 // in the pipeline, if desired.
376
377 // Shouldn't be a fall through.
378 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000379 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000380 "SystemZ branch conditions have one component!");
381
382 if (Cond.empty()) {
383 // Unconditional branch?
384 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000385 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000386 return 1;
387 }
388
389 // Conditional branch.
390 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000391 unsigned CCValid = Cond[0].getImm();
392 unsigned CCMask = Cond[1].getImm();
393 BuildMI(&MBB, DL, get(SystemZ::BRC))
394 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000395 ++Count;
396
397 if (FBB) {
398 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000399 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000400 ++Count;
401 }
402 return Count;
403}
404
Richard Sandiford564681c2013-08-12 10:28:10 +0000405bool SystemZInstrInfo::analyzeCompare(const MachineInstr *MI,
406 unsigned &SrcReg, unsigned &SrcReg2,
407 int &Mask, int &Value) const {
408 assert(MI->isCompare() && "Caller should have checked for a comparison");
409
410 if (MI->getNumExplicitOperands() == 2 &&
411 MI->getOperand(0).isReg() &&
412 MI->getOperand(1).isImm()) {
413 SrcReg = MI->getOperand(0).getReg();
414 SrcReg2 = 0;
415 Value = MI->getOperand(1).getImm();
416 Mask = ~0;
417 return true;
418 }
419
420 return false;
421}
422
Richard Sandiforda5901252013-08-16 10:22:54 +0000423// If Reg is a virtual register, return its definition, otherwise return null.
424static MachineInstr *getDef(unsigned Reg,
425 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000426 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +0000427 return nullptr;
Richard Sandiford564681c2013-08-12 10:28:10 +0000428 return MRI->getUniqueVRegDef(Reg);
429}
430
431// Return true if MI is a shift of type Opcode by Imm bits.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000432static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000433 return (MI->getOpcode() == Opcode &&
434 !MI->getOperand(2).getReg() &&
435 MI->getOperand(3).getImm() == Imm);
436}
437
Richard Sandiforda5901252013-08-16 10:22:54 +0000438// If the destination of MI has no uses, delete it as dead.
439static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
440 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
441 MI->eraseFromParent();
442}
443
Richard Sandiford564681c2013-08-12 10:28:10 +0000444// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000445// the result of an IPM sequence whose input CC survives until Compare,
446// and whether Compare is therefore redundant. Delete it and return
447// true if so.
448static bool removeIPMBasedCompare(MachineInstr *Compare, unsigned SrcReg,
449 const MachineRegisterInfo *MRI,
450 const TargetRegisterInfo *TRI) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000451 MachineInstr *LGFR = nullptr;
Richard Sandiforda5901252013-08-16 10:22:54 +0000452 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000453 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
454 LGFR = RLL;
455 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
456 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000457 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000458 return false;
459
Richard Sandiforda5901252013-08-16 10:22:54 +0000460 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000461 if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
Richard Sandiford564681c2013-08-12 10:28:10 +0000462 return false;
463
Richard Sandiforda5901252013-08-16 10:22:54 +0000464 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000465 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
466 return false;
467
468 // Check that there are no assignments to CC between the IPM and Compare,
Richard Sandiford564681c2013-08-12 10:28:10 +0000469 if (IPM->getParent() != Compare->getParent())
470 return false;
471 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare;
472 for (++MBBI; MBBI != MBBE; ++MBBI) {
473 MachineInstr *MI = MBBI;
Richard Sandiforda5901252013-08-16 10:22:54 +0000474 if (MI->modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000475 return false;
476 }
477
Richard Sandiford564681c2013-08-12 10:28:10 +0000478 Compare->eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000479 if (LGFR)
480 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000481 eraseIfDead(RLL, MRI);
482 eraseIfDead(SRL, MRI);
483 eraseIfDead(IPM, MRI);
484
Richard Sandiford564681c2013-08-12 10:28:10 +0000485 return true;
486}
487
488bool
489SystemZInstrInfo::optimizeCompareInstr(MachineInstr *Compare,
490 unsigned SrcReg, unsigned SrcReg2,
491 int Mask, int Value,
492 const MachineRegisterInfo *MRI) const {
493 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
494 bool IsLogical = (Compare->getDesc().TSFlags & SystemZII::IsLogical) != 0;
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000495 return Value == 0 && !IsLogical &&
496 removeIPMBasedCompare(Compare, SrcReg, MRI, &RI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000497}
498
Richard Sandifordf2404162013-07-25 09:11:15 +0000499// If Opcode is a move that has a conditional variant, return that variant,
500// otherwise return 0.
501static unsigned getConditionalMove(unsigned Opcode) {
502 switch (Opcode) {
503 case SystemZ::LR: return SystemZ::LOCR;
504 case SystemZ::LGR: return SystemZ::LOCGR;
505 default: return 0;
506 }
507}
508
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000509bool SystemZInstrInfo::isPredicable(MachineInstr &MI) const {
510 unsigned Opcode = MI.getOpcode();
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000511 if (STI.hasLoadStoreOnCond() && getConditionalMove(Opcode))
512 return true;
513 if (Opcode == SystemZ::Return)
514 return true;
515 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000516}
517
518bool SystemZInstrInfo::
519isProfitableToIfCvt(MachineBasicBlock &MBB,
520 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000521 BranchProbability Probability) const {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000522 // Avoid using conditional returns at the end of a loop (since then
523 // we'd need to emit an unconditional branch to the beginning anyway,
524 // making the loop body longer). This doesn't apply for low-probability
525 // loops (eg. compare-and-swap retry), so just decide based on branch
526 // probability instead of looping structure.
527 if (MBB.succ_empty() && Probability < BranchProbability(1, 8))
528 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000529 // For now only convert single instructions.
530 return NumCycles == 1;
531}
532
533bool SystemZInstrInfo::
534isProfitableToIfCvt(MachineBasicBlock &TMBB,
535 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
536 MachineBasicBlock &FMBB,
537 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000538 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000539 // For now avoid converting mutually-exclusive cases.
540 return false;
541}
542
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000543bool SystemZInstrInfo::
544isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
545 BranchProbability Probability) const {
546 // For now only duplicate single instructions.
547 return NumCycles == 1;
548}
549
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000550bool SystemZInstrInfo::PredicateInstruction(
551 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000552 assert(Pred.size() == 2 && "Invalid condition");
553 unsigned CCValid = Pred[0].getImm();
554 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000555 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000556 unsigned Opcode = MI.getOpcode();
Eric Christopher673b3af2014-06-27 07:01:17 +0000557 if (STI.hasLoadStoreOnCond()) {
Richard Sandifordf2404162013-07-25 09:11:15 +0000558 if (unsigned CondOpcode = getConditionalMove(Opcode)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000559 MI.setDesc(get(CondOpcode));
560 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
561 .addImm(CCValid)
562 .addImm(CCMask)
563 .addReg(SystemZ::CC, RegState::Implicit);
Richard Sandifordf2404162013-07-25 09:11:15 +0000564 return true;
565 }
566 }
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000567 if (Opcode == SystemZ::Return) {
568 MI.setDesc(get(SystemZ::CondReturn));
569 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
570 .addImm(CCValid).addImm(CCMask)
571 .addReg(SystemZ::CC, RegState::Implicit);
572 return true;
573 }
Richard Sandifordf2404162013-07-25 09:11:15 +0000574 return false;
575}
576
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000577void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
578 MachineBasicBlock::iterator MBBI,
579 DebugLoc DL, unsigned DestReg,
580 unsigned SrcReg, bool KillSrc) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000581 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
582 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000583 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
584 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
585 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
586 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000587 return;
588 }
589
Richard Sandiford0755c932013-10-01 11:26:28 +0000590 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
591 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
592 return;
593 }
594
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000595 // Everything else needs only one instruction.
596 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000597 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000598 Opcode = SystemZ::LGR;
599 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigandcdce0262016-03-14 13:50:03 +0000600 // For z13 we prefer LDR over LER to avoid partial register dependencies.
601 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000602 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
603 Opcode = SystemZ::LDR;
604 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
605 Opcode = SystemZ::LXR;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000606 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
607 Opcode = SystemZ::VLR32;
608 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
609 Opcode = SystemZ::VLR64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000610 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
611 Opcode = SystemZ::VLR;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000612 else
613 llvm_unreachable("Impossible reg-to-reg copy");
614
615 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
616 .addReg(SrcReg, getKillRegState(KillSrc));
617}
618
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000619void SystemZInstrInfo::storeRegToStackSlot(
620 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
621 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
622 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000623 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
624
625 // Callers may expect a single instruction, so keep 128-bit moves
626 // together for now and lower them after register allocation.
627 unsigned LoadOpcode, StoreOpcode;
628 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
629 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000630 .addReg(SrcReg, getKillRegState(isKill)),
631 FrameIdx);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000632}
633
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000634void SystemZInstrInfo::loadRegFromStackSlot(
635 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
636 int FrameIdx, const TargetRegisterClass *RC,
637 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000638 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
639
640 // Callers may expect a single instruction, so keep 128-bit moves
641 // together for now and lower them after register allocation.
642 unsigned LoadOpcode, StoreOpcode;
643 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
644 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
645 FrameIdx);
646}
647
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000648// Return true if MI is a simple load or store with a 12-bit displacement
649// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
650static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
651 const MCInstrDesc &MCID = MI->getDesc();
652 return ((MCID.TSFlags & Flag) &&
653 isUInt<12>(MI->getOperand(2).getImm()) &&
654 MI->getOperand(3).getReg() == 0);
655}
656
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000657namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +0000658struct LogicOp {
659 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
660 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
661 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000662
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000663 explicit operator bool() const { return RegSize; }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000664
Richard Sandifordc2312692014-03-06 10:38:30 +0000665 unsigned RegSize, ImmLSB, ImmSize;
666};
667} // end anonymous namespace
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000668
669static LogicOp interpretAndImmediate(unsigned Opcode) {
670 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000671 case SystemZ::NILMux: return LogicOp(32, 0, 16);
672 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000673 case SystemZ::NILL64: return LogicOp(64, 0, 16);
674 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000675 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
676 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
677 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000678 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000679 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000680 default: return LogicOp();
681 }
682}
683
684// Used to return from convertToThreeAddress after replacing two-address
685// instruction OldMI with three-address instruction NewMI.
686static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
687 MachineInstr *NewMI,
688 LiveVariables *LV) {
689 if (LV) {
690 unsigned NumOps = OldMI->getNumOperands();
691 for (unsigned I = 1; I < NumOps; ++I) {
692 MachineOperand &Op = OldMI->getOperand(I);
693 if (Op.isReg() && Op.isKill())
694 LV->replaceKillInstruction(Op.getReg(), OldMI, NewMI);
695 }
696 }
697 return NewMI;
698}
699
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000700MachineInstr *
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000701SystemZInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
702 MachineBasicBlock::iterator &MBBI,
703 LiveVariables *LV) const {
704 MachineInstr *MI = MBBI;
705 MachineBasicBlock *MBB = MI->getParent();
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000706 MachineFunction *MF = MBB->getParent();
707 MachineRegisterInfo &MRI = MF->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000708
709 unsigned Opcode = MI->getOpcode();
710 unsigned NumOps = MI->getNumOperands();
711
712 // Try to convert something like SLL into SLLK, if supported.
713 // We prefer to keep the two-operand form where possible both
714 // because it tends to be shorter and because some instructions
715 // have memory forms that can be used during spilling.
Eric Christopher673b3af2014-06-27 07:01:17 +0000716 if (STI.hasDistinctOps()) {
Richard Sandiford42a694f2013-10-01 14:53:46 +0000717 MachineOperand &Dest = MI->getOperand(0);
718 MachineOperand &Src = MI->getOperand(1);
719 unsigned DestReg = Dest.getReg();
720 unsigned SrcReg = Src.getReg();
721 // AHIMux is only really a three-operand instruction when both operands
722 // are low registers. Try to constrain both operands to be low if
723 // possible.
724 if (Opcode == SystemZ::AHIMux &&
725 TargetRegisterInfo::isVirtualRegister(DestReg) &&
726 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
727 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
728 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
729 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
730 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
731 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000732 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
733 if (ThreeOperandOpcode >= 0) {
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000734 // Create three address instruction without adding the implicit
735 // operands. Those will instead be copied over from the original
736 // instruction by the loop below.
737 MachineInstrBuilder MIB(*MF,
738 MF->CreateMachineInstr(get(ThreeOperandOpcode),
739 MI->getDebugLoc(), /*NoImplicit=*/true));
740 MIB.addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000741 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000742 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000743 // Keep the remaining operands as-is.
744 for (unsigned I = 2; I < NumOps; ++I)
745 MIB.addOperand(MI->getOperand(I));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000746 MBB->insert(MI, MIB);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000747 return finishConvertToThreeAddress(MI, MIB, LV);
748 }
749 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000750
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000751 // Try to convert an AND into an RISBG-type instruction.
752 if (LogicOp And = interpretAndImmediate(Opcode)) {
Richard Sandiford70284282013-10-01 14:20:41 +0000753 uint64_t Imm = MI->getOperand(2).getImm() << And.ImmLSB;
754 // AND IMMEDIATE leaves the other bits of the register unchanged.
755 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
756 unsigned Start, End;
757 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
758 unsigned NewOpcode;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000759 if (And.RegSize == 64) {
Richard Sandiford70284282013-10-01 14:20:41 +0000760 NewOpcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000761 // Prefer RISBGN if available, since it does not clobber CC.
762 if (STI.hasMiscellaneousExtensions())
763 NewOpcode = SystemZ::RISBGN;
764 } else {
Richard Sandiford70284282013-10-01 14:20:41 +0000765 NewOpcode = SystemZ::RISBMux;
766 Start &= 31;
767 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000768 }
Richard Sandiford70284282013-10-01 14:20:41 +0000769 MachineOperand &Dest = MI->getOperand(0);
770 MachineOperand &Src = MI->getOperand(1);
771 MachineInstrBuilder MIB =
772 BuildMI(*MBB, MI, MI->getDebugLoc(), get(NewOpcode))
773 .addOperand(Dest).addReg(0)
774 .addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg())
775 .addImm(Start).addImm(End + 128).addImm(0);
776 return finishConvertToThreeAddress(MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000777 }
778 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000779 return nullptr;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000780}
781
Keno Fischere70b31f2015-06-08 20:09:58 +0000782MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
783 MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
784 MachineBasicBlock::iterator InsertPt, int FrameIndex) const {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000785 const MachineFrameInfo *MFI = MF.getFrameInfo();
786 unsigned Size = MFI->getObjectSize(FrameIndex);
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000787 unsigned Opcode = MI->getOpcode();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000788
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000789 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
790 if ((Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
791 isInt<8>(MI->getOperand(2).getImm()) &&
792 !MI->getOperand(3).getReg()) {
793 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
Keno Fischere70b31f2015-06-08 20:09:58 +0000794 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
795 get(SystemZ::AGSI))
796 .addFrameIndex(FrameIndex)
797 .addImm(0)
798 .addImm(MI->getOperand(2).getImm());
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000799 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000800 return nullptr;
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000801 }
802
803 // All other cases require a single operand.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000804 if (Ops.size() != 1)
Craig Topper062a2ba2014-04-25 05:30:21 +0000805 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000806
807 unsigned OpNum = Ops[0];
NAKAMURA Takumiddcba562013-07-03 02:20:49 +0000808 assert(Size == MF.getRegInfo()
809 .getRegClass(MI->getOperand(OpNum).getReg())->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000810 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000811
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000812 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) &&
813 OpNum == 0 &&
814 isInt<8>(MI->getOperand(2).getImm())) {
815 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
816 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
Keno Fischere70b31f2015-06-08 20:09:58 +0000817 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
818 get(Opcode))
819 .addFrameIndex(FrameIndex)
820 .addImm(0)
821 .addImm(MI->getOperand(2).getImm());
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000822 }
823
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000824 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
825 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
826 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
827 // If we're spilling the destination of an LDGR or LGDR, store the
828 // source register instead.
829 if (OpNum == 0) {
830 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
Keno Fischere70b31f2015-06-08 20:09:58 +0000831 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
832 get(StoreOpcode))
833 .addOperand(MI->getOperand(1))
834 .addFrameIndex(FrameIndex)
835 .addImm(0)
836 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000837 }
838 // If we're spilling the source of an LDGR or LGDR, load the
839 // destination register instead.
840 if (OpNum == 1) {
841 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
842 unsigned Dest = MI->getOperand(0).getReg();
Keno Fischere70b31f2015-06-08 20:09:58 +0000843 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
844 get(LoadOpcode), Dest)
845 .addFrameIndex(FrameIndex)
846 .addImm(0)
847 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000848 }
849 }
850
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000851 // Look for cases where the source of a simple store or the destination
852 // of a simple load is being spilled. Try to use MVC instead.
853 //
854 // Although MVC is in practice a fast choice in these cases, it is still
855 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +0000856 // load or store is volatile. We also wouldn't be able to use MVC if
857 // the two memories partially overlap, but that case cannot occur here,
858 // because we know that one of the memories is a full frame index.
859 //
860 // For performance reasons, we also want to avoid using MVC if the addresses
861 // might be equal. We don't worry about that case here, because spill slot
862 // coloring happens later, and because we have special code to remove
863 // MVCs that turn out to be redundant.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000864 if (OpNum == 0 && MI->hasOneMemOperand()) {
865 MachineMemOperand *MMO = *MI->memoperands_begin();
866 if (MMO->getSize() == Size && !MMO->isVolatile()) {
867 // Handle conversion of loads.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000868 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXLoad)) {
Keno Fischere70b31f2015-06-08 20:09:58 +0000869 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
870 get(SystemZ::MVC))
871 .addFrameIndex(FrameIndex)
872 .addImm(0)
873 .addImm(Size)
874 .addOperand(MI->getOperand(1))
875 .addImm(MI->getOperand(2).getImm())
876 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000877 }
878 // Handle conversion of stores.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000879 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXStore)) {
Keno Fischere70b31f2015-06-08 20:09:58 +0000880 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
881 get(SystemZ::MVC))
882 .addOperand(MI->getOperand(1))
883 .addImm(MI->getOperand(2).getImm())
884 .addImm(Size)
885 .addFrameIndex(FrameIndex)
886 .addImm(0)
887 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000888 }
889 }
890 }
891
Richard Sandiforded1fab62013-07-03 10:10:02 +0000892 // If the spilled operand is the final one, try to change <INSN>R
893 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000894 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000895 if (MemOpcode >= 0) {
896 unsigned NumOps = MI->getNumExplicitOperands();
897 if (OpNum == NumOps - 1) {
898 const MCInstrDesc &MemDesc = get(MemOpcode);
899 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
900 assert(AccessBytes != 0 && "Size of access should be known");
901 assert(AccessBytes <= Size && "Access outside the frame index");
902 uint64_t Offset = Size - AccessBytes;
Keno Fischere70b31f2015-06-08 20:09:58 +0000903 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
904 MI->getDebugLoc(), get(MemOpcode));
Richard Sandiforded1fab62013-07-03 10:10:02 +0000905 for (unsigned I = 0; I < OpNum; ++I)
906 MIB.addOperand(MI->getOperand(I));
907 MIB.addFrameIndex(FrameIndex).addImm(Offset);
908 if (MemDesc.TSFlags & SystemZII::HasIndex)
909 MIB.addReg(0);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000910 return MIB;
911 }
912 }
913
Craig Topper062a2ba2014-04-25 05:30:21 +0000914 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000915}
916
Keno Fischere70b31f2015-06-08 20:09:58 +0000917MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
918 MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
919 MachineBasicBlock::iterator InsertPt, MachineInstr *LoadMI) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000920 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000921}
922
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000923bool
924SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
925 switch (MI->getOpcode()) {
926 case SystemZ::L128:
927 splitMove(MI, SystemZ::LG);
928 return true;
929
930 case SystemZ::ST128:
931 splitMove(MI, SystemZ::STG);
932 return true;
933
934 case SystemZ::LX:
935 splitMove(MI, SystemZ::LD);
936 return true;
937
938 case SystemZ::STX:
939 splitMove(MI, SystemZ::STD);
940 return true;
941
Richard Sandiford89e160d2013-10-01 12:11:47 +0000942 case SystemZ::LBMux:
943 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
944 return true;
945
946 case SystemZ::LHMux:
947 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
948 return true;
949
Richard Sandiford21235a22013-10-01 12:49:07 +0000950 case SystemZ::LLCRMux:
951 expandZExtPseudo(MI, SystemZ::LLCR, 8);
952 return true;
953
954 case SystemZ::LLHRMux:
955 expandZExtPseudo(MI, SystemZ::LLHR, 16);
956 return true;
957
Richard Sandiford0d46b1a2013-10-01 12:19:08 +0000958 case SystemZ::LLCMux:
959 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
960 return true;
961
962 case SystemZ::LLHMux:
963 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
964 return true;
965
Richard Sandiford0755c932013-10-01 11:26:28 +0000966 case SystemZ::LMux:
967 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
968 return true;
969
Richard Sandiford5469c392013-10-01 12:22:49 +0000970 case SystemZ::STCMux:
971 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
972 return true;
973
974 case SystemZ::STHMux:
975 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
976 return true;
977
Richard Sandiford0755c932013-10-01 11:26:28 +0000978 case SystemZ::STMux:
979 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
980 return true;
981
Richard Sandiford01240232013-10-01 13:02:28 +0000982 case SystemZ::LHIMux:
983 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
984 return true;
985
986 case SystemZ::IIFMux:
987 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
988 return true;
989
Richard Sandiford1a569312013-10-01 13:18:56 +0000990 case SystemZ::IILMux:
991 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
992 return true;
993
994 case SystemZ::IIHMux:
995 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
996 return true;
997
Richard Sandiford70284282013-10-01 14:20:41 +0000998 case SystemZ::NIFMux:
999 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1000 return true;
1001
1002 case SystemZ::NILMux:
1003 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1004 return true;
1005
1006 case SystemZ::NIHMux:
1007 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1008 return true;
1009
Richard Sandiford6e96ac62013-10-01 13:22:41 +00001010 case SystemZ::OIFMux:
1011 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1012 return true;
1013
1014 case SystemZ::OILMux:
1015 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1016 return true;
1017
1018 case SystemZ::OIHMux:
1019 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1020 return true;
1021
Richard Sandiford5718dac2013-10-01 14:08:44 +00001022 case SystemZ::XIFMux:
1023 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1024 return true;
1025
Richard Sandiford2cac7632013-10-01 14:41:52 +00001026 case SystemZ::TMLMux:
1027 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1028 return true;
1029
1030 case SystemZ::TMHMux:
1031 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1032 return true;
1033
Richard Sandiford42a694f2013-10-01 14:53:46 +00001034 case SystemZ::AHIMux:
1035 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1036 return true;
1037
1038 case SystemZ::AHIMuxK:
1039 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1040 return true;
1041
1042 case SystemZ::AFIMux:
1043 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1044 return true;
1045
Richard Sandiforda9ac0e02013-10-01 14:56:23 +00001046 case SystemZ::CFIMux:
1047 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1048 return true;
1049
1050 case SystemZ::CLFIMux:
1051 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1052 return true;
1053
Richard Sandifordb63e3002013-10-01 15:00:44 +00001054 case SystemZ::CMux:
1055 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1056 return true;
1057
1058 case SystemZ::CLMux:
1059 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1060 return true;
1061
Richard Sandiford70284282013-10-01 14:20:41 +00001062 case SystemZ::RISBMux: {
1063 bool DestIsHigh = isHighReg(MI->getOperand(0).getReg());
1064 bool SrcIsHigh = isHighReg(MI->getOperand(2).getReg());
1065 if (SrcIsHigh == DestIsHigh)
1066 MI->setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
1067 else {
1068 MI->setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1069 MI->getOperand(5).setImm(MI->getOperand(5).getImm() ^ 32);
1070 }
1071 return true;
1072 }
1073
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001074 case SystemZ::ADJDYNALLOC:
1075 splitAdjDynAlloc(MI);
1076 return true;
1077
1078 default:
1079 return false;
1080 }
1081}
1082
Richard Sandiford312425f2013-05-20 14:23:08 +00001083uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
1084 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
1085 const MachineFunction *MF = MI->getParent()->getParent();
1086 const char *AsmStr = MI->getOperand(0).getSymbolName();
1087 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1088 }
1089 return MI->getDesc().getSize();
1090}
1091
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001092SystemZII::Branch
1093SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001094 switch (MI->getOpcode()) {
1095 case SystemZ::BR:
1096 case SystemZ::J:
1097 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001098 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Richard Sandiford3d768e32013-07-31 12:30:20 +00001099 SystemZ::CCMASK_ANY, &MI->getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001100
1101 case SystemZ::BRC:
1102 case SystemZ::BRCL:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001103 return SystemZII::Branch(SystemZII::BranchNormal,
Richard Sandiford3d768e32013-07-31 12:30:20 +00001104 MI->getOperand(0).getImm(),
1105 MI->getOperand(1).getImm(), &MI->getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001106
Richard Sandifordc2121252013-08-05 11:23:46 +00001107 case SystemZ::BRCT:
1108 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
1109 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
1110
1111 case SystemZ::BRCTG:
1112 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
1113 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
1114
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001115 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001116 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001117 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
1118 MI->getOperand(2).getImm(), &MI->getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001119
Richard Sandiford93183ee2013-09-18 09:56:40 +00001120 case SystemZ::CLIJ:
1121 case SystemZ::CLRJ:
1122 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
1123 MI->getOperand(2).getImm(), &MI->getOperand(3));
1124
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001125 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001126 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001127 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
1128 MI->getOperand(2).getImm(), &MI->getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001129
Richard Sandiford93183ee2013-09-18 09:56:40 +00001130 case SystemZ::CLGIJ:
1131 case SystemZ::CLGRJ:
1132 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
1133 MI->getOperand(2).getImm(), &MI->getOperand(3));
1134
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001135 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001136 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001137 }
1138}
1139
1140void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1141 unsigned &LoadOpcode,
1142 unsigned &StoreOpcode) const {
1143 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1144 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001145 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001146 } else if (RC == &SystemZ::GRH32BitRegClass) {
1147 LoadOpcode = SystemZ::LFH;
1148 StoreOpcode = SystemZ::STFH;
1149 } else if (RC == &SystemZ::GRX32BitRegClass) {
1150 LoadOpcode = SystemZ::LMux;
1151 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001152 } else if (RC == &SystemZ::GR64BitRegClass ||
1153 RC == &SystemZ::ADDR64BitRegClass) {
1154 LoadOpcode = SystemZ::LG;
1155 StoreOpcode = SystemZ::STG;
1156 } else if (RC == &SystemZ::GR128BitRegClass ||
1157 RC == &SystemZ::ADDR128BitRegClass) {
1158 LoadOpcode = SystemZ::L128;
1159 StoreOpcode = SystemZ::ST128;
1160 } else if (RC == &SystemZ::FP32BitRegClass) {
1161 LoadOpcode = SystemZ::LE;
1162 StoreOpcode = SystemZ::STE;
1163 } else if (RC == &SystemZ::FP64BitRegClass) {
1164 LoadOpcode = SystemZ::LD;
1165 StoreOpcode = SystemZ::STD;
1166 } else if (RC == &SystemZ::FP128BitRegClass) {
1167 LoadOpcode = SystemZ::LX;
1168 StoreOpcode = SystemZ::STX;
Ulrich Weigand49506d72015-05-05 19:28:34 +00001169 } else if (RC == &SystemZ::VR32BitRegClass) {
1170 LoadOpcode = SystemZ::VL32;
1171 StoreOpcode = SystemZ::VST32;
1172 } else if (RC == &SystemZ::VR64BitRegClass) {
1173 LoadOpcode = SystemZ::VL64;
1174 StoreOpcode = SystemZ::VST64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001175 } else if (RC == &SystemZ::VF128BitRegClass ||
1176 RC == &SystemZ::VR128BitRegClass) {
1177 LoadOpcode = SystemZ::VL;
1178 StoreOpcode = SystemZ::VST;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001179 } else
1180 llvm_unreachable("Unsupported regclass to load or store");
1181}
1182
1183unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1184 int64_t Offset) const {
1185 const MCInstrDesc &MCID = get(Opcode);
1186 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1187 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1188 // Get the instruction to use for unsigned 12-bit displacements.
1189 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1190 if (Disp12Opcode >= 0)
1191 return Disp12Opcode;
1192
1193 // All address-related instructions can use unsigned 12-bit
1194 // displacements.
1195 return Opcode;
1196 }
1197 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1198 // Get the instruction to use for signed 20-bit displacements.
1199 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1200 if (Disp20Opcode >= 0)
1201 return Disp20Opcode;
1202
1203 // Check whether Opcode allows signed 20-bit displacements.
1204 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1205 return Opcode;
1206 }
1207 return 0;
1208}
1209
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001210unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1211 switch (Opcode) {
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001212 case SystemZ::L: return SystemZ::LT;
1213 case SystemZ::LY: return SystemZ::LT;
1214 case SystemZ::LG: return SystemZ::LTG;
1215 case SystemZ::LGF: return SystemZ::LTGF;
1216 case SystemZ::LR: return SystemZ::LTR;
1217 case SystemZ::LGFR: return SystemZ::LTGFR;
1218 case SystemZ::LGR: return SystemZ::LTGR;
1219 case SystemZ::LER: return SystemZ::LTEBR;
1220 case SystemZ::LDR: return SystemZ::LTDBR;
1221 case SystemZ::LXR: return SystemZ::LTXBR;
Jonas Paulsson12629322015-10-01 18:12:28 +00001222 case SystemZ::LCDFR: return SystemZ::LCDBR;
1223 case SystemZ::LPDFR: return SystemZ::LPDBR;
1224 case SystemZ::LNDFR: return SystemZ::LNDBR;
1225 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1226 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1227 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001228 // On zEC12 we prefer to use RISBGN. But if there is a chance to
1229 // actually use the condition code, we may turn it back into RISGB.
1230 // Note that RISBG is not really a "load-and-test" instruction,
1231 // but sets the same condition code values, so is OK to use here.
1232 case SystemZ::RISBGN: return SystemZ::RISBG;
1233 default: return 0;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001234 }
1235}
1236
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001237// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1238// have already been filtered out. Store the first set bit in LSB and
1239// the number of set bits in Length if so.
1240static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1241 unsigned First = findFirstSet(Mask);
1242 uint64_t Top = (Mask >> First) + 1;
1243 if ((Top & -Top) == Top) {
1244 LSB = First;
1245 Length = findFirstSet(Top);
1246 return true;
1247 }
1248 return false;
1249}
1250
1251bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1252 unsigned &Start, unsigned &End) const {
1253 // Reject trivial all-zero masks.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001254 Mask &= allOnes(BitSize);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001255 if (Mask == 0)
1256 return false;
1257
1258 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1259 // the msb and End specifies the index of the lsb.
1260 unsigned LSB, Length;
1261 if (isStringOfOnes(Mask, LSB, Length)) {
1262 Start = 63 - (LSB + Length - 1);
1263 End = 63 - LSB;
1264 return true;
1265 }
1266
1267 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1268 // of the low 1s and End specifies the lsb of the high 1s.
1269 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1270 assert(LSB > 0 && "Bottom bit must be set");
1271 assert(LSB + Length < BitSize && "Top bit must be set");
1272 Start = 63 - (LSB - 1);
1273 End = 63 - (LSB + Length);
1274 return true;
1275 }
1276
1277 return false;
1278}
1279
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001280unsigned SystemZInstrInfo::getCompareAndBranch(unsigned Opcode,
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001281 SystemZII::CompareAndBranchType Type,
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001282 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001283 switch (Opcode) {
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001284 case SystemZ::CHI:
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001285 case SystemZ::CGHI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001286 if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
1287 return 0;
1288 break;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001289 case SystemZ::CLFI:
Richard Sandiford93183ee2013-09-18 09:56:40 +00001290 case SystemZ::CLGFI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001291 if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
1292 return 0;
1293 }
1294 switch (Type) {
1295 case SystemZII::CompareAndBranch:
1296 switch (Opcode) {
1297 case SystemZ::CR:
1298 return SystemZ::CRJ;
1299 case SystemZ::CGR:
1300 return SystemZ::CGRJ;
1301 case SystemZ::CHI:
1302 return SystemZ::CIJ;
1303 case SystemZ::CGHI:
1304 return SystemZ::CGIJ;
1305 case SystemZ::CLR:
1306 return SystemZ::CLRJ;
1307 case SystemZ::CLGR:
1308 return SystemZ::CLGRJ;
1309 case SystemZ::CLFI:
1310 return SystemZ::CLIJ;
1311 case SystemZ::CLGFI:
1312 return SystemZ::CLGIJ;
1313 default:
1314 return 0;
1315 }
1316 case SystemZII::CompareAndReturn:
1317 switch (Opcode) {
1318 case SystemZ::CR:
1319 return SystemZ::CRBReturn;
1320 case SystemZ::CGR:
1321 return SystemZ::CGRBReturn;
1322 case SystemZ::CHI:
1323 return SystemZ::CIBReturn;
1324 case SystemZ::CGHI:
1325 return SystemZ::CGIBReturn;
1326 case SystemZ::CLR:
1327 return SystemZ::CLRBReturn;
1328 case SystemZ::CLGR:
1329 return SystemZ::CLGRBReturn;
1330 case SystemZ::CLFI:
1331 return SystemZ::CLIBReturn;
1332 case SystemZ::CLGFI:
1333 return SystemZ::CLGIBReturn;
1334 default:
1335 return 0;
1336 }
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001337 default:
1338 return 0;
1339 }
1340}
1341
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001342void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1343 MachineBasicBlock::iterator MBBI,
1344 unsigned Reg, uint64_t Value) const {
1345 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1346 unsigned Opcode;
1347 if (isInt<16>(Value))
1348 Opcode = SystemZ::LGHI;
1349 else if (SystemZ::isImmLL(Value))
1350 Opcode = SystemZ::LLILL;
1351 else if (SystemZ::isImmLH(Value)) {
1352 Opcode = SystemZ::LLILH;
1353 Value >>= 16;
1354 } else {
1355 assert(isInt<32>(Value) && "Huge values not handled yet");
1356 Opcode = SystemZ::LGFI;
1357 }
1358 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1359}