blob: 7cbb4f715b5c804b55af037454a2bc75a9559e6a [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 Sandiford0755c932013-10-01 11:26:28 +000094// MI is an RXY-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.
97void SystemZInstrInfo::expandRXYPseudo(MachineInstr *MI, unsigned LowOpcode,
98 unsigned HighOpcode) const {
99 unsigned Reg = MI->getOperand(0).getReg();
100 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
101 MI->getOperand(2).getImm());
102 MI->setDesc(get(Opcode));
103}
104
105// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
106// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
107// are low registers, otherwise use RISB[LH]G. Size is the number of bits
108// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
109// KillSrc is true if this move is the last use of SrcReg.
110void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
111 MachineBasicBlock::iterator MBBI,
112 DebugLoc DL, unsigned DestReg,
113 unsigned SrcReg, unsigned LowLowOpcode,
114 unsigned Size, bool KillSrc) const {
115 unsigned Opcode;
116 bool DestIsHigh = isHighReg(DestReg);
117 bool SrcIsHigh = isHighReg(SrcReg);
118 if (DestIsHigh && SrcIsHigh)
119 Opcode = SystemZ::RISBHH;
120 else if (DestIsHigh && !SrcIsHigh)
121 Opcode = SystemZ::RISBHL;
122 else if (!DestIsHigh && SrcIsHigh)
123 Opcode = SystemZ::RISBLH;
124 else {
125 BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
126 .addReg(SrcReg, getKillRegState(KillSrc));
127 return;
128 }
129 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
130 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
131 .addReg(DestReg, RegState::Undef)
132 .addReg(SrcReg, getKillRegState(KillSrc))
133 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
134}
135
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000136// If MI is a simple load or store for a frame object, return the register
137// it loads or stores and set FrameIndex to the index of the frame object.
138// Return 0 otherwise.
139//
140// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000141static int isSimpleMove(const MachineInstr *MI, int &FrameIndex,
142 unsigned Flag) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000143 const MCInstrDesc &MCID = MI->getDesc();
144 if ((MCID.TSFlags & Flag) &&
145 MI->getOperand(1).isFI() &&
146 MI->getOperand(2).getImm() == 0 &&
147 MI->getOperand(3).getReg() == 0) {
148 FrameIndex = MI->getOperand(1).getIndex();
149 return MI->getOperand(0).getReg();
150 }
151 return 0;
152}
153
154unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
155 int &FrameIndex) const {
156 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
157}
158
159unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
160 int &FrameIndex) const {
161 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
162}
163
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000164bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr *MI,
165 int &DestFrameIndex,
166 int &SrcFrameIndex) const {
167 // Check for MVC 0(Length,FI1),0(FI2)
168 const MachineFrameInfo *MFI = MI->getParent()->getParent()->getFrameInfo();
169 if (MI->getOpcode() != SystemZ::MVC ||
170 !MI->getOperand(0).isFI() ||
171 MI->getOperand(1).getImm() != 0 ||
172 !MI->getOperand(3).isFI() ||
173 MI->getOperand(4).getImm() != 0)
174 return false;
175
176 // Check that Length covers the full slots.
177 int64_t Length = MI->getOperand(2).getImm();
178 unsigned FI1 = MI->getOperand(0).getIndex();
179 unsigned FI2 = MI->getOperand(3).getIndex();
180 if (MFI->getObjectSize(FI1) != Length ||
181 MFI->getObjectSize(FI2) != Length)
182 return false;
183
184 DestFrameIndex = FI1;
185 SrcFrameIndex = FI2;
186 return true;
187}
188
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000189bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
190 MachineBasicBlock *&TBB,
191 MachineBasicBlock *&FBB,
192 SmallVectorImpl<MachineOperand> &Cond,
193 bool AllowModify) const {
194 // Most of the code and comments here are boilerplate.
195
196 // Start from the bottom of the block and work up, examining the
197 // terminator instructions.
198 MachineBasicBlock::iterator I = MBB.end();
199 while (I != MBB.begin()) {
200 --I;
201 if (I->isDebugValue())
202 continue;
203
204 // Working from the bottom, when we see a non-terminator instruction, we're
205 // done.
206 if (!isUnpredicatedTerminator(I))
207 break;
208
209 // A terminator that isn't a branch can't easily be handled by this
210 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000211 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000212 return true;
213
214 // Can't handle indirect branches.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000215 SystemZII::Branch Branch(getBranchInfo(I));
216 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000217 return true;
218
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000219 // Punt on compound branches.
220 if (Branch.Type != SystemZII::BranchNormal)
221 return true;
222
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000223 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000224 // Handle unconditional branches.
225 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000226 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000227 continue;
228 }
229
230 // If the block has any instructions after a JMP, delete them.
231 while (llvm::next(I) != MBB.end())
232 llvm::next(I)->eraseFromParent();
233
234 Cond.clear();
235 FBB = 0;
236
237 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000238 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000239 TBB = 0;
240 I->eraseFromParent();
241 I = MBB.end();
242 continue;
243 }
244
245 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000246 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000247 continue;
248 }
249
250 // Working from the bottom, handle the first conditional branch.
251 if (Cond.empty()) {
252 // FIXME: add X86-style branch swap
253 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000254 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000255 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000256 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000257 continue;
258 }
259
260 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000261 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000262
263 // Only handle the case where all conditional branches branch to the same
264 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000265 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000266 return true;
267
268 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000269 unsigned OldCCValid = Cond[0].getImm();
270 unsigned OldCCMask = Cond[1].getImm();
271 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000272 continue;
273
274 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000275 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000276 }
277
278 return false;
279}
280
281unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
282 // Most of the code and comments here are boilerplate.
283 MachineBasicBlock::iterator I = MBB.end();
284 unsigned Count = 0;
285
286 while (I != MBB.begin()) {
287 --I;
288 if (I->isDebugValue())
289 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000290 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000291 break;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000292 if (!getBranchInfo(I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000293 break;
294 // Remove the branch.
295 I->eraseFromParent();
296 I = MBB.end();
297 ++Count;
298 }
299
300 return Count;
301}
302
Richard Sandiford3d768e32013-07-31 12:30:20 +0000303bool SystemZInstrInfo::
304ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
305 assert(Cond.size() == 2 && "Invalid condition");
306 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
307 return false;
308}
309
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000310unsigned
311SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
312 MachineBasicBlock *FBB,
313 const SmallVectorImpl<MachineOperand> &Cond,
314 DebugLoc DL) const {
315 // In this function we output 32-bit branches, which should always
316 // have enough range. They can be shortened and relaxed by later code
317 // in the pipeline, if desired.
318
319 // Shouldn't be a fall through.
320 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000321 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000322 "SystemZ branch conditions have one component!");
323
324 if (Cond.empty()) {
325 // Unconditional branch?
326 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000327 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000328 return 1;
329 }
330
331 // Conditional branch.
332 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000333 unsigned CCValid = Cond[0].getImm();
334 unsigned CCMask = Cond[1].getImm();
335 BuildMI(&MBB, DL, get(SystemZ::BRC))
336 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000337 ++Count;
338
339 if (FBB) {
340 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000341 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000342 ++Count;
343 }
344 return Count;
345}
346
Richard Sandiford564681c2013-08-12 10:28:10 +0000347bool SystemZInstrInfo::analyzeCompare(const MachineInstr *MI,
348 unsigned &SrcReg, unsigned &SrcReg2,
349 int &Mask, int &Value) const {
350 assert(MI->isCompare() && "Caller should have checked for a comparison");
351
352 if (MI->getNumExplicitOperands() == 2 &&
353 MI->getOperand(0).isReg() &&
354 MI->getOperand(1).isImm()) {
355 SrcReg = MI->getOperand(0).getReg();
356 SrcReg2 = 0;
357 Value = MI->getOperand(1).getImm();
358 Mask = ~0;
359 return true;
360 }
361
362 return false;
363}
364
Richard Sandiforda5901252013-08-16 10:22:54 +0000365// If Reg is a virtual register, return its definition, otherwise return null.
366static MachineInstr *getDef(unsigned Reg,
367 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000368 if (TargetRegisterInfo::isPhysicalRegister(Reg))
369 return 0;
Richard Sandiford564681c2013-08-12 10:28:10 +0000370 return MRI->getUniqueVRegDef(Reg);
371}
372
373// Return true if MI is a shift of type Opcode by Imm bits.
374static bool isShift(MachineInstr *MI, int Opcode, int64_t Imm) {
375 return (MI->getOpcode() == Opcode &&
376 !MI->getOperand(2).getReg() &&
377 MI->getOperand(3).getImm() == Imm);
378}
379
Richard Sandiforda5901252013-08-16 10:22:54 +0000380// If the destination of MI has no uses, delete it as dead.
381static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
382 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
383 MI->eraseFromParent();
384}
385
Richard Sandiford564681c2013-08-12 10:28:10 +0000386// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000387// the result of an IPM sequence whose input CC survives until Compare,
388// and whether Compare is therefore redundant. Delete it and return
389// true if so.
390static bool removeIPMBasedCompare(MachineInstr *Compare, unsigned SrcReg,
391 const MachineRegisterInfo *MRI,
392 const TargetRegisterInfo *TRI) {
Richard Sandiforde3827752013-08-16 10:55:47 +0000393 MachineInstr *LGFR = 0;
Richard Sandiforda5901252013-08-16 10:22:54 +0000394 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000395 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
396 LGFR = RLL;
397 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
398 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000399 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000400 return false;
401
Richard Sandiforda5901252013-08-16 10:22:54 +0000402 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
403 if (!SRL || !isShift(SRL, SystemZ::SRL, 28))
Richard Sandiford564681c2013-08-12 10:28:10 +0000404 return false;
405
Richard Sandiforda5901252013-08-16 10:22:54 +0000406 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000407 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
408 return false;
409
410 // Check that there are no assignments to CC between the IPM and Compare,
Richard Sandiford564681c2013-08-12 10:28:10 +0000411 if (IPM->getParent() != Compare->getParent())
412 return false;
413 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare;
414 for (++MBBI; MBBI != MBBE; ++MBBI) {
415 MachineInstr *MI = MBBI;
Richard Sandiforda5901252013-08-16 10:22:54 +0000416 if (MI->modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000417 return false;
418 }
419
Richard Sandiford564681c2013-08-12 10:28:10 +0000420 Compare->eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000421 if (LGFR)
422 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000423 eraseIfDead(RLL, MRI);
424 eraseIfDead(SRL, MRI);
425 eraseIfDead(IPM, MRI);
426
Richard Sandiford564681c2013-08-12 10:28:10 +0000427 return true;
428}
429
430bool
431SystemZInstrInfo::optimizeCompareInstr(MachineInstr *Compare,
432 unsigned SrcReg, unsigned SrcReg2,
433 int Mask, int Value,
434 const MachineRegisterInfo *MRI) const {
435 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
436 bool IsLogical = (Compare->getDesc().TSFlags & SystemZII::IsLogical) != 0;
437 if (Value == 0 &&
438 !IsLogical &&
Richard Sandiforda5901252013-08-16 10:22:54 +0000439 removeIPMBasedCompare(Compare, SrcReg, MRI, TM.getRegisterInfo()))
Richard Sandiford564681c2013-08-12 10:28:10 +0000440 return true;
441 return false;
442}
443
Richard Sandifordf2404162013-07-25 09:11:15 +0000444// If Opcode is a move that has a conditional variant, return that variant,
445// otherwise return 0.
446static unsigned getConditionalMove(unsigned Opcode) {
447 switch (Opcode) {
448 case SystemZ::LR: return SystemZ::LOCR;
449 case SystemZ::LGR: return SystemZ::LOCGR;
450 default: return 0;
451 }
452}
453
454bool SystemZInstrInfo::isPredicable(MachineInstr *MI) const {
455 unsigned Opcode = MI->getOpcode();
456 if (TM.getSubtargetImpl()->hasLoadStoreOnCond() &&
457 getConditionalMove(Opcode))
458 return true;
459 return false;
460}
461
462bool SystemZInstrInfo::
463isProfitableToIfCvt(MachineBasicBlock &MBB,
464 unsigned NumCycles, unsigned ExtraPredCycles,
465 const BranchProbability &Probability) const {
466 // For now only convert single instructions.
467 return NumCycles == 1;
468}
469
470bool SystemZInstrInfo::
471isProfitableToIfCvt(MachineBasicBlock &TMBB,
472 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
473 MachineBasicBlock &FMBB,
474 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
475 const BranchProbability &Probability) const {
476 // For now avoid converting mutually-exclusive cases.
477 return false;
478}
479
480bool SystemZInstrInfo::
481PredicateInstruction(MachineInstr *MI,
482 const SmallVectorImpl<MachineOperand> &Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000483 assert(Pred.size() == 2 && "Invalid condition");
484 unsigned CCValid = Pred[0].getImm();
485 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000486 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
487 unsigned Opcode = MI->getOpcode();
488 if (TM.getSubtargetImpl()->hasLoadStoreOnCond()) {
489 if (unsigned CondOpcode = getConditionalMove(Opcode)) {
490 MI->setDesc(get(CondOpcode));
Richard Sandiford3d768e32013-07-31 12:30:20 +0000491 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000492 .addImm(CCValid).addImm(CCMask)
493 .addReg(SystemZ::CC, RegState::Implicit);;
Richard Sandifordf2404162013-07-25 09:11:15 +0000494 return true;
495 }
496 }
497 return false;
498}
499
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000500void
501SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
502 MachineBasicBlock::iterator MBBI, DebugLoc DL,
503 unsigned DestReg, unsigned SrcReg,
504 bool KillSrc) const {
505 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
506 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000507 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
508 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
509 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
510 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000511 return;
512 }
513
Richard Sandiford0755c932013-10-01 11:26:28 +0000514 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
515 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
516 return;
517 }
518
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000519 // Everything else needs only one instruction.
520 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000521 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000522 Opcode = SystemZ::LGR;
523 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
524 Opcode = SystemZ::LER;
525 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
526 Opcode = SystemZ::LDR;
527 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
528 Opcode = SystemZ::LXR;
529 else
530 llvm_unreachable("Impossible reg-to-reg copy");
531
532 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
533 .addReg(SrcReg, getKillRegState(KillSrc));
534}
535
536void
537SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
538 MachineBasicBlock::iterator MBBI,
539 unsigned SrcReg, bool isKill,
540 int FrameIdx,
541 const TargetRegisterClass *RC,
542 const TargetRegisterInfo *TRI) const {
543 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
544
545 // Callers may expect a single instruction, so keep 128-bit moves
546 // together for now and lower them after register allocation.
547 unsigned LoadOpcode, StoreOpcode;
548 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
549 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
550 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
551}
552
553void
554SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
555 MachineBasicBlock::iterator MBBI,
556 unsigned DestReg, int FrameIdx,
557 const TargetRegisterClass *RC,
558 const TargetRegisterInfo *TRI) const {
559 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
560
561 // Callers may expect a single instruction, so keep 128-bit moves
562 // together for now and lower them after register allocation.
563 unsigned LoadOpcode, StoreOpcode;
564 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
565 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
566 FrameIdx);
567}
568
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000569// Return true if MI is a simple load or store with a 12-bit displacement
570// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
571static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
572 const MCInstrDesc &MCID = MI->getDesc();
573 return ((MCID.TSFlags & Flag) &&
574 isUInt<12>(MI->getOperand(2).getImm()) &&
575 MI->getOperand(3).getReg() == 0);
576}
577
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000578namespace {
579 struct LogicOp {
580 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
581 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
582 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
583
584 operator bool() const { return RegSize; }
585
586 unsigned RegSize, ImmLSB, ImmSize;
587 };
588}
589
590static LogicOp interpretAndImmediate(unsigned Opcode) {
591 switch (Opcode) {
Richard Sandiford652784e2013-09-25 11:11:53 +0000592 case SystemZ::NILL: return LogicOp(32, 0, 16);
593 case SystemZ::NILH: return LogicOp(32, 16, 16);
594 case SystemZ::NILL64: return LogicOp(64, 0, 16);
595 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000596 case SystemZ::NIHL: return LogicOp(64, 32, 16);
597 case SystemZ::NIHH: return LogicOp(64, 48, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000598 case SystemZ::NILF: return LogicOp(32, 0, 32);
599 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000600 case SystemZ::NIHF: return LogicOp(64, 32, 32);
601 default: return LogicOp();
602 }
603}
604
605// Used to return from convertToThreeAddress after replacing two-address
606// instruction OldMI with three-address instruction NewMI.
607static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
608 MachineInstr *NewMI,
609 LiveVariables *LV) {
610 if (LV) {
611 unsigned NumOps = OldMI->getNumOperands();
612 for (unsigned I = 1; I < NumOps; ++I) {
613 MachineOperand &Op = OldMI->getOperand(I);
614 if (Op.isReg() && Op.isKill())
615 LV->replaceKillInstruction(Op.getReg(), OldMI, NewMI);
616 }
617 }
618 return NewMI;
619}
620
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000621MachineInstr *
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000622SystemZInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
623 MachineBasicBlock::iterator &MBBI,
624 LiveVariables *LV) const {
625 MachineInstr *MI = MBBI;
626 MachineBasicBlock *MBB = MI->getParent();
627
628 unsigned Opcode = MI->getOpcode();
629 unsigned NumOps = MI->getNumOperands();
630
631 // Try to convert something like SLL into SLLK, if supported.
632 // We prefer to keep the two-operand form where possible both
633 // because it tends to be shorter and because some instructions
634 // have memory forms that can be used during spilling.
635 if (TM.getSubtargetImpl()->hasDistinctOps()) {
636 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
637 if (ThreeOperandOpcode >= 0) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000638 MachineOperand &Dest = MI->getOperand(0);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000639 MachineOperand &Src = MI->getOperand(1);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000640 MachineInstrBuilder MIB =
641 BuildMI(*MBB, MBBI, MI->getDebugLoc(), get(ThreeOperandOpcode))
642 .addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000643 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000644 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000645 // Keep the remaining operands as-is.
646 for (unsigned I = 2; I < NumOps; ++I)
647 MIB.addOperand(MI->getOperand(I));
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000648 return finishConvertToThreeAddress(MI, MIB, LV);
649 }
650 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000651
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000652 // Try to convert an AND into an RISBG-type instruction.
653 if (LogicOp And = interpretAndImmediate(Opcode)) {
654 unsigned NewOpcode;
655 if (And.RegSize == 64)
656 NewOpcode = SystemZ::RISBG;
657 else if (TM.getSubtargetImpl()->hasHighWord())
Richard Sandiford0755c932013-10-01 11:26:28 +0000658 NewOpcode = SystemZ::RISBLL;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000659 else
660 // We can't use RISBG for 32-bit operations because it clobbers the
661 // high word of the destination too.
662 NewOpcode = 0;
663 if (NewOpcode) {
664 uint64_t Imm = MI->getOperand(2).getImm() << And.ImmLSB;
665 // AND IMMEDIATE leaves the other bits of the register unchanged.
666 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
667 unsigned Start, End;
668 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
Richard Sandiford0755c932013-10-01 11:26:28 +0000669 if (NewOpcode == SystemZ::RISBLL) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000670 Start &= 31;
671 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000672 }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000673 MachineOperand &Dest = MI->getOperand(0);
674 MachineOperand &Src = MI->getOperand(1);
675 MachineInstrBuilder MIB =
676 BuildMI(*MBB, MI, MI->getDebugLoc(), get(NewOpcode))
677 .addOperand(Dest).addReg(0)
678 .addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg())
679 .addImm(Start).addImm(End + 128).addImm(0);
680 return finishConvertToThreeAddress(MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000681 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000682 }
683 }
684 return 0;
685}
686
687MachineInstr *
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000688SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
689 MachineInstr *MI,
690 const SmallVectorImpl<unsigned> &Ops,
691 int FrameIndex) const {
692 const MachineFrameInfo *MFI = MF.getFrameInfo();
693 unsigned Size = MFI->getObjectSize(FrameIndex);
694
695 // Eary exit for cases we don't care about
696 if (Ops.size() != 1)
697 return 0;
698
699 unsigned OpNum = Ops[0];
NAKAMURA Takumiddcba562013-07-03 02:20:49 +0000700 assert(Size == MF.getRegInfo()
701 .getRegClass(MI->getOperand(OpNum).getReg())->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000702 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000703
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000704 unsigned Opcode = MI->getOpcode();
705 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
706 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
707 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
708 // If we're spilling the destination of an LDGR or LGDR, store the
709 // source register instead.
710 if (OpNum == 0) {
711 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
712 return BuildMI(MF, MI->getDebugLoc(), get(StoreOpcode))
713 .addOperand(MI->getOperand(1)).addFrameIndex(FrameIndex)
714 .addImm(0).addReg(0);
715 }
716 // If we're spilling the source of an LDGR or LGDR, load the
717 // destination register instead.
718 if (OpNum == 1) {
719 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
720 unsigned Dest = MI->getOperand(0).getReg();
721 return BuildMI(MF, MI->getDebugLoc(), get(LoadOpcode), Dest)
722 .addFrameIndex(FrameIndex).addImm(0).addReg(0);
723 }
724 }
725
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000726 // Look for cases where the source of a simple store or the destination
727 // of a simple load is being spilled. Try to use MVC instead.
728 //
729 // Although MVC is in practice a fast choice in these cases, it is still
730 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +0000731 // load or store is volatile. We also wouldn't be able to use MVC if
732 // the two memories partially overlap, but that case cannot occur here,
733 // because we know that one of the memories is a full frame index.
734 //
735 // For performance reasons, we also want to avoid using MVC if the addresses
736 // might be equal. We don't worry about that case here, because spill slot
737 // coloring happens later, and because we have special code to remove
738 // MVCs that turn out to be redundant.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000739 if (OpNum == 0 && MI->hasOneMemOperand()) {
740 MachineMemOperand *MMO = *MI->memoperands_begin();
741 if (MMO->getSize() == Size && !MMO->isVolatile()) {
742 // Handle conversion of loads.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000743 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXLoad)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000744 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000745 .addFrameIndex(FrameIndex).addImm(0).addImm(Size)
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000746 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000747 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000748 }
749 // Handle conversion of stores.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000750 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXStore)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000751 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
752 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000753 .addImm(Size).addFrameIndex(FrameIndex).addImm(0)
754 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000755 }
756 }
757 }
758
Richard Sandiforded1fab62013-07-03 10:10:02 +0000759 // If the spilled operand is the final one, try to change <INSN>R
760 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000761 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000762 if (MemOpcode >= 0) {
763 unsigned NumOps = MI->getNumExplicitOperands();
764 if (OpNum == NumOps - 1) {
765 const MCInstrDesc &MemDesc = get(MemOpcode);
766 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
767 assert(AccessBytes != 0 && "Size of access should be known");
768 assert(AccessBytes <= Size && "Access outside the frame index");
769 uint64_t Offset = Size - AccessBytes;
Richard Sandiforded1fab62013-07-03 10:10:02 +0000770 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), get(MemOpcode));
771 for (unsigned I = 0; I < OpNum; ++I)
772 MIB.addOperand(MI->getOperand(I));
773 MIB.addFrameIndex(FrameIndex).addImm(Offset);
774 if (MemDesc.TSFlags & SystemZII::HasIndex)
775 MIB.addReg(0);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000776 return MIB;
777 }
778 }
779
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000780 return 0;
781}
782
783MachineInstr *
784SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr* MI,
785 const SmallVectorImpl<unsigned> &Ops,
786 MachineInstr* LoadMI) const {
787 return 0;
788}
789
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000790bool
791SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
792 switch (MI->getOpcode()) {
793 case SystemZ::L128:
794 splitMove(MI, SystemZ::LG);
795 return true;
796
797 case SystemZ::ST128:
798 splitMove(MI, SystemZ::STG);
799 return true;
800
801 case SystemZ::LX:
802 splitMove(MI, SystemZ::LD);
803 return true;
804
805 case SystemZ::STX:
806 splitMove(MI, SystemZ::STD);
807 return true;
808
Richard Sandiford89e160d2013-10-01 12:11:47 +0000809 case SystemZ::LBMux:
810 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
811 return true;
812
813 case SystemZ::LHMux:
814 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
815 return true;
816
Richard Sandiford0d46b1a2013-10-01 12:19:08 +0000817 case SystemZ::LLCMux:
818 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
819 return true;
820
821 case SystemZ::LLHMux:
822 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
823 return true;
824
Richard Sandiford0755c932013-10-01 11:26:28 +0000825 case SystemZ::LMux:
826 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
827 return true;
828
829 case SystemZ::STMux:
830 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
831 return true;
832
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000833 case SystemZ::ADJDYNALLOC:
834 splitAdjDynAlloc(MI);
835 return true;
836
837 default:
838 return false;
839 }
840}
841
Richard Sandiford312425f2013-05-20 14:23:08 +0000842uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
843 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
844 const MachineFunction *MF = MI->getParent()->getParent();
845 const char *AsmStr = MI->getOperand(0).getSymbolName();
846 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
847 }
848 return MI->getDesc().getSize();
849}
850
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000851SystemZII::Branch
852SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000853 switch (MI->getOpcode()) {
854 case SystemZ::BR:
855 case SystemZ::J:
856 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000857 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Richard Sandiford3d768e32013-07-31 12:30:20 +0000858 SystemZ::CCMASK_ANY, &MI->getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000859
860 case SystemZ::BRC:
861 case SystemZ::BRCL:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000862 return SystemZII::Branch(SystemZII::BranchNormal,
Richard Sandiford3d768e32013-07-31 12:30:20 +0000863 MI->getOperand(0).getImm(),
864 MI->getOperand(1).getImm(), &MI->getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000865
Richard Sandifordc2121252013-08-05 11:23:46 +0000866 case SystemZ::BRCT:
867 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
868 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
869
870 case SystemZ::BRCTG:
871 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
872 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
873
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000874 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000875 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +0000876 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
877 MI->getOperand(2).getImm(), &MI->getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000878
Richard Sandiford93183ee2013-09-18 09:56:40 +0000879 case SystemZ::CLIJ:
880 case SystemZ::CLRJ:
881 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
882 MI->getOperand(2).getImm(), &MI->getOperand(3));
883
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000884 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000885 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +0000886 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
887 MI->getOperand(2).getImm(), &MI->getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000888
Richard Sandiford93183ee2013-09-18 09:56:40 +0000889 case SystemZ::CLGIJ:
890 case SystemZ::CLGRJ:
891 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
892 MI->getOperand(2).getImm(), &MI->getOperand(3));
893
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000894 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000895 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000896 }
897}
898
899void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
900 unsigned &LoadOpcode,
901 unsigned &StoreOpcode) const {
902 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
903 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +0000904 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +0000905 } else if (RC == &SystemZ::GRH32BitRegClass) {
906 LoadOpcode = SystemZ::LFH;
907 StoreOpcode = SystemZ::STFH;
908 } else if (RC == &SystemZ::GRX32BitRegClass) {
909 LoadOpcode = SystemZ::LMux;
910 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000911 } else if (RC == &SystemZ::GR64BitRegClass ||
912 RC == &SystemZ::ADDR64BitRegClass) {
913 LoadOpcode = SystemZ::LG;
914 StoreOpcode = SystemZ::STG;
915 } else if (RC == &SystemZ::GR128BitRegClass ||
916 RC == &SystemZ::ADDR128BitRegClass) {
917 LoadOpcode = SystemZ::L128;
918 StoreOpcode = SystemZ::ST128;
919 } else if (RC == &SystemZ::FP32BitRegClass) {
920 LoadOpcode = SystemZ::LE;
921 StoreOpcode = SystemZ::STE;
922 } else if (RC == &SystemZ::FP64BitRegClass) {
923 LoadOpcode = SystemZ::LD;
924 StoreOpcode = SystemZ::STD;
925 } else if (RC == &SystemZ::FP128BitRegClass) {
926 LoadOpcode = SystemZ::LX;
927 StoreOpcode = SystemZ::STX;
928 } else
929 llvm_unreachable("Unsupported regclass to load or store");
930}
931
932unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
933 int64_t Offset) const {
934 const MCInstrDesc &MCID = get(Opcode);
935 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
936 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
937 // Get the instruction to use for unsigned 12-bit displacements.
938 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
939 if (Disp12Opcode >= 0)
940 return Disp12Opcode;
941
942 // All address-related instructions can use unsigned 12-bit
943 // displacements.
944 return Opcode;
945 }
946 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
947 // Get the instruction to use for signed 20-bit displacements.
948 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
949 if (Disp20Opcode >= 0)
950 return Disp20Opcode;
951
952 // Check whether Opcode allows signed 20-bit displacements.
953 if (MCID.TSFlags & SystemZII::Has20BitOffset)
954 return Opcode;
955 }
956 return 0;
957}
958
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000959unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
960 switch (Opcode) {
961 case SystemZ::L: return SystemZ::LT;
962 case SystemZ::LY: return SystemZ::LT;
963 case SystemZ::LG: return SystemZ::LTG;
964 case SystemZ::LGF: return SystemZ::LTGF;
965 case SystemZ::LR: return SystemZ::LTR;
966 case SystemZ::LGFR: return SystemZ::LTGFR;
967 case SystemZ::LGR: return SystemZ::LTGR;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000968 case SystemZ::LER: return SystemZ::LTEBR;
969 case SystemZ::LDR: return SystemZ::LTDBR;
970 case SystemZ::LXR: return SystemZ::LTXBR;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000971 default: return 0;
972 }
973}
974
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000975// Return true if Mask matches the regexp 0*1+0*, given that zero masks
976// have already been filtered out. Store the first set bit in LSB and
977// the number of set bits in Length if so.
978static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
979 unsigned First = findFirstSet(Mask);
980 uint64_t Top = (Mask >> First) + 1;
981 if ((Top & -Top) == Top) {
982 LSB = First;
983 Length = findFirstSet(Top);
984 return true;
985 }
986 return false;
987}
988
989bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
990 unsigned &Start, unsigned &End) const {
991 // Reject trivial all-zero masks.
992 if (Mask == 0)
993 return false;
994
995 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
996 // the msb and End specifies the index of the lsb.
997 unsigned LSB, Length;
998 if (isStringOfOnes(Mask, LSB, Length)) {
999 Start = 63 - (LSB + Length - 1);
1000 End = 63 - LSB;
1001 return true;
1002 }
1003
1004 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1005 // of the low 1s and End specifies the lsb of the high 1s.
1006 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1007 assert(LSB > 0 && "Bottom bit must be set");
1008 assert(LSB + Length < BitSize && "Top bit must be set");
1009 Start = 63 - (LSB - 1);
1010 End = 63 - (LSB + Length);
1011 return true;
1012 }
1013
1014 return false;
1015}
1016
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001017unsigned SystemZInstrInfo::getCompareAndBranch(unsigned Opcode,
1018 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001019 switch (Opcode) {
1020 case SystemZ::CR:
1021 return SystemZ::CRJ;
1022 case SystemZ::CGR:
1023 return SystemZ::CGRJ;
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001024 case SystemZ::CHI:
1025 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CIJ : 0;
1026 case SystemZ::CGHI:
1027 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CGIJ : 0;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001028 case SystemZ::CLR:
1029 return SystemZ::CLRJ;
1030 case SystemZ::CLGR:
1031 return SystemZ::CLGRJ;
1032 case SystemZ::CLFI:
1033 return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLIJ : 0;
1034 case SystemZ::CLGFI:
1035 return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLGIJ : 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001036 default:
1037 return 0;
1038 }
1039}
1040
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001041void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1042 MachineBasicBlock::iterator MBBI,
1043 unsigned Reg, uint64_t Value) const {
1044 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1045 unsigned Opcode;
1046 if (isInt<16>(Value))
1047 Opcode = SystemZ::LGHI;
1048 else if (SystemZ::isImmLL(Value))
1049 Opcode = SystemZ::LLILL;
1050 else if (SystemZ::isImmLH(Value)) {
1051 Opcode = SystemZ::LLILH;
1052 Value >>= 16;
1053 } else {
1054 assert(isInt<32>(Value) && "Huge values not handled yet");
1055 Opcode = SystemZ::LGFI;
1056 }
1057 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1058}