blob: 57054891247b10db648834f6b45de6103359de80 [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"
Richard Sandifordff6c5a52013-07-19 16:12:08 +000015#include "SystemZTargetMachine.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000016#include "SystemZInstrBuilder.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
20#define GET_INSTRINFO_CTOR
21#define GET_INSTRMAP_INFO
22#include "SystemZGenInstrInfo.inc"
23
24using namespace llvm;
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
Ulrich Weigand5f613df2013-05-06 16:15:19 +000040SystemZInstrInfo::SystemZInstrInfo(SystemZTargetMachine &tm)
41 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
Richard Sandifordff6c5a52013-07-19 16:12:08 +000042 RI(tm), TM(tm) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000043}
44
45// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
46// each having the opcode given by NewOpcode.
47void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
48 unsigned NewOpcode) const {
49 MachineBasicBlock *MBB = MI->getParent();
50 MachineFunction &MF = *MBB->getParent();
51
52 // Get two load or store instructions. Use the original instruction for one
53 // of them (arbitarily the second here) and create a clone for the other.
54 MachineInstr *EarlierMI = MF.CloneMachineInstr(MI);
55 MBB->insert(MI, EarlierMI);
56
57 // Set up the two 64-bit registers.
58 MachineOperand &HighRegOp = EarlierMI->getOperand(0);
59 MachineOperand &LowRegOp = MI->getOperand(0);
Richard Sandiford87a44362013-09-30 10:28:35 +000060 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64));
61 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64));
Ulrich Weigand5f613df2013-05-06 16:15:19 +000062
63 // The address in the first (high) instruction is already correct.
64 // Adjust the offset in the second (low) instruction.
65 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
66 MachineOperand &LowOffsetOp = MI->getOperand(2);
67 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
68
69 // Set the opcodes.
70 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
71 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
72 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
73
74 EarlierMI->setDesc(get(HighOpcode));
75 MI->setDesc(get(LowOpcode));
76}
77
78// Split ADJDYNALLOC instruction MI.
79void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
80 MachineBasicBlock *MBB = MI->getParent();
81 MachineFunction &MF = *MBB->getParent();
82 MachineFrameInfo *MFFrame = MF.getFrameInfo();
83 MachineOperand &OffsetMO = MI->getOperand(2);
84
85 uint64_t Offset = (MFFrame->getMaxCallFrameSize() +
86 SystemZMC::CallFrameSize +
87 OffsetMO.getImm());
88 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
89 assert(NewOpcode && "No support for huge argument lists yet");
90 MI->setDesc(get(NewOpcode));
91 OffsetMO.setImm(Offset);
92}
93
Richard Sandiford01240232013-10-01 13:02:28 +000094// MI is an RI-style pseudo instruction. Replace it with LowOpcode
95// if the first operand is a low GR32 and HighOpcode if the first operand
96// is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
97// and HighOpcode takes an unsigned 32-bit operand. In those cases,
98// MI has the same kind of operand as LowOpcode, so needs to be converted
99// if HighOpcode is used.
100void SystemZInstrInfo::expandRIPseudo(MachineInstr *MI, unsigned LowOpcode,
101 unsigned HighOpcode,
102 bool ConvertHigh) const {
103 unsigned Reg = MI->getOperand(0).getReg();
104 bool IsHigh = isHighReg(Reg);
105 MI->setDesc(get(IsHigh ? HighOpcode : LowOpcode));
106 if (IsHigh && ConvertHigh)
107 MI->getOperand(1).setImm(uint32_t(MI->getOperand(1).getImm()));
108}
109
Richard Sandiford0755c932013-10-01 11:26:28 +0000110// MI is an RXY-style pseudo instruction. Replace it with LowOpcode
111// if the first operand is a low GR32 and HighOpcode if the first operand
112// is a high GR32.
113void SystemZInstrInfo::expandRXYPseudo(MachineInstr *MI, unsigned LowOpcode,
114 unsigned HighOpcode) const {
115 unsigned Reg = MI->getOperand(0).getReg();
116 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
117 MI->getOperand(2).getImm());
118 MI->setDesc(get(Opcode));
119}
120
Richard Sandiford21235a22013-10-01 12:49:07 +0000121// MI is an RR-style pseudo instruction that zero-extends the low Size bits
122// of one GRX32 into another. Replace it with LowOpcode if both operands
123// are low registers, otherwise use RISB[LH]G.
124void SystemZInstrInfo::expandZExtPseudo(MachineInstr *MI, unsigned LowOpcode,
125 unsigned Size) const {
126 emitGRX32Move(*MI->getParent(), MI, MI->getDebugLoc(),
127 MI->getOperand(0).getReg(), MI->getOperand(1).getReg(),
128 LowOpcode, Size, MI->getOperand(1).isKill());
129 MI->eraseFromParent();
130}
131
Richard Sandiford0755c932013-10-01 11:26:28 +0000132// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
133// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
134// are low registers, otherwise use RISB[LH]G. Size is the number of bits
135// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
136// KillSrc is true if this move is the last use of SrcReg.
137void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
138 MachineBasicBlock::iterator MBBI,
139 DebugLoc DL, unsigned DestReg,
140 unsigned SrcReg, unsigned LowLowOpcode,
141 unsigned Size, bool KillSrc) const {
142 unsigned Opcode;
143 bool DestIsHigh = isHighReg(DestReg);
144 bool SrcIsHigh = isHighReg(SrcReg);
145 if (DestIsHigh && SrcIsHigh)
146 Opcode = SystemZ::RISBHH;
147 else if (DestIsHigh && !SrcIsHigh)
148 Opcode = SystemZ::RISBHL;
149 else if (!DestIsHigh && SrcIsHigh)
150 Opcode = SystemZ::RISBLH;
151 else {
152 BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
153 .addReg(SrcReg, getKillRegState(KillSrc));
154 return;
155 }
156 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
157 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
158 .addReg(DestReg, RegState::Undef)
159 .addReg(SrcReg, getKillRegState(KillSrc))
160 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
161}
162
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000163// If MI is a simple load or store for a frame object, return the register
164// it loads or stores and set FrameIndex to the index of the frame object.
165// Return 0 otherwise.
166//
167// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000168static int isSimpleMove(const MachineInstr *MI, int &FrameIndex,
169 unsigned Flag) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000170 const MCInstrDesc &MCID = MI->getDesc();
171 if ((MCID.TSFlags & Flag) &&
172 MI->getOperand(1).isFI() &&
173 MI->getOperand(2).getImm() == 0 &&
174 MI->getOperand(3).getReg() == 0) {
175 FrameIndex = MI->getOperand(1).getIndex();
176 return MI->getOperand(0).getReg();
177 }
178 return 0;
179}
180
181unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
182 int &FrameIndex) const {
183 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
184}
185
186unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
187 int &FrameIndex) const {
188 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
189}
190
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000191bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr *MI,
192 int &DestFrameIndex,
193 int &SrcFrameIndex) const {
194 // Check for MVC 0(Length,FI1),0(FI2)
195 const MachineFrameInfo *MFI = MI->getParent()->getParent()->getFrameInfo();
196 if (MI->getOpcode() != SystemZ::MVC ||
197 !MI->getOperand(0).isFI() ||
198 MI->getOperand(1).getImm() != 0 ||
199 !MI->getOperand(3).isFI() ||
200 MI->getOperand(4).getImm() != 0)
201 return false;
202
203 // Check that Length covers the full slots.
204 int64_t Length = MI->getOperand(2).getImm();
205 unsigned FI1 = MI->getOperand(0).getIndex();
206 unsigned FI2 = MI->getOperand(3).getIndex();
207 if (MFI->getObjectSize(FI1) != Length ||
208 MFI->getObjectSize(FI2) != Length)
209 return false;
210
211 DestFrameIndex = FI1;
212 SrcFrameIndex = FI2;
213 return true;
214}
215
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000216bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
217 MachineBasicBlock *&TBB,
218 MachineBasicBlock *&FBB,
219 SmallVectorImpl<MachineOperand> &Cond,
220 bool AllowModify) const {
221 // Most of the code and comments here are boilerplate.
222
223 // Start from the bottom of the block and work up, examining the
224 // terminator instructions.
225 MachineBasicBlock::iterator I = MBB.end();
226 while (I != MBB.begin()) {
227 --I;
228 if (I->isDebugValue())
229 continue;
230
231 // Working from the bottom, when we see a non-terminator instruction, we're
232 // done.
233 if (!isUnpredicatedTerminator(I))
234 break;
235
236 // A terminator that isn't a branch can't easily be handled by this
237 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000238 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000239 return true;
240
241 // Can't handle indirect branches.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000242 SystemZII::Branch Branch(getBranchInfo(I));
243 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000244 return true;
245
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000246 // Punt on compound branches.
247 if (Branch.Type != SystemZII::BranchNormal)
248 return true;
249
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000250 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000251 // Handle unconditional branches.
252 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000253 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000254 continue;
255 }
256
257 // If the block has any instructions after a JMP, delete them.
258 while (llvm::next(I) != MBB.end())
259 llvm::next(I)->eraseFromParent();
260
261 Cond.clear();
262 FBB = 0;
263
264 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000265 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000266 TBB = 0;
267 I->eraseFromParent();
268 I = MBB.end();
269 continue;
270 }
271
272 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000273 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000274 continue;
275 }
276
277 // Working from the bottom, handle the first conditional branch.
278 if (Cond.empty()) {
279 // FIXME: add X86-style branch swap
280 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000281 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000282 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000283 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000284 continue;
285 }
286
287 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000288 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000289
290 // Only handle the case where all conditional branches branch to the same
291 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000292 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000293 return true;
294
295 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000296 unsigned OldCCValid = Cond[0].getImm();
297 unsigned OldCCMask = Cond[1].getImm();
298 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000299 continue;
300
301 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000302 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000303 }
304
305 return false;
306}
307
308unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
309 // Most of the code and comments here are boilerplate.
310 MachineBasicBlock::iterator I = MBB.end();
311 unsigned Count = 0;
312
313 while (I != MBB.begin()) {
314 --I;
315 if (I->isDebugValue())
316 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000317 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000318 break;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000319 if (!getBranchInfo(I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000320 break;
321 // Remove the branch.
322 I->eraseFromParent();
323 I = MBB.end();
324 ++Count;
325 }
326
327 return Count;
328}
329
Richard Sandiford3d768e32013-07-31 12:30:20 +0000330bool SystemZInstrInfo::
331ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
332 assert(Cond.size() == 2 && "Invalid condition");
333 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
334 return false;
335}
336
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000337unsigned
338SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
339 MachineBasicBlock *FBB,
340 const SmallVectorImpl<MachineOperand> &Cond,
341 DebugLoc DL) const {
342 // In this function we output 32-bit branches, which should always
343 // have enough range. They can be shortened and relaxed by later code
344 // in the pipeline, if desired.
345
346 // Shouldn't be a fall through.
347 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000348 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000349 "SystemZ branch conditions have one component!");
350
351 if (Cond.empty()) {
352 // Unconditional branch?
353 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000354 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000355 return 1;
356 }
357
358 // Conditional branch.
359 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000360 unsigned CCValid = Cond[0].getImm();
361 unsigned CCMask = Cond[1].getImm();
362 BuildMI(&MBB, DL, get(SystemZ::BRC))
363 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000364 ++Count;
365
366 if (FBB) {
367 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000368 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000369 ++Count;
370 }
371 return Count;
372}
373
Richard Sandiford564681c2013-08-12 10:28:10 +0000374bool SystemZInstrInfo::analyzeCompare(const MachineInstr *MI,
375 unsigned &SrcReg, unsigned &SrcReg2,
376 int &Mask, int &Value) const {
377 assert(MI->isCompare() && "Caller should have checked for a comparison");
378
379 if (MI->getNumExplicitOperands() == 2 &&
380 MI->getOperand(0).isReg() &&
381 MI->getOperand(1).isImm()) {
382 SrcReg = MI->getOperand(0).getReg();
383 SrcReg2 = 0;
384 Value = MI->getOperand(1).getImm();
385 Mask = ~0;
386 return true;
387 }
388
389 return false;
390}
391
Richard Sandiforda5901252013-08-16 10:22:54 +0000392// If Reg is a virtual register, return its definition, otherwise return null.
393static MachineInstr *getDef(unsigned Reg,
394 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000395 if (TargetRegisterInfo::isPhysicalRegister(Reg))
396 return 0;
Richard Sandiford564681c2013-08-12 10:28:10 +0000397 return MRI->getUniqueVRegDef(Reg);
398}
399
400// Return true if MI is a shift of type Opcode by Imm bits.
401static bool isShift(MachineInstr *MI, int Opcode, int64_t Imm) {
402 return (MI->getOpcode() == Opcode &&
403 !MI->getOperand(2).getReg() &&
404 MI->getOperand(3).getImm() == Imm);
405}
406
Richard Sandiforda5901252013-08-16 10:22:54 +0000407// If the destination of MI has no uses, delete it as dead.
408static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
409 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
410 MI->eraseFromParent();
411}
412
Richard Sandiford564681c2013-08-12 10:28:10 +0000413// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000414// the result of an IPM sequence whose input CC survives until Compare,
415// and whether Compare is therefore redundant. Delete it and return
416// true if so.
417static bool removeIPMBasedCompare(MachineInstr *Compare, unsigned SrcReg,
418 const MachineRegisterInfo *MRI,
419 const TargetRegisterInfo *TRI) {
Richard Sandiforde3827752013-08-16 10:55:47 +0000420 MachineInstr *LGFR = 0;
Richard Sandiforda5901252013-08-16 10:22:54 +0000421 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000422 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
423 LGFR = RLL;
424 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
425 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000426 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000427 return false;
428
Richard Sandiforda5901252013-08-16 10:22:54 +0000429 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
430 if (!SRL || !isShift(SRL, SystemZ::SRL, 28))
Richard Sandiford564681c2013-08-12 10:28:10 +0000431 return false;
432
Richard Sandiforda5901252013-08-16 10:22:54 +0000433 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000434 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
435 return false;
436
437 // Check that there are no assignments to CC between the IPM and Compare,
Richard Sandiford564681c2013-08-12 10:28:10 +0000438 if (IPM->getParent() != Compare->getParent())
439 return false;
440 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare;
441 for (++MBBI; MBBI != MBBE; ++MBBI) {
442 MachineInstr *MI = MBBI;
Richard Sandiforda5901252013-08-16 10:22:54 +0000443 if (MI->modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000444 return false;
445 }
446
Richard Sandiford564681c2013-08-12 10:28:10 +0000447 Compare->eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000448 if (LGFR)
449 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000450 eraseIfDead(RLL, MRI);
451 eraseIfDead(SRL, MRI);
452 eraseIfDead(IPM, MRI);
453
Richard Sandiford564681c2013-08-12 10:28:10 +0000454 return true;
455}
456
457bool
458SystemZInstrInfo::optimizeCompareInstr(MachineInstr *Compare,
459 unsigned SrcReg, unsigned SrcReg2,
460 int Mask, int Value,
461 const MachineRegisterInfo *MRI) const {
462 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
463 bool IsLogical = (Compare->getDesc().TSFlags & SystemZII::IsLogical) != 0;
464 if (Value == 0 &&
465 !IsLogical &&
Richard Sandiforda5901252013-08-16 10:22:54 +0000466 removeIPMBasedCompare(Compare, SrcReg, MRI, TM.getRegisterInfo()))
Richard Sandiford564681c2013-08-12 10:28:10 +0000467 return true;
468 return false;
469}
470
Richard Sandifordf2404162013-07-25 09:11:15 +0000471// If Opcode is a move that has a conditional variant, return that variant,
472// otherwise return 0.
473static unsigned getConditionalMove(unsigned Opcode) {
474 switch (Opcode) {
475 case SystemZ::LR: return SystemZ::LOCR;
476 case SystemZ::LGR: return SystemZ::LOCGR;
477 default: return 0;
478 }
479}
480
481bool SystemZInstrInfo::isPredicable(MachineInstr *MI) const {
482 unsigned Opcode = MI->getOpcode();
483 if (TM.getSubtargetImpl()->hasLoadStoreOnCond() &&
484 getConditionalMove(Opcode))
485 return true;
486 return false;
487}
488
489bool SystemZInstrInfo::
490isProfitableToIfCvt(MachineBasicBlock &MBB,
491 unsigned NumCycles, unsigned ExtraPredCycles,
492 const BranchProbability &Probability) const {
493 // For now only convert single instructions.
494 return NumCycles == 1;
495}
496
497bool SystemZInstrInfo::
498isProfitableToIfCvt(MachineBasicBlock &TMBB,
499 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
500 MachineBasicBlock &FMBB,
501 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
502 const BranchProbability &Probability) const {
503 // For now avoid converting mutually-exclusive cases.
504 return false;
505}
506
507bool SystemZInstrInfo::
508PredicateInstruction(MachineInstr *MI,
509 const SmallVectorImpl<MachineOperand> &Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000510 assert(Pred.size() == 2 && "Invalid condition");
511 unsigned CCValid = Pred[0].getImm();
512 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000513 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
514 unsigned Opcode = MI->getOpcode();
515 if (TM.getSubtargetImpl()->hasLoadStoreOnCond()) {
516 if (unsigned CondOpcode = getConditionalMove(Opcode)) {
517 MI->setDesc(get(CondOpcode));
Richard Sandiford3d768e32013-07-31 12:30:20 +0000518 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000519 .addImm(CCValid).addImm(CCMask)
520 .addReg(SystemZ::CC, RegState::Implicit);;
Richard Sandifordf2404162013-07-25 09:11:15 +0000521 return true;
522 }
523 }
524 return false;
525}
526
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000527void
528SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
529 MachineBasicBlock::iterator MBBI, DebugLoc DL,
530 unsigned DestReg, unsigned SrcReg,
531 bool KillSrc) const {
532 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
533 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000534 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
535 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
536 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
537 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000538 return;
539 }
540
Richard Sandiford0755c932013-10-01 11:26:28 +0000541 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
542 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
543 return;
544 }
545
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000546 // Everything else needs only one instruction.
547 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000548 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000549 Opcode = SystemZ::LGR;
550 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
551 Opcode = SystemZ::LER;
552 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
553 Opcode = SystemZ::LDR;
554 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
555 Opcode = SystemZ::LXR;
556 else
557 llvm_unreachable("Impossible reg-to-reg copy");
558
559 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
560 .addReg(SrcReg, getKillRegState(KillSrc));
561}
562
563void
564SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
565 MachineBasicBlock::iterator MBBI,
566 unsigned SrcReg, bool isKill,
567 int FrameIdx,
568 const TargetRegisterClass *RC,
569 const TargetRegisterInfo *TRI) const {
570 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
571
572 // Callers may expect a single instruction, so keep 128-bit moves
573 // together for now and lower them after register allocation.
574 unsigned LoadOpcode, StoreOpcode;
575 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
576 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
577 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
578}
579
580void
581SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
582 MachineBasicBlock::iterator MBBI,
583 unsigned DestReg, int FrameIdx,
584 const TargetRegisterClass *RC,
585 const TargetRegisterInfo *TRI) const {
586 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
587
588 // Callers may expect a single instruction, so keep 128-bit moves
589 // together for now and lower them after register allocation.
590 unsigned LoadOpcode, StoreOpcode;
591 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
592 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
593 FrameIdx);
594}
595
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000596// Return true if MI is a simple load or store with a 12-bit displacement
597// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
598static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
599 const MCInstrDesc &MCID = MI->getDesc();
600 return ((MCID.TSFlags & Flag) &&
601 isUInt<12>(MI->getOperand(2).getImm()) &&
602 MI->getOperand(3).getReg() == 0);
603}
604
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000605namespace {
606 struct LogicOp {
607 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
608 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
609 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
610
611 operator bool() const { return RegSize; }
612
613 unsigned RegSize, ImmLSB, ImmSize;
614 };
615}
616
617static LogicOp interpretAndImmediate(unsigned Opcode) {
618 switch (Opcode) {
Richard Sandiford652784e2013-09-25 11:11:53 +0000619 case SystemZ::NILL: return LogicOp(32, 0, 16);
620 case SystemZ::NILH: return LogicOp(32, 16, 16);
621 case SystemZ::NILL64: return LogicOp(64, 0, 16);
622 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000623 case SystemZ::NIHL: return LogicOp(64, 32, 16);
624 case SystemZ::NIHH: return LogicOp(64, 48, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000625 case SystemZ::NILF: return LogicOp(32, 0, 32);
626 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000627 case SystemZ::NIHF: return LogicOp(64, 32, 32);
628 default: return LogicOp();
629 }
630}
631
632// Used to return from convertToThreeAddress after replacing two-address
633// instruction OldMI with three-address instruction NewMI.
634static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
635 MachineInstr *NewMI,
636 LiveVariables *LV) {
637 if (LV) {
638 unsigned NumOps = OldMI->getNumOperands();
639 for (unsigned I = 1; I < NumOps; ++I) {
640 MachineOperand &Op = OldMI->getOperand(I);
641 if (Op.isReg() && Op.isKill())
642 LV->replaceKillInstruction(Op.getReg(), OldMI, NewMI);
643 }
644 }
645 return NewMI;
646}
647
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000648MachineInstr *
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000649SystemZInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
650 MachineBasicBlock::iterator &MBBI,
651 LiveVariables *LV) const {
652 MachineInstr *MI = MBBI;
653 MachineBasicBlock *MBB = MI->getParent();
654
655 unsigned Opcode = MI->getOpcode();
656 unsigned NumOps = MI->getNumOperands();
657
658 // Try to convert something like SLL into SLLK, if supported.
659 // We prefer to keep the two-operand form where possible both
660 // because it tends to be shorter and because some instructions
661 // have memory forms that can be used during spilling.
662 if (TM.getSubtargetImpl()->hasDistinctOps()) {
663 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
664 if (ThreeOperandOpcode >= 0) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000665 MachineOperand &Dest = MI->getOperand(0);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000666 MachineOperand &Src = MI->getOperand(1);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000667 MachineInstrBuilder MIB =
668 BuildMI(*MBB, MBBI, MI->getDebugLoc(), get(ThreeOperandOpcode))
669 .addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000670 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000671 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000672 // Keep the remaining operands as-is.
673 for (unsigned I = 2; I < NumOps; ++I)
674 MIB.addOperand(MI->getOperand(I));
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000675 return finishConvertToThreeAddress(MI, MIB, LV);
676 }
677 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000678
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000679 // Try to convert an AND into an RISBG-type instruction.
680 if (LogicOp And = interpretAndImmediate(Opcode)) {
681 unsigned NewOpcode;
682 if (And.RegSize == 64)
683 NewOpcode = SystemZ::RISBG;
684 else if (TM.getSubtargetImpl()->hasHighWord())
Richard Sandiford0755c932013-10-01 11:26:28 +0000685 NewOpcode = SystemZ::RISBLL;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000686 else
687 // We can't use RISBG for 32-bit operations because it clobbers the
688 // high word of the destination too.
689 NewOpcode = 0;
690 if (NewOpcode) {
691 uint64_t Imm = MI->getOperand(2).getImm() << And.ImmLSB;
692 // AND IMMEDIATE leaves the other bits of the register unchanged.
693 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
694 unsigned Start, End;
695 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
Richard Sandiford0755c932013-10-01 11:26:28 +0000696 if (NewOpcode == SystemZ::RISBLL) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000697 Start &= 31;
698 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000699 }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000700 MachineOperand &Dest = MI->getOperand(0);
701 MachineOperand &Src = MI->getOperand(1);
702 MachineInstrBuilder MIB =
703 BuildMI(*MBB, MI, MI->getDebugLoc(), get(NewOpcode))
704 .addOperand(Dest).addReg(0)
705 .addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg())
706 .addImm(Start).addImm(End + 128).addImm(0);
707 return finishConvertToThreeAddress(MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000708 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000709 }
710 }
711 return 0;
712}
713
714MachineInstr *
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000715SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
716 MachineInstr *MI,
717 const SmallVectorImpl<unsigned> &Ops,
718 int FrameIndex) const {
719 const MachineFrameInfo *MFI = MF.getFrameInfo();
720 unsigned Size = MFI->getObjectSize(FrameIndex);
721
722 // Eary exit for cases we don't care about
723 if (Ops.size() != 1)
724 return 0;
725
726 unsigned OpNum = Ops[0];
NAKAMURA Takumiddcba562013-07-03 02:20:49 +0000727 assert(Size == MF.getRegInfo()
728 .getRegClass(MI->getOperand(OpNum).getReg())->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000729 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000730
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000731 unsigned Opcode = MI->getOpcode();
732 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
733 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
734 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
735 // If we're spilling the destination of an LDGR or LGDR, store the
736 // source register instead.
737 if (OpNum == 0) {
738 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
739 return BuildMI(MF, MI->getDebugLoc(), get(StoreOpcode))
740 .addOperand(MI->getOperand(1)).addFrameIndex(FrameIndex)
741 .addImm(0).addReg(0);
742 }
743 // If we're spilling the source of an LDGR or LGDR, load the
744 // destination register instead.
745 if (OpNum == 1) {
746 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
747 unsigned Dest = MI->getOperand(0).getReg();
748 return BuildMI(MF, MI->getDebugLoc(), get(LoadOpcode), Dest)
749 .addFrameIndex(FrameIndex).addImm(0).addReg(0);
750 }
751 }
752
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000753 // Look for cases where the source of a simple store or the destination
754 // of a simple load is being spilled. Try to use MVC instead.
755 //
756 // Although MVC is in practice a fast choice in these cases, it is still
757 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +0000758 // load or store is volatile. We also wouldn't be able to use MVC if
759 // the two memories partially overlap, but that case cannot occur here,
760 // because we know that one of the memories is a full frame index.
761 //
762 // For performance reasons, we also want to avoid using MVC if the addresses
763 // might be equal. We don't worry about that case here, because spill slot
764 // coloring happens later, and because we have special code to remove
765 // MVCs that turn out to be redundant.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000766 if (OpNum == 0 && MI->hasOneMemOperand()) {
767 MachineMemOperand *MMO = *MI->memoperands_begin();
768 if (MMO->getSize() == Size && !MMO->isVolatile()) {
769 // Handle conversion of loads.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000770 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXLoad)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000771 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000772 .addFrameIndex(FrameIndex).addImm(0).addImm(Size)
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000773 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000774 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000775 }
776 // Handle conversion of stores.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000777 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXStore)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000778 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
779 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000780 .addImm(Size).addFrameIndex(FrameIndex).addImm(0)
781 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000782 }
783 }
784 }
785
Richard Sandiforded1fab62013-07-03 10:10:02 +0000786 // If the spilled operand is the final one, try to change <INSN>R
787 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000788 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000789 if (MemOpcode >= 0) {
790 unsigned NumOps = MI->getNumExplicitOperands();
791 if (OpNum == NumOps - 1) {
792 const MCInstrDesc &MemDesc = get(MemOpcode);
793 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
794 assert(AccessBytes != 0 && "Size of access should be known");
795 assert(AccessBytes <= Size && "Access outside the frame index");
796 uint64_t Offset = Size - AccessBytes;
Richard Sandiforded1fab62013-07-03 10:10:02 +0000797 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), get(MemOpcode));
798 for (unsigned I = 0; I < OpNum; ++I)
799 MIB.addOperand(MI->getOperand(I));
800 MIB.addFrameIndex(FrameIndex).addImm(Offset);
801 if (MemDesc.TSFlags & SystemZII::HasIndex)
802 MIB.addReg(0);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000803 return MIB;
804 }
805 }
806
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000807 return 0;
808}
809
810MachineInstr *
811SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr* MI,
812 const SmallVectorImpl<unsigned> &Ops,
813 MachineInstr* LoadMI) const {
814 return 0;
815}
816
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000817bool
818SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
819 switch (MI->getOpcode()) {
820 case SystemZ::L128:
821 splitMove(MI, SystemZ::LG);
822 return true;
823
824 case SystemZ::ST128:
825 splitMove(MI, SystemZ::STG);
826 return true;
827
828 case SystemZ::LX:
829 splitMove(MI, SystemZ::LD);
830 return true;
831
832 case SystemZ::STX:
833 splitMove(MI, SystemZ::STD);
834 return true;
835
Richard Sandiford89e160d2013-10-01 12:11:47 +0000836 case SystemZ::LBMux:
837 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
838 return true;
839
840 case SystemZ::LHMux:
841 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
842 return true;
843
Richard Sandiford21235a22013-10-01 12:49:07 +0000844 case SystemZ::LLCRMux:
845 expandZExtPseudo(MI, SystemZ::LLCR, 8);
846 return true;
847
848 case SystemZ::LLHRMux:
849 expandZExtPseudo(MI, SystemZ::LLHR, 16);
850 return true;
851
Richard Sandiford0d46b1a2013-10-01 12:19:08 +0000852 case SystemZ::LLCMux:
853 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
854 return true;
855
856 case SystemZ::LLHMux:
857 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
858 return true;
859
Richard Sandiford0755c932013-10-01 11:26:28 +0000860 case SystemZ::LMux:
861 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
862 return true;
863
Richard Sandiford5469c392013-10-01 12:22:49 +0000864 case SystemZ::STCMux:
865 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
866 return true;
867
868 case SystemZ::STHMux:
869 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
870 return true;
871
Richard Sandiford0755c932013-10-01 11:26:28 +0000872 case SystemZ::STMux:
873 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
874 return true;
875
Richard Sandiford01240232013-10-01 13:02:28 +0000876 case SystemZ::LHIMux:
877 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
878 return true;
879
880 case SystemZ::IIFMux:
881 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
882 return true;
883
Richard Sandiford1a569312013-10-01 13:18:56 +0000884 case SystemZ::IILMux:
885 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
886 return true;
887
888 case SystemZ::IIHMux:
889 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
890 return true;
891
Richard Sandiford6e96ac62013-10-01 13:22:41 +0000892 case SystemZ::OIFMux:
893 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
894 return true;
895
896 case SystemZ::OILMux:
897 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
898 return true;
899
900 case SystemZ::OIHMux:
901 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
902 return true;
903
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000904 case SystemZ::ADJDYNALLOC:
905 splitAdjDynAlloc(MI);
906 return true;
907
908 default:
909 return false;
910 }
911}
912
Richard Sandiford312425f2013-05-20 14:23:08 +0000913uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
914 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
915 const MachineFunction *MF = MI->getParent()->getParent();
916 const char *AsmStr = MI->getOperand(0).getSymbolName();
917 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
918 }
919 return MI->getDesc().getSize();
920}
921
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000922SystemZII::Branch
923SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000924 switch (MI->getOpcode()) {
925 case SystemZ::BR:
926 case SystemZ::J:
927 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000928 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Richard Sandiford3d768e32013-07-31 12:30:20 +0000929 SystemZ::CCMASK_ANY, &MI->getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000930
931 case SystemZ::BRC:
932 case SystemZ::BRCL:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000933 return SystemZII::Branch(SystemZII::BranchNormal,
Richard Sandiford3d768e32013-07-31 12:30:20 +0000934 MI->getOperand(0).getImm(),
935 MI->getOperand(1).getImm(), &MI->getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000936
Richard Sandifordc2121252013-08-05 11:23:46 +0000937 case SystemZ::BRCT:
938 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
939 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
940
941 case SystemZ::BRCTG:
942 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
943 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
944
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000945 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000946 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +0000947 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
948 MI->getOperand(2).getImm(), &MI->getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000949
Richard Sandiford93183ee2013-09-18 09:56:40 +0000950 case SystemZ::CLIJ:
951 case SystemZ::CLRJ:
952 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
953 MI->getOperand(2).getImm(), &MI->getOperand(3));
954
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000955 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000956 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +0000957 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
958 MI->getOperand(2).getImm(), &MI->getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000959
Richard Sandiford93183ee2013-09-18 09:56:40 +0000960 case SystemZ::CLGIJ:
961 case SystemZ::CLGRJ:
962 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
963 MI->getOperand(2).getImm(), &MI->getOperand(3));
964
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000965 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000966 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000967 }
968}
969
970void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
971 unsigned &LoadOpcode,
972 unsigned &StoreOpcode) const {
973 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
974 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +0000975 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +0000976 } else if (RC == &SystemZ::GRH32BitRegClass) {
977 LoadOpcode = SystemZ::LFH;
978 StoreOpcode = SystemZ::STFH;
979 } else if (RC == &SystemZ::GRX32BitRegClass) {
980 LoadOpcode = SystemZ::LMux;
981 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000982 } else if (RC == &SystemZ::GR64BitRegClass ||
983 RC == &SystemZ::ADDR64BitRegClass) {
984 LoadOpcode = SystemZ::LG;
985 StoreOpcode = SystemZ::STG;
986 } else if (RC == &SystemZ::GR128BitRegClass ||
987 RC == &SystemZ::ADDR128BitRegClass) {
988 LoadOpcode = SystemZ::L128;
989 StoreOpcode = SystemZ::ST128;
990 } else if (RC == &SystemZ::FP32BitRegClass) {
991 LoadOpcode = SystemZ::LE;
992 StoreOpcode = SystemZ::STE;
993 } else if (RC == &SystemZ::FP64BitRegClass) {
994 LoadOpcode = SystemZ::LD;
995 StoreOpcode = SystemZ::STD;
996 } else if (RC == &SystemZ::FP128BitRegClass) {
997 LoadOpcode = SystemZ::LX;
998 StoreOpcode = SystemZ::STX;
999 } else
1000 llvm_unreachable("Unsupported regclass to load or store");
1001}
1002
1003unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1004 int64_t Offset) const {
1005 const MCInstrDesc &MCID = get(Opcode);
1006 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1007 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1008 // Get the instruction to use for unsigned 12-bit displacements.
1009 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1010 if (Disp12Opcode >= 0)
1011 return Disp12Opcode;
1012
1013 // All address-related instructions can use unsigned 12-bit
1014 // displacements.
1015 return Opcode;
1016 }
1017 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1018 // Get the instruction to use for signed 20-bit displacements.
1019 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1020 if (Disp20Opcode >= 0)
1021 return Disp20Opcode;
1022
1023 // Check whether Opcode allows signed 20-bit displacements.
1024 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1025 return Opcode;
1026 }
1027 return 0;
1028}
1029
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001030unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1031 switch (Opcode) {
1032 case SystemZ::L: return SystemZ::LT;
1033 case SystemZ::LY: return SystemZ::LT;
1034 case SystemZ::LG: return SystemZ::LTG;
1035 case SystemZ::LGF: return SystemZ::LTGF;
1036 case SystemZ::LR: return SystemZ::LTR;
1037 case SystemZ::LGFR: return SystemZ::LTGFR;
1038 case SystemZ::LGR: return SystemZ::LTGR;
Richard Sandiford0897fce2013-08-07 11:10:06 +00001039 case SystemZ::LER: return SystemZ::LTEBR;
1040 case SystemZ::LDR: return SystemZ::LTDBR;
1041 case SystemZ::LXR: return SystemZ::LTXBR;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001042 default: return 0;
1043 }
1044}
1045
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001046// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1047// have already been filtered out. Store the first set bit in LSB and
1048// the number of set bits in Length if so.
1049static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1050 unsigned First = findFirstSet(Mask);
1051 uint64_t Top = (Mask >> First) + 1;
1052 if ((Top & -Top) == Top) {
1053 LSB = First;
1054 Length = findFirstSet(Top);
1055 return true;
1056 }
1057 return false;
1058}
1059
1060bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1061 unsigned &Start, unsigned &End) const {
1062 // Reject trivial all-zero masks.
1063 if (Mask == 0)
1064 return false;
1065
1066 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1067 // the msb and End specifies the index of the lsb.
1068 unsigned LSB, Length;
1069 if (isStringOfOnes(Mask, LSB, Length)) {
1070 Start = 63 - (LSB + Length - 1);
1071 End = 63 - LSB;
1072 return true;
1073 }
1074
1075 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1076 // of the low 1s and End specifies the lsb of the high 1s.
1077 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1078 assert(LSB > 0 && "Bottom bit must be set");
1079 assert(LSB + Length < BitSize && "Top bit must be set");
1080 Start = 63 - (LSB - 1);
1081 End = 63 - (LSB + Length);
1082 return true;
1083 }
1084
1085 return false;
1086}
1087
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001088unsigned SystemZInstrInfo::getCompareAndBranch(unsigned Opcode,
1089 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001090 switch (Opcode) {
1091 case SystemZ::CR:
1092 return SystemZ::CRJ;
1093 case SystemZ::CGR:
1094 return SystemZ::CGRJ;
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001095 case SystemZ::CHI:
1096 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CIJ : 0;
1097 case SystemZ::CGHI:
1098 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CGIJ : 0;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001099 case SystemZ::CLR:
1100 return SystemZ::CLRJ;
1101 case SystemZ::CLGR:
1102 return SystemZ::CLGRJ;
1103 case SystemZ::CLFI:
1104 return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLIJ : 0;
1105 case SystemZ::CLGFI:
1106 return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLGIJ : 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001107 default:
1108 return 0;
1109 }
1110}
1111
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001112void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1113 MachineBasicBlock::iterator MBBI,
1114 unsigned Reg, uint64_t Value) const {
1115 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1116 unsigned Opcode;
1117 if (isInt<16>(Value))
1118 Opcode = SystemZ::LGHI;
1119 else if (SystemZ::isImmLL(Value))
1120 Opcode = SystemZ::LLILL;
1121 else if (SystemZ::isImmLH(Value)) {
1122 Opcode = SystemZ::LLILH;
1123 Value >>= 16;
1124 } else {
1125 assert(isInt<32>(Value) && "Huge values not handled yet");
1126 Opcode = SystemZ::LGFI;
1127 }
1128 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1129}