blob: 5d7accc6c0ecbd91657557a5d760a75f8116c09a [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 Paulsson63a2b682015-10-10 07:14:24 +000072 // Clear the kill flag for the address reg in the first instruction.
73 EarlierMI->getOperand(1).setIsKill(false);
74
Ulrich Weigand5f613df2013-05-06 16:15:19 +000075 // Set the opcodes.
76 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
77 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
78 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
79
80 EarlierMI->setDesc(get(HighOpcode));
81 MI->setDesc(get(LowOpcode));
82}
83
84// Split ADJDYNALLOC instruction MI.
85void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
86 MachineBasicBlock *MBB = MI->getParent();
87 MachineFunction &MF = *MBB->getParent();
88 MachineFrameInfo *MFFrame = MF.getFrameInfo();
89 MachineOperand &OffsetMO = MI->getOperand(2);
90
91 uint64_t Offset = (MFFrame->getMaxCallFrameSize() +
92 SystemZMC::CallFrameSize +
93 OffsetMO.getImm());
94 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
95 assert(NewOpcode && "No support for huge argument lists yet");
96 MI->setDesc(get(NewOpcode));
97 OffsetMO.setImm(Offset);
98}
99
Richard Sandiford01240232013-10-01 13:02:28 +0000100// MI is an RI-style pseudo instruction. Replace it with LowOpcode
101// if the first operand is a low GR32 and HighOpcode if the first operand
102// is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
103// and HighOpcode takes an unsigned 32-bit operand. In those cases,
104// MI has the same kind of operand as LowOpcode, so needs to be converted
105// if HighOpcode is used.
106void SystemZInstrInfo::expandRIPseudo(MachineInstr *MI, unsigned LowOpcode,
107 unsigned HighOpcode,
108 bool ConvertHigh) const {
109 unsigned Reg = MI->getOperand(0).getReg();
110 bool IsHigh = isHighReg(Reg);
111 MI->setDesc(get(IsHigh ? HighOpcode : LowOpcode));
112 if (IsHigh && ConvertHigh)
113 MI->getOperand(1).setImm(uint32_t(MI->getOperand(1).getImm()));
114}
115
Richard Sandiford42a694f2013-10-01 14:53:46 +0000116// MI is a three-operand RIE-style pseudo instruction. Replace it with
Jonas Paulsson18d877f2015-10-09 07:19:16 +0000117// LowOpcodeK if the registers are both low GR32s, otherwise use a move
Richard Sandiford42a694f2013-10-01 14:53:46 +0000118// followed by HighOpcode or LowOpcode, depending on whether the target
119// is a high or low GR32.
120void SystemZInstrInfo::expandRIEPseudo(MachineInstr *MI, unsigned LowOpcode,
121 unsigned LowOpcodeK,
122 unsigned HighOpcode) const {
123 unsigned DestReg = MI->getOperand(0).getReg();
124 unsigned SrcReg = MI->getOperand(1).getReg();
125 bool DestIsHigh = isHighReg(DestReg);
126 bool SrcIsHigh = isHighReg(SrcReg);
127 if (!DestIsHigh && !SrcIsHigh)
128 MI->setDesc(get(LowOpcodeK));
129 else {
130 emitGRX32Move(*MI->getParent(), MI, MI->getDebugLoc(),
131 DestReg, SrcReg, SystemZ::LR, 32,
132 MI->getOperand(1).isKill());
133 MI->setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
134 MI->getOperand(1).setReg(DestReg);
Jonas Paulsson18d877f2015-10-09 07:19:16 +0000135 MI->tieOperands(0, 1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000136 }
137}
138
Richard Sandiford0755c932013-10-01 11:26:28 +0000139// MI is an RXY-style pseudo instruction. Replace it with LowOpcode
140// if the first operand is a low GR32 and HighOpcode if the first operand
141// is a high GR32.
142void SystemZInstrInfo::expandRXYPseudo(MachineInstr *MI, unsigned LowOpcode,
143 unsigned HighOpcode) const {
144 unsigned Reg = MI->getOperand(0).getReg();
145 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
146 MI->getOperand(2).getImm());
147 MI->setDesc(get(Opcode));
148}
149
Richard Sandiford21235a22013-10-01 12:49:07 +0000150// MI is an RR-style pseudo instruction that zero-extends the low Size bits
151// of one GRX32 into another. Replace it with LowOpcode if both operands
152// are low registers, otherwise use RISB[LH]G.
153void SystemZInstrInfo::expandZExtPseudo(MachineInstr *MI, unsigned LowOpcode,
154 unsigned Size) const {
155 emitGRX32Move(*MI->getParent(), MI, MI->getDebugLoc(),
156 MI->getOperand(0).getReg(), MI->getOperand(1).getReg(),
157 LowOpcode, Size, MI->getOperand(1).isKill());
158 MI->eraseFromParent();
159}
160
Richard Sandiford0755c932013-10-01 11:26:28 +0000161// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
162// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
163// are low registers, otherwise use RISB[LH]G. Size is the number of bits
164// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
165// KillSrc is true if this move is the last use of SrcReg.
166void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
167 MachineBasicBlock::iterator MBBI,
168 DebugLoc DL, unsigned DestReg,
169 unsigned SrcReg, unsigned LowLowOpcode,
170 unsigned Size, bool KillSrc) const {
171 unsigned Opcode;
172 bool DestIsHigh = isHighReg(DestReg);
173 bool SrcIsHigh = isHighReg(SrcReg);
174 if (DestIsHigh && SrcIsHigh)
175 Opcode = SystemZ::RISBHH;
176 else if (DestIsHigh && !SrcIsHigh)
177 Opcode = SystemZ::RISBHL;
178 else if (!DestIsHigh && SrcIsHigh)
179 Opcode = SystemZ::RISBLH;
180 else {
181 BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
182 .addReg(SrcReg, getKillRegState(KillSrc));
183 return;
184 }
185 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
186 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
187 .addReg(DestReg, RegState::Undef)
188 .addReg(SrcReg, getKillRegState(KillSrc))
189 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
190}
191
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192// If MI is a simple load or store for a frame object, return the register
193// it loads or stores and set FrameIndex to the index of the frame object.
194// Return 0 otherwise.
195//
196// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000197static int isSimpleMove(const MachineInstr *MI, int &FrameIndex,
198 unsigned Flag) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199 const MCInstrDesc &MCID = MI->getDesc();
200 if ((MCID.TSFlags & Flag) &&
201 MI->getOperand(1).isFI() &&
202 MI->getOperand(2).getImm() == 0 &&
203 MI->getOperand(3).getReg() == 0) {
204 FrameIndex = MI->getOperand(1).getIndex();
205 return MI->getOperand(0).getReg();
206 }
207 return 0;
208}
209
210unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
211 int &FrameIndex) const {
212 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
213}
214
215unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
216 int &FrameIndex) const {
217 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
218}
219
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000220bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr *MI,
221 int &DestFrameIndex,
222 int &SrcFrameIndex) const {
223 // Check for MVC 0(Length,FI1),0(FI2)
224 const MachineFrameInfo *MFI = MI->getParent()->getParent()->getFrameInfo();
225 if (MI->getOpcode() != SystemZ::MVC ||
226 !MI->getOperand(0).isFI() ||
227 MI->getOperand(1).getImm() != 0 ||
228 !MI->getOperand(3).isFI() ||
229 MI->getOperand(4).getImm() != 0)
230 return false;
231
232 // Check that Length covers the full slots.
233 int64_t Length = MI->getOperand(2).getImm();
234 unsigned FI1 = MI->getOperand(0).getIndex();
235 unsigned FI2 = MI->getOperand(3).getIndex();
236 if (MFI->getObjectSize(FI1) != Length ||
237 MFI->getObjectSize(FI2) != Length)
238 return false;
239
240 DestFrameIndex = FI1;
241 SrcFrameIndex = FI2;
242 return true;
243}
244
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000245bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
246 MachineBasicBlock *&TBB,
247 MachineBasicBlock *&FBB,
248 SmallVectorImpl<MachineOperand> &Cond,
249 bool AllowModify) const {
250 // Most of the code and comments here are boilerplate.
251
252 // Start from the bottom of the block and work up, examining the
253 // terminator instructions.
254 MachineBasicBlock::iterator I = MBB.end();
255 while (I != MBB.begin()) {
256 --I;
257 if (I->isDebugValue())
258 continue;
259
260 // Working from the bottom, when we see a non-terminator instruction, we're
261 // done.
262 if (!isUnpredicatedTerminator(I))
263 break;
264
265 // A terminator that isn't a branch can't easily be handled by this
266 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000267 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000268 return true;
269
270 // Can't handle indirect branches.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000271 SystemZII::Branch Branch(getBranchInfo(I));
272 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000273 return true;
274
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000275 // Punt on compound branches.
276 if (Branch.Type != SystemZII::BranchNormal)
277 return true;
278
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000279 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000280 // Handle unconditional branches.
281 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000282 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000283 continue;
284 }
285
286 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000287 while (std::next(I) != MBB.end())
288 std::next(I)->eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000289
290 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000291 FBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000292
293 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000294 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000295 TBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000296 I->eraseFromParent();
297 I = MBB.end();
298 continue;
299 }
300
301 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000302 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000303 continue;
304 }
305
306 // Working from the bottom, handle the first conditional branch.
307 if (Cond.empty()) {
308 // FIXME: add X86-style branch swap
309 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000310 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000311 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000312 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000313 continue;
314 }
315
316 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000317 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000318
319 // Only handle the case where all conditional branches branch to the same
320 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000321 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000322 return true;
323
324 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000325 unsigned OldCCValid = Cond[0].getImm();
326 unsigned OldCCMask = Cond[1].getImm();
327 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000328 continue;
329
330 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000331 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000332 }
333
334 return false;
335}
336
337unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
338 // Most of the code and comments here are boilerplate.
339 MachineBasicBlock::iterator I = MBB.end();
340 unsigned Count = 0;
341
342 while (I != MBB.begin()) {
343 --I;
344 if (I->isDebugValue())
345 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000346 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000347 break;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000348 if (!getBranchInfo(I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000349 break;
350 // Remove the branch.
351 I->eraseFromParent();
352 I = MBB.end();
353 ++Count;
354 }
355
356 return Count;
357}
358
Richard Sandiford3d768e32013-07-31 12:30:20 +0000359bool SystemZInstrInfo::
360ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
361 assert(Cond.size() == 2 && "Invalid condition");
362 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
363 return false;
364}
365
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000366unsigned
367SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
368 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000369 ArrayRef<MachineOperand> Cond,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000370 DebugLoc DL) const {
371 // In this function we output 32-bit branches, which should always
372 // have enough range. They can be shortened and relaxed by later code
373 // in the pipeline, if desired.
374
375 // Shouldn't be a fall through.
376 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000377 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000378 "SystemZ branch conditions have one component!");
379
380 if (Cond.empty()) {
381 // Unconditional branch?
382 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000383 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000384 return 1;
385 }
386
387 // Conditional branch.
388 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000389 unsigned CCValid = Cond[0].getImm();
390 unsigned CCMask = Cond[1].getImm();
391 BuildMI(&MBB, DL, get(SystemZ::BRC))
392 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000393 ++Count;
394
395 if (FBB) {
396 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000397 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000398 ++Count;
399 }
400 return Count;
401}
402
Richard Sandiford564681c2013-08-12 10:28:10 +0000403bool SystemZInstrInfo::analyzeCompare(const MachineInstr *MI,
404 unsigned &SrcReg, unsigned &SrcReg2,
405 int &Mask, int &Value) const {
406 assert(MI->isCompare() && "Caller should have checked for a comparison");
407
408 if (MI->getNumExplicitOperands() == 2 &&
409 MI->getOperand(0).isReg() &&
410 MI->getOperand(1).isImm()) {
411 SrcReg = MI->getOperand(0).getReg();
412 SrcReg2 = 0;
413 Value = MI->getOperand(1).getImm();
414 Mask = ~0;
415 return true;
416 }
417
418 return false;
419}
420
Richard Sandiforda5901252013-08-16 10:22:54 +0000421// If Reg is a virtual register, return its definition, otherwise return null.
422static MachineInstr *getDef(unsigned Reg,
423 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000424 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +0000425 return nullptr;
Richard Sandiford564681c2013-08-12 10:28:10 +0000426 return MRI->getUniqueVRegDef(Reg);
427}
428
429// Return true if MI is a shift of type Opcode by Imm bits.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000430static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000431 return (MI->getOpcode() == Opcode &&
432 !MI->getOperand(2).getReg() &&
433 MI->getOperand(3).getImm() == Imm);
434}
435
Richard Sandiforda5901252013-08-16 10:22:54 +0000436// If the destination of MI has no uses, delete it as dead.
437static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
438 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
439 MI->eraseFromParent();
440}
441
Richard Sandiford564681c2013-08-12 10:28:10 +0000442// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000443// the result of an IPM sequence whose input CC survives until Compare,
444// and whether Compare is therefore redundant. Delete it and return
445// true if so.
446static bool removeIPMBasedCompare(MachineInstr *Compare, unsigned SrcReg,
447 const MachineRegisterInfo *MRI,
448 const TargetRegisterInfo *TRI) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000449 MachineInstr *LGFR = nullptr;
Richard Sandiforda5901252013-08-16 10:22:54 +0000450 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000451 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
452 LGFR = RLL;
453 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
454 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000455 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000456 return false;
457
Richard Sandiforda5901252013-08-16 10:22:54 +0000458 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000459 if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
Richard Sandiford564681c2013-08-12 10:28:10 +0000460 return false;
461
Richard Sandiforda5901252013-08-16 10:22:54 +0000462 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000463 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
464 return false;
465
466 // Check that there are no assignments to CC between the IPM and Compare,
Richard Sandiford564681c2013-08-12 10:28:10 +0000467 if (IPM->getParent() != Compare->getParent())
468 return false;
469 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare;
470 for (++MBBI; MBBI != MBBE; ++MBBI) {
471 MachineInstr *MI = MBBI;
Richard Sandiforda5901252013-08-16 10:22:54 +0000472 if (MI->modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000473 return false;
474 }
475
Richard Sandiford564681c2013-08-12 10:28:10 +0000476 Compare->eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000477 if (LGFR)
478 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000479 eraseIfDead(RLL, MRI);
480 eraseIfDead(SRL, MRI);
481 eraseIfDead(IPM, MRI);
482
Richard Sandiford564681c2013-08-12 10:28:10 +0000483 return true;
484}
485
486bool
487SystemZInstrInfo::optimizeCompareInstr(MachineInstr *Compare,
488 unsigned SrcReg, unsigned SrcReg2,
489 int Mask, int Value,
490 const MachineRegisterInfo *MRI) const {
491 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
492 bool IsLogical = (Compare->getDesc().TSFlags & SystemZII::IsLogical) != 0;
493 if (Value == 0 &&
494 !IsLogical &&
Eric Christopher673b3af2014-06-27 07:01:17 +0000495 removeIPMBasedCompare(Compare, SrcReg, MRI, &RI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000496 return true;
497 return false;
498}
499
Richard Sandifordf2404162013-07-25 09:11:15 +0000500// If Opcode is a move that has a conditional variant, return that variant,
501// otherwise return 0.
502static unsigned getConditionalMove(unsigned Opcode) {
503 switch (Opcode) {
504 case SystemZ::LR: return SystemZ::LOCR;
505 case SystemZ::LGR: return SystemZ::LOCGR;
506 default: return 0;
507 }
508}
509
510bool SystemZInstrInfo::isPredicable(MachineInstr *MI) const {
511 unsigned Opcode = MI->getOpcode();
Eric Christopher673b3af2014-06-27 07:01:17 +0000512 if (STI.hasLoadStoreOnCond() &&
Richard Sandifordf2404162013-07-25 09:11:15 +0000513 getConditionalMove(Opcode))
514 return true;
515 return false;
516}
517
518bool SystemZInstrInfo::
519isProfitableToIfCvt(MachineBasicBlock &MBB,
520 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000521 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000522 // For now only convert single instructions.
523 return NumCycles == 1;
524}
525
526bool SystemZInstrInfo::
527isProfitableToIfCvt(MachineBasicBlock &TMBB,
528 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
529 MachineBasicBlock &FMBB,
530 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000531 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000532 // For now avoid converting mutually-exclusive cases.
533 return false;
534}
535
536bool SystemZInstrInfo::
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000537PredicateInstruction(MachineInstr *MI, ArrayRef<MachineOperand> Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000538 assert(Pred.size() == 2 && "Invalid condition");
539 unsigned CCValid = Pred[0].getImm();
540 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000541 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
542 unsigned Opcode = MI->getOpcode();
Eric Christopher673b3af2014-06-27 07:01:17 +0000543 if (STI.hasLoadStoreOnCond()) {
Richard Sandifordf2404162013-07-25 09:11:15 +0000544 if (unsigned CondOpcode = getConditionalMove(Opcode)) {
545 MI->setDesc(get(CondOpcode));
Richard Sandiford3d768e32013-07-31 12:30:20 +0000546 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000547 .addImm(CCValid).addImm(CCMask)
Alp Toker98444342014-04-19 23:56:35 +0000548 .addReg(SystemZ::CC, RegState::Implicit);
Richard Sandifordf2404162013-07-25 09:11:15 +0000549 return true;
550 }
551 }
552 return false;
553}
554
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000555void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
556 MachineBasicBlock::iterator MBBI,
557 DebugLoc DL, unsigned DestReg,
558 unsigned SrcReg, bool KillSrc) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000559 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
560 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000561 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
562 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
563 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
564 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000565 return;
566 }
567
Richard Sandiford0755c932013-10-01 11:26:28 +0000568 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
569 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
570 return;
571 }
572
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000573 // Everything else needs only one instruction.
574 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000575 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000576 Opcode = SystemZ::LGR;
577 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
578 Opcode = SystemZ::LER;
579 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
580 Opcode = SystemZ::LDR;
581 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
582 Opcode = SystemZ::LXR;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000583 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
584 Opcode = SystemZ::VLR32;
585 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
586 Opcode = SystemZ::VLR64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000587 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
588 Opcode = SystemZ::VLR;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000589 else
590 llvm_unreachable("Impossible reg-to-reg copy");
591
592 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
593 .addReg(SrcReg, getKillRegState(KillSrc));
594}
595
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000596void SystemZInstrInfo::storeRegToStackSlot(
597 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
598 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
599 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000600 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
601
602 // Callers may expect a single instruction, so keep 128-bit moves
603 // together for now and lower them after register allocation.
604 unsigned LoadOpcode, StoreOpcode;
605 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
606 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000607 .addReg(SrcReg, getKillRegState(isKill)),
608 FrameIdx);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000609}
610
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000611void SystemZInstrInfo::loadRegFromStackSlot(
612 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
613 int FrameIdx, const TargetRegisterClass *RC,
614 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000615 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
616
617 // Callers may expect a single instruction, so keep 128-bit moves
618 // together for now and lower them after register allocation.
619 unsigned LoadOpcode, StoreOpcode;
620 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
621 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
622 FrameIdx);
623}
624
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000625// Return true if MI is a simple load or store with a 12-bit displacement
626// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
627static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
628 const MCInstrDesc &MCID = MI->getDesc();
629 return ((MCID.TSFlags & Flag) &&
630 isUInt<12>(MI->getOperand(2).getImm()) &&
631 MI->getOperand(3).getReg() == 0);
632}
633
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000634namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +0000635struct LogicOp {
636 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
637 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
638 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000639
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000640 explicit operator bool() const { return RegSize; }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000641
Richard Sandifordc2312692014-03-06 10:38:30 +0000642 unsigned RegSize, ImmLSB, ImmSize;
643};
644} // end anonymous namespace
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000645
646static LogicOp interpretAndImmediate(unsigned Opcode) {
647 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000648 case SystemZ::NILMux: return LogicOp(32, 0, 16);
649 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000650 case SystemZ::NILL64: return LogicOp(64, 0, 16);
651 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000652 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
653 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
654 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000655 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000656 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000657 default: return LogicOp();
658 }
659}
660
661// Used to return from convertToThreeAddress after replacing two-address
662// instruction OldMI with three-address instruction NewMI.
663static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
664 MachineInstr *NewMI,
665 LiveVariables *LV) {
666 if (LV) {
667 unsigned NumOps = OldMI->getNumOperands();
668 for (unsigned I = 1; I < NumOps; ++I) {
669 MachineOperand &Op = OldMI->getOperand(I);
670 if (Op.isReg() && Op.isKill())
671 LV->replaceKillInstruction(Op.getReg(), OldMI, NewMI);
672 }
673 }
674 return NewMI;
675}
676
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000677MachineInstr *
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000678SystemZInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
679 MachineBasicBlock::iterator &MBBI,
680 LiveVariables *LV) const {
681 MachineInstr *MI = MBBI;
682 MachineBasicBlock *MBB = MI->getParent();
Richard Sandiford42a694f2013-10-01 14:53:46 +0000683 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000684
685 unsigned Opcode = MI->getOpcode();
686 unsigned NumOps = MI->getNumOperands();
687
688 // Try to convert something like SLL into SLLK, if supported.
689 // We prefer to keep the two-operand form where possible both
690 // because it tends to be shorter and because some instructions
691 // have memory forms that can be used during spilling.
Eric Christopher673b3af2014-06-27 07:01:17 +0000692 if (STI.hasDistinctOps()) {
Richard Sandiford42a694f2013-10-01 14:53:46 +0000693 MachineOperand &Dest = MI->getOperand(0);
694 MachineOperand &Src = MI->getOperand(1);
695 unsigned DestReg = Dest.getReg();
696 unsigned SrcReg = Src.getReg();
697 // AHIMux is only really a three-operand instruction when both operands
698 // are low registers. Try to constrain both operands to be low if
699 // possible.
700 if (Opcode == SystemZ::AHIMux &&
701 TargetRegisterInfo::isVirtualRegister(DestReg) &&
702 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
703 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
704 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
705 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
706 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
707 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000708 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
709 if (ThreeOperandOpcode >= 0) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000710 MachineInstrBuilder MIB =
711 BuildMI(*MBB, MBBI, MI->getDebugLoc(), get(ThreeOperandOpcode))
712 .addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000713 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000714 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000715 // Keep the remaining operands as-is.
716 for (unsigned I = 2; I < NumOps; ++I)
717 MIB.addOperand(MI->getOperand(I));
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000718 return finishConvertToThreeAddress(MI, MIB, LV);
719 }
720 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000721
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000722 // Try to convert an AND into an RISBG-type instruction.
723 if (LogicOp And = interpretAndImmediate(Opcode)) {
Richard Sandiford70284282013-10-01 14:20:41 +0000724 uint64_t Imm = MI->getOperand(2).getImm() << And.ImmLSB;
725 // AND IMMEDIATE leaves the other bits of the register unchanged.
726 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
727 unsigned Start, End;
728 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
729 unsigned NewOpcode;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000730 if (And.RegSize == 64) {
Richard Sandiford70284282013-10-01 14:20:41 +0000731 NewOpcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000732 // Prefer RISBGN if available, since it does not clobber CC.
733 if (STI.hasMiscellaneousExtensions())
734 NewOpcode = SystemZ::RISBGN;
735 } else {
Richard Sandiford70284282013-10-01 14:20:41 +0000736 NewOpcode = SystemZ::RISBMux;
737 Start &= 31;
738 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000739 }
Richard Sandiford70284282013-10-01 14:20:41 +0000740 MachineOperand &Dest = MI->getOperand(0);
741 MachineOperand &Src = MI->getOperand(1);
742 MachineInstrBuilder MIB =
743 BuildMI(*MBB, MI, MI->getDebugLoc(), get(NewOpcode))
744 .addOperand(Dest).addReg(0)
745 .addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg())
746 .addImm(Start).addImm(End + 128).addImm(0);
747 return finishConvertToThreeAddress(MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000748 }
749 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000750 return nullptr;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000751}
752
Keno Fischere70b31f2015-06-08 20:09:58 +0000753MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
754 MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
755 MachineBasicBlock::iterator InsertPt, int FrameIndex) const {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000756 const MachineFrameInfo *MFI = MF.getFrameInfo();
757 unsigned Size = MFI->getObjectSize(FrameIndex);
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000758 unsigned Opcode = MI->getOpcode();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000759
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000760 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
761 if ((Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
762 isInt<8>(MI->getOperand(2).getImm()) &&
763 !MI->getOperand(3).getReg()) {
764 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
Keno Fischere70b31f2015-06-08 20:09:58 +0000765 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
766 get(SystemZ::AGSI))
767 .addFrameIndex(FrameIndex)
768 .addImm(0)
769 .addImm(MI->getOperand(2).getImm());
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000770 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000771 return nullptr;
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000772 }
773
774 // All other cases require a single operand.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000775 if (Ops.size() != 1)
Craig Topper062a2ba2014-04-25 05:30:21 +0000776 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000777
778 unsigned OpNum = Ops[0];
NAKAMURA Takumiddcba562013-07-03 02:20:49 +0000779 assert(Size == MF.getRegInfo()
780 .getRegClass(MI->getOperand(OpNum).getReg())->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000781 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000782
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000783 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) &&
784 OpNum == 0 &&
785 isInt<8>(MI->getOperand(2).getImm())) {
786 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
787 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
Keno Fischere70b31f2015-06-08 20:09:58 +0000788 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
789 get(Opcode))
790 .addFrameIndex(FrameIndex)
791 .addImm(0)
792 .addImm(MI->getOperand(2).getImm());
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000793 }
794
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000795 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
796 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
797 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
798 // If we're spilling the destination of an LDGR or LGDR, store the
799 // source register instead.
800 if (OpNum == 0) {
801 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
Keno Fischere70b31f2015-06-08 20:09:58 +0000802 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
803 get(StoreOpcode))
804 .addOperand(MI->getOperand(1))
805 .addFrameIndex(FrameIndex)
806 .addImm(0)
807 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000808 }
809 // If we're spilling the source of an LDGR or LGDR, load the
810 // destination register instead.
811 if (OpNum == 1) {
812 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
813 unsigned Dest = MI->getOperand(0).getReg();
Keno Fischere70b31f2015-06-08 20:09:58 +0000814 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
815 get(LoadOpcode), Dest)
816 .addFrameIndex(FrameIndex)
817 .addImm(0)
818 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000819 }
820 }
821
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000822 // Look for cases where the source of a simple store or the destination
823 // of a simple load is being spilled. Try to use MVC instead.
824 //
825 // Although MVC is in practice a fast choice in these cases, it is still
826 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +0000827 // load or store is volatile. We also wouldn't be able to use MVC if
828 // the two memories partially overlap, but that case cannot occur here,
829 // because we know that one of the memories is a full frame index.
830 //
831 // For performance reasons, we also want to avoid using MVC if the addresses
832 // might be equal. We don't worry about that case here, because spill slot
833 // coloring happens later, and because we have special code to remove
834 // MVCs that turn out to be redundant.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000835 if (OpNum == 0 && MI->hasOneMemOperand()) {
836 MachineMemOperand *MMO = *MI->memoperands_begin();
837 if (MMO->getSize() == Size && !MMO->isVolatile()) {
838 // Handle conversion of loads.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000839 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXLoad)) {
Keno Fischere70b31f2015-06-08 20:09:58 +0000840 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
841 get(SystemZ::MVC))
842 .addFrameIndex(FrameIndex)
843 .addImm(0)
844 .addImm(Size)
845 .addOperand(MI->getOperand(1))
846 .addImm(MI->getOperand(2).getImm())
847 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000848 }
849 // Handle conversion of stores.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000850 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXStore)) {
Keno Fischere70b31f2015-06-08 20:09:58 +0000851 return BuildMI(*InsertPt->getParent(), InsertPt, MI->getDebugLoc(),
852 get(SystemZ::MVC))
853 .addOperand(MI->getOperand(1))
854 .addImm(MI->getOperand(2).getImm())
855 .addImm(Size)
856 .addFrameIndex(FrameIndex)
857 .addImm(0)
858 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000859 }
860 }
861 }
862
Richard Sandiforded1fab62013-07-03 10:10:02 +0000863 // If the spilled operand is the final one, try to change <INSN>R
864 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000865 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000866 if (MemOpcode >= 0) {
867 unsigned NumOps = MI->getNumExplicitOperands();
868 if (OpNum == NumOps - 1) {
869 const MCInstrDesc &MemDesc = get(MemOpcode);
870 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
871 assert(AccessBytes != 0 && "Size of access should be known");
872 assert(AccessBytes <= Size && "Access outside the frame index");
873 uint64_t Offset = Size - AccessBytes;
Keno Fischere70b31f2015-06-08 20:09:58 +0000874 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
875 MI->getDebugLoc(), get(MemOpcode));
Richard Sandiforded1fab62013-07-03 10:10:02 +0000876 for (unsigned I = 0; I < OpNum; ++I)
877 MIB.addOperand(MI->getOperand(I));
878 MIB.addFrameIndex(FrameIndex).addImm(Offset);
879 if (MemDesc.TSFlags & SystemZII::HasIndex)
880 MIB.addReg(0);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000881 return MIB;
882 }
883 }
884
Craig Topper062a2ba2014-04-25 05:30:21 +0000885 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000886}
887
Keno Fischere70b31f2015-06-08 20:09:58 +0000888MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
889 MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
890 MachineBasicBlock::iterator InsertPt, MachineInstr *LoadMI) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000891 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000892}
893
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000894bool
895SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
896 switch (MI->getOpcode()) {
897 case SystemZ::L128:
898 splitMove(MI, SystemZ::LG);
899 return true;
900
901 case SystemZ::ST128:
902 splitMove(MI, SystemZ::STG);
903 return true;
904
905 case SystemZ::LX:
906 splitMove(MI, SystemZ::LD);
907 return true;
908
909 case SystemZ::STX:
910 splitMove(MI, SystemZ::STD);
911 return true;
912
Richard Sandiford89e160d2013-10-01 12:11:47 +0000913 case SystemZ::LBMux:
914 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
915 return true;
916
917 case SystemZ::LHMux:
918 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
919 return true;
920
Richard Sandiford21235a22013-10-01 12:49:07 +0000921 case SystemZ::LLCRMux:
922 expandZExtPseudo(MI, SystemZ::LLCR, 8);
923 return true;
924
925 case SystemZ::LLHRMux:
926 expandZExtPseudo(MI, SystemZ::LLHR, 16);
927 return true;
928
Richard Sandiford0d46b1a2013-10-01 12:19:08 +0000929 case SystemZ::LLCMux:
930 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
931 return true;
932
933 case SystemZ::LLHMux:
934 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
935 return true;
936
Richard Sandiford0755c932013-10-01 11:26:28 +0000937 case SystemZ::LMux:
938 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
939 return true;
940
Richard Sandiford5469c392013-10-01 12:22:49 +0000941 case SystemZ::STCMux:
942 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
943 return true;
944
945 case SystemZ::STHMux:
946 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
947 return true;
948
Richard Sandiford0755c932013-10-01 11:26:28 +0000949 case SystemZ::STMux:
950 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
951 return true;
952
Richard Sandiford01240232013-10-01 13:02:28 +0000953 case SystemZ::LHIMux:
954 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
955 return true;
956
957 case SystemZ::IIFMux:
958 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
959 return true;
960
Richard Sandiford1a569312013-10-01 13:18:56 +0000961 case SystemZ::IILMux:
962 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
963 return true;
964
965 case SystemZ::IIHMux:
966 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
967 return true;
968
Richard Sandiford70284282013-10-01 14:20:41 +0000969 case SystemZ::NIFMux:
970 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
971 return true;
972
973 case SystemZ::NILMux:
974 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
975 return true;
976
977 case SystemZ::NIHMux:
978 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
979 return true;
980
Richard Sandiford6e96ac62013-10-01 13:22:41 +0000981 case SystemZ::OIFMux:
982 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
983 return true;
984
985 case SystemZ::OILMux:
986 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
987 return true;
988
989 case SystemZ::OIHMux:
990 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
991 return true;
992
Richard Sandiford5718dac2013-10-01 14:08:44 +0000993 case SystemZ::XIFMux:
994 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
995 return true;
996
Richard Sandiford2cac7632013-10-01 14:41:52 +0000997 case SystemZ::TMLMux:
998 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
999 return true;
1000
1001 case SystemZ::TMHMux:
1002 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1003 return true;
1004
Richard Sandiford42a694f2013-10-01 14:53:46 +00001005 case SystemZ::AHIMux:
1006 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1007 return true;
1008
1009 case SystemZ::AHIMuxK:
1010 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1011 return true;
1012
1013 case SystemZ::AFIMux:
1014 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1015 return true;
1016
Richard Sandiforda9ac0e02013-10-01 14:56:23 +00001017 case SystemZ::CFIMux:
1018 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1019 return true;
1020
1021 case SystemZ::CLFIMux:
1022 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1023 return true;
1024
Richard Sandifordb63e3002013-10-01 15:00:44 +00001025 case SystemZ::CMux:
1026 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1027 return true;
1028
1029 case SystemZ::CLMux:
1030 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1031 return true;
1032
Richard Sandiford70284282013-10-01 14:20:41 +00001033 case SystemZ::RISBMux: {
1034 bool DestIsHigh = isHighReg(MI->getOperand(0).getReg());
1035 bool SrcIsHigh = isHighReg(MI->getOperand(2).getReg());
1036 if (SrcIsHigh == DestIsHigh)
1037 MI->setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
1038 else {
1039 MI->setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1040 MI->getOperand(5).setImm(MI->getOperand(5).getImm() ^ 32);
1041 }
1042 return true;
1043 }
1044
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001045 case SystemZ::ADJDYNALLOC:
1046 splitAdjDynAlloc(MI);
1047 return true;
1048
1049 default:
1050 return false;
1051 }
1052}
1053
Richard Sandiford312425f2013-05-20 14:23:08 +00001054uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
1055 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
1056 const MachineFunction *MF = MI->getParent()->getParent();
1057 const char *AsmStr = MI->getOperand(0).getSymbolName();
1058 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1059 }
1060 return MI->getDesc().getSize();
1061}
1062
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001063SystemZII::Branch
1064SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001065 switch (MI->getOpcode()) {
1066 case SystemZ::BR:
1067 case SystemZ::J:
1068 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001069 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Richard Sandiford3d768e32013-07-31 12:30:20 +00001070 SystemZ::CCMASK_ANY, &MI->getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001071
1072 case SystemZ::BRC:
1073 case SystemZ::BRCL:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001074 return SystemZII::Branch(SystemZII::BranchNormal,
Richard Sandiford3d768e32013-07-31 12:30:20 +00001075 MI->getOperand(0).getImm(),
1076 MI->getOperand(1).getImm(), &MI->getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001077
Richard Sandifordc2121252013-08-05 11:23:46 +00001078 case SystemZ::BRCT:
1079 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
1080 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
1081
1082 case SystemZ::BRCTG:
1083 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
1084 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
1085
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001086 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001087 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001088 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
1089 MI->getOperand(2).getImm(), &MI->getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001090
Richard Sandiford93183ee2013-09-18 09:56:40 +00001091 case SystemZ::CLIJ:
1092 case SystemZ::CLRJ:
1093 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
1094 MI->getOperand(2).getImm(), &MI->getOperand(3));
1095
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001096 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001097 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001098 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
1099 MI->getOperand(2).getImm(), &MI->getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001100
Richard Sandiford93183ee2013-09-18 09:56:40 +00001101 case SystemZ::CLGIJ:
1102 case SystemZ::CLGRJ:
1103 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
1104 MI->getOperand(2).getImm(), &MI->getOperand(3));
1105
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001106 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001107 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001108 }
1109}
1110
1111void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1112 unsigned &LoadOpcode,
1113 unsigned &StoreOpcode) const {
1114 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1115 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001116 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001117 } else if (RC == &SystemZ::GRH32BitRegClass) {
1118 LoadOpcode = SystemZ::LFH;
1119 StoreOpcode = SystemZ::STFH;
1120 } else if (RC == &SystemZ::GRX32BitRegClass) {
1121 LoadOpcode = SystemZ::LMux;
1122 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001123 } else if (RC == &SystemZ::GR64BitRegClass ||
1124 RC == &SystemZ::ADDR64BitRegClass) {
1125 LoadOpcode = SystemZ::LG;
1126 StoreOpcode = SystemZ::STG;
1127 } else if (RC == &SystemZ::GR128BitRegClass ||
1128 RC == &SystemZ::ADDR128BitRegClass) {
1129 LoadOpcode = SystemZ::L128;
1130 StoreOpcode = SystemZ::ST128;
1131 } else if (RC == &SystemZ::FP32BitRegClass) {
1132 LoadOpcode = SystemZ::LE;
1133 StoreOpcode = SystemZ::STE;
1134 } else if (RC == &SystemZ::FP64BitRegClass) {
1135 LoadOpcode = SystemZ::LD;
1136 StoreOpcode = SystemZ::STD;
1137 } else if (RC == &SystemZ::FP128BitRegClass) {
1138 LoadOpcode = SystemZ::LX;
1139 StoreOpcode = SystemZ::STX;
Ulrich Weigand49506d72015-05-05 19:28:34 +00001140 } else if (RC == &SystemZ::VR32BitRegClass) {
1141 LoadOpcode = SystemZ::VL32;
1142 StoreOpcode = SystemZ::VST32;
1143 } else if (RC == &SystemZ::VR64BitRegClass) {
1144 LoadOpcode = SystemZ::VL64;
1145 StoreOpcode = SystemZ::VST64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001146 } else if (RC == &SystemZ::VF128BitRegClass ||
1147 RC == &SystemZ::VR128BitRegClass) {
1148 LoadOpcode = SystemZ::VL;
1149 StoreOpcode = SystemZ::VST;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001150 } else
1151 llvm_unreachable("Unsupported regclass to load or store");
1152}
1153
1154unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1155 int64_t Offset) const {
1156 const MCInstrDesc &MCID = get(Opcode);
1157 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1158 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1159 // Get the instruction to use for unsigned 12-bit displacements.
1160 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1161 if (Disp12Opcode >= 0)
1162 return Disp12Opcode;
1163
1164 // All address-related instructions can use unsigned 12-bit
1165 // displacements.
1166 return Opcode;
1167 }
1168 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1169 // Get the instruction to use for signed 20-bit displacements.
1170 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1171 if (Disp20Opcode >= 0)
1172 return Disp20Opcode;
1173
1174 // Check whether Opcode allows signed 20-bit displacements.
1175 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1176 return Opcode;
1177 }
1178 return 0;
1179}
1180
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001181unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1182 switch (Opcode) {
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001183 case SystemZ::L: return SystemZ::LT;
1184 case SystemZ::LY: return SystemZ::LT;
1185 case SystemZ::LG: return SystemZ::LTG;
1186 case SystemZ::LGF: return SystemZ::LTGF;
1187 case SystemZ::LR: return SystemZ::LTR;
1188 case SystemZ::LGFR: return SystemZ::LTGFR;
1189 case SystemZ::LGR: return SystemZ::LTGR;
1190 case SystemZ::LER: return SystemZ::LTEBR;
1191 case SystemZ::LDR: return SystemZ::LTDBR;
1192 case SystemZ::LXR: return SystemZ::LTXBR;
Jonas Paulsson12629322015-10-01 18:12:28 +00001193 case SystemZ::LCDFR: return SystemZ::LCDBR;
1194 case SystemZ::LPDFR: return SystemZ::LPDBR;
1195 case SystemZ::LNDFR: return SystemZ::LNDBR;
1196 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1197 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1198 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001199 // On zEC12 we prefer to use RISBGN. But if there is a chance to
1200 // actually use the condition code, we may turn it back into RISGB.
1201 // Note that RISBG is not really a "load-and-test" instruction,
1202 // but sets the same condition code values, so is OK to use here.
1203 case SystemZ::RISBGN: return SystemZ::RISBG;
1204 default: return 0;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001205 }
1206}
1207
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001208// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1209// have already been filtered out. Store the first set bit in LSB and
1210// the number of set bits in Length if so.
1211static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1212 unsigned First = findFirstSet(Mask);
1213 uint64_t Top = (Mask >> First) + 1;
1214 if ((Top & -Top) == Top) {
1215 LSB = First;
1216 Length = findFirstSet(Top);
1217 return true;
1218 }
1219 return false;
1220}
1221
1222bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1223 unsigned &Start, unsigned &End) const {
1224 // Reject trivial all-zero masks.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001225 Mask &= allOnes(BitSize);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001226 if (Mask == 0)
1227 return false;
1228
1229 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1230 // the msb and End specifies the index of the lsb.
1231 unsigned LSB, Length;
1232 if (isStringOfOnes(Mask, LSB, Length)) {
1233 Start = 63 - (LSB + Length - 1);
1234 End = 63 - LSB;
1235 return true;
1236 }
1237
1238 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1239 // of the low 1s and End specifies the lsb of the high 1s.
1240 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1241 assert(LSB > 0 && "Bottom bit must be set");
1242 assert(LSB + Length < BitSize && "Top bit must be set");
1243 Start = 63 - (LSB - 1);
1244 End = 63 - (LSB + Length);
1245 return true;
1246 }
1247
1248 return false;
1249}
1250
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001251unsigned SystemZInstrInfo::getCompareAndBranch(unsigned Opcode,
1252 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001253 switch (Opcode) {
1254 case SystemZ::CR:
1255 return SystemZ::CRJ;
1256 case SystemZ::CGR:
1257 return SystemZ::CGRJ;
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001258 case SystemZ::CHI:
1259 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CIJ : 0;
1260 case SystemZ::CGHI:
1261 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CGIJ : 0;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001262 case SystemZ::CLR:
1263 return SystemZ::CLRJ;
1264 case SystemZ::CLGR:
1265 return SystemZ::CLGRJ;
1266 case SystemZ::CLFI:
1267 return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLIJ : 0;
1268 case SystemZ::CLGFI:
1269 return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLGIJ : 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001270 default:
1271 return 0;
1272 }
1273}
1274
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001275void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1276 MachineBasicBlock::iterator MBBI,
1277 unsigned Reg, uint64_t Value) const {
1278 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1279 unsigned Opcode;
1280 if (isInt<16>(Value))
1281 Opcode = SystemZ::LGHI;
1282 else if (SystemZ::isImmLL(Value))
1283 Opcode = SystemZ::LLILL;
1284 else if (SystemZ::isImmLH(Value)) {
1285 Opcode = SystemZ::LLILH;
1286 Value >>= 16;
1287 } else {
1288 assert(isInt<32>(Value) && "Huge values not handled yet");
1289 Opcode = SystemZ::LGFI;
1290 }
1291 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1292}