blob: 38d0a32d6f7ba97b90bf06cedc395f01d57ab25d [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 Sandiford42a694f2013-10-01 14:53:46 +0000110// MI is a three-operand RIE-style pseudo instruction. Replace it with
111// LowOpcode3 if the registers are both low GR32s, otherwise use a move
112// followed by HighOpcode or LowOpcode, depending on whether the target
113// is a high or low GR32.
114void SystemZInstrInfo::expandRIEPseudo(MachineInstr *MI, unsigned LowOpcode,
115 unsigned LowOpcodeK,
116 unsigned HighOpcode) const {
117 unsigned DestReg = MI->getOperand(0).getReg();
118 unsigned SrcReg = MI->getOperand(1).getReg();
119 bool DestIsHigh = isHighReg(DestReg);
120 bool SrcIsHigh = isHighReg(SrcReg);
121 if (!DestIsHigh && !SrcIsHigh)
122 MI->setDesc(get(LowOpcodeK));
123 else {
124 emitGRX32Move(*MI->getParent(), MI, MI->getDebugLoc(),
125 DestReg, SrcReg, SystemZ::LR, 32,
126 MI->getOperand(1).isKill());
127 MI->setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
128 MI->getOperand(1).setReg(DestReg);
129 }
130}
131
Richard Sandiford0755c932013-10-01 11:26:28 +0000132// MI is an RXY-style pseudo instruction. Replace it with LowOpcode
133// if the first operand is a low GR32 and HighOpcode if the first operand
134// is a high GR32.
135void SystemZInstrInfo::expandRXYPseudo(MachineInstr *MI, unsigned LowOpcode,
136 unsigned HighOpcode) const {
137 unsigned Reg = MI->getOperand(0).getReg();
138 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
139 MI->getOperand(2).getImm());
140 MI->setDesc(get(Opcode));
141}
142
Richard Sandiford21235a22013-10-01 12:49:07 +0000143// MI is an RR-style pseudo instruction that zero-extends the low Size bits
144// of one GRX32 into another. Replace it with LowOpcode if both operands
145// are low registers, otherwise use RISB[LH]G.
146void SystemZInstrInfo::expandZExtPseudo(MachineInstr *MI, unsigned LowOpcode,
147 unsigned Size) const {
148 emitGRX32Move(*MI->getParent(), MI, MI->getDebugLoc(),
149 MI->getOperand(0).getReg(), MI->getOperand(1).getReg(),
150 LowOpcode, Size, MI->getOperand(1).isKill());
151 MI->eraseFromParent();
152}
153
Richard Sandiford0755c932013-10-01 11:26:28 +0000154// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
155// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
156// are low registers, otherwise use RISB[LH]G. Size is the number of bits
157// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
158// KillSrc is true if this move is the last use of SrcReg.
159void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
160 MachineBasicBlock::iterator MBBI,
161 DebugLoc DL, unsigned DestReg,
162 unsigned SrcReg, unsigned LowLowOpcode,
163 unsigned Size, bool KillSrc) const {
164 unsigned Opcode;
165 bool DestIsHigh = isHighReg(DestReg);
166 bool SrcIsHigh = isHighReg(SrcReg);
167 if (DestIsHigh && SrcIsHigh)
168 Opcode = SystemZ::RISBHH;
169 else if (DestIsHigh && !SrcIsHigh)
170 Opcode = SystemZ::RISBHL;
171 else if (!DestIsHigh && SrcIsHigh)
172 Opcode = SystemZ::RISBLH;
173 else {
174 BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
175 .addReg(SrcReg, getKillRegState(KillSrc));
176 return;
177 }
178 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
179 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
180 .addReg(DestReg, RegState::Undef)
181 .addReg(SrcReg, getKillRegState(KillSrc))
182 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
183}
184
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000185// If MI is a simple load or store for a frame object, return the register
186// it loads or stores and set FrameIndex to the index of the frame object.
187// Return 0 otherwise.
188//
189// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000190static int isSimpleMove(const MachineInstr *MI, int &FrameIndex,
191 unsigned Flag) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 const MCInstrDesc &MCID = MI->getDesc();
193 if ((MCID.TSFlags & Flag) &&
194 MI->getOperand(1).isFI() &&
195 MI->getOperand(2).getImm() == 0 &&
196 MI->getOperand(3).getReg() == 0) {
197 FrameIndex = MI->getOperand(1).getIndex();
198 return MI->getOperand(0).getReg();
199 }
200 return 0;
201}
202
203unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
204 int &FrameIndex) const {
205 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
206}
207
208unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
209 int &FrameIndex) const {
210 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
211}
212
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000213bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr *MI,
214 int &DestFrameIndex,
215 int &SrcFrameIndex) const {
216 // Check for MVC 0(Length,FI1),0(FI2)
217 const MachineFrameInfo *MFI = MI->getParent()->getParent()->getFrameInfo();
218 if (MI->getOpcode() != SystemZ::MVC ||
219 !MI->getOperand(0).isFI() ||
220 MI->getOperand(1).getImm() != 0 ||
221 !MI->getOperand(3).isFI() ||
222 MI->getOperand(4).getImm() != 0)
223 return false;
224
225 // Check that Length covers the full slots.
226 int64_t Length = MI->getOperand(2).getImm();
227 unsigned FI1 = MI->getOperand(0).getIndex();
228 unsigned FI2 = MI->getOperand(3).getIndex();
229 if (MFI->getObjectSize(FI1) != Length ||
230 MFI->getObjectSize(FI2) != Length)
231 return false;
232
233 DestFrameIndex = FI1;
234 SrcFrameIndex = FI2;
235 return true;
236}
237
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000238bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
239 MachineBasicBlock *&TBB,
240 MachineBasicBlock *&FBB,
241 SmallVectorImpl<MachineOperand> &Cond,
242 bool AllowModify) const {
243 // Most of the code and comments here are boilerplate.
244
245 // Start from the bottom of the block and work up, examining the
246 // terminator instructions.
247 MachineBasicBlock::iterator I = MBB.end();
248 while (I != MBB.begin()) {
249 --I;
250 if (I->isDebugValue())
251 continue;
252
253 // Working from the bottom, when we see a non-terminator instruction, we're
254 // done.
255 if (!isUnpredicatedTerminator(I))
256 break;
257
258 // A terminator that isn't a branch can't easily be handled by this
259 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000260 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000261 return true;
262
263 // Can't handle indirect branches.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000264 SystemZII::Branch Branch(getBranchInfo(I));
265 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000266 return true;
267
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000268 // Punt on compound branches.
269 if (Branch.Type != SystemZII::BranchNormal)
270 return true;
271
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000272 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000273 // Handle unconditional branches.
274 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000275 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000276 continue;
277 }
278
279 // If the block has any instructions after a JMP, delete them.
280 while (llvm::next(I) != MBB.end())
281 llvm::next(I)->eraseFromParent();
282
283 Cond.clear();
284 FBB = 0;
285
286 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000287 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000288 TBB = 0;
289 I->eraseFromParent();
290 I = MBB.end();
291 continue;
292 }
293
294 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000295 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000296 continue;
297 }
298
299 // Working from the bottom, handle the first conditional branch.
300 if (Cond.empty()) {
301 // FIXME: add X86-style branch swap
302 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000303 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000304 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000305 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000306 continue;
307 }
308
309 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000310 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000311
312 // Only handle the case where all conditional branches branch to the same
313 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000314 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000315 return true;
316
317 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000318 unsigned OldCCValid = Cond[0].getImm();
319 unsigned OldCCMask = Cond[1].getImm();
320 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000321 continue;
322
323 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000324 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000325 }
326
327 return false;
328}
329
330unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
331 // Most of the code and comments here are boilerplate.
332 MachineBasicBlock::iterator I = MBB.end();
333 unsigned Count = 0;
334
335 while (I != MBB.begin()) {
336 --I;
337 if (I->isDebugValue())
338 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000339 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000340 break;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000341 if (!getBranchInfo(I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000342 break;
343 // Remove the branch.
344 I->eraseFromParent();
345 I = MBB.end();
346 ++Count;
347 }
348
349 return Count;
350}
351
Richard Sandiford3d768e32013-07-31 12:30:20 +0000352bool SystemZInstrInfo::
353ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
354 assert(Cond.size() == 2 && "Invalid condition");
355 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
356 return false;
357}
358
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000359unsigned
360SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
361 MachineBasicBlock *FBB,
362 const SmallVectorImpl<MachineOperand> &Cond,
363 DebugLoc DL) const {
364 // In this function we output 32-bit branches, which should always
365 // have enough range. They can be shortened and relaxed by later code
366 // in the pipeline, if desired.
367
368 // Shouldn't be a fall through.
369 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000370 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000371 "SystemZ branch conditions have one component!");
372
373 if (Cond.empty()) {
374 // Unconditional branch?
375 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000376 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000377 return 1;
378 }
379
380 // Conditional branch.
381 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000382 unsigned CCValid = Cond[0].getImm();
383 unsigned CCMask = Cond[1].getImm();
384 BuildMI(&MBB, DL, get(SystemZ::BRC))
385 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000386 ++Count;
387
388 if (FBB) {
389 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000390 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000391 ++Count;
392 }
393 return Count;
394}
395
Richard Sandiford564681c2013-08-12 10:28:10 +0000396bool SystemZInstrInfo::analyzeCompare(const MachineInstr *MI,
397 unsigned &SrcReg, unsigned &SrcReg2,
398 int &Mask, int &Value) const {
399 assert(MI->isCompare() && "Caller should have checked for a comparison");
400
401 if (MI->getNumExplicitOperands() == 2 &&
402 MI->getOperand(0).isReg() &&
403 MI->getOperand(1).isImm()) {
404 SrcReg = MI->getOperand(0).getReg();
405 SrcReg2 = 0;
406 Value = MI->getOperand(1).getImm();
407 Mask = ~0;
408 return true;
409 }
410
411 return false;
412}
413
Richard Sandiforda5901252013-08-16 10:22:54 +0000414// If Reg is a virtual register, return its definition, otherwise return null.
415static MachineInstr *getDef(unsigned Reg,
416 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000417 if (TargetRegisterInfo::isPhysicalRegister(Reg))
418 return 0;
Richard Sandiford564681c2013-08-12 10:28:10 +0000419 return MRI->getUniqueVRegDef(Reg);
420}
421
422// Return true if MI is a shift of type Opcode by Imm bits.
423static bool isShift(MachineInstr *MI, int Opcode, int64_t Imm) {
424 return (MI->getOpcode() == Opcode &&
425 !MI->getOperand(2).getReg() &&
426 MI->getOperand(3).getImm() == Imm);
427}
428
Richard Sandiforda5901252013-08-16 10:22:54 +0000429// If the destination of MI has no uses, delete it as dead.
430static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
431 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
432 MI->eraseFromParent();
433}
434
Richard Sandiford564681c2013-08-12 10:28:10 +0000435// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000436// the result of an IPM sequence whose input CC survives until Compare,
437// and whether Compare is therefore redundant. Delete it and return
438// true if so.
439static bool removeIPMBasedCompare(MachineInstr *Compare, unsigned SrcReg,
440 const MachineRegisterInfo *MRI,
441 const TargetRegisterInfo *TRI) {
Richard Sandiforde3827752013-08-16 10:55:47 +0000442 MachineInstr *LGFR = 0;
Richard Sandiforda5901252013-08-16 10:22:54 +0000443 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000444 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
445 LGFR = RLL;
446 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
447 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000448 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000449 return false;
450
Richard Sandiforda5901252013-08-16 10:22:54 +0000451 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
452 if (!SRL || !isShift(SRL, SystemZ::SRL, 28))
Richard Sandiford564681c2013-08-12 10:28:10 +0000453 return false;
454
Richard Sandiforda5901252013-08-16 10:22:54 +0000455 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000456 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
457 return false;
458
459 // Check that there are no assignments to CC between the IPM and Compare,
Richard Sandiford564681c2013-08-12 10:28:10 +0000460 if (IPM->getParent() != Compare->getParent())
461 return false;
462 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare;
463 for (++MBBI; MBBI != MBBE; ++MBBI) {
464 MachineInstr *MI = MBBI;
Richard Sandiforda5901252013-08-16 10:22:54 +0000465 if (MI->modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000466 return false;
467 }
468
Richard Sandiford564681c2013-08-12 10:28:10 +0000469 Compare->eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000470 if (LGFR)
471 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000472 eraseIfDead(RLL, MRI);
473 eraseIfDead(SRL, MRI);
474 eraseIfDead(IPM, MRI);
475
Richard Sandiford564681c2013-08-12 10:28:10 +0000476 return true;
477}
478
479bool
480SystemZInstrInfo::optimizeCompareInstr(MachineInstr *Compare,
481 unsigned SrcReg, unsigned SrcReg2,
482 int Mask, int Value,
483 const MachineRegisterInfo *MRI) const {
484 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
485 bool IsLogical = (Compare->getDesc().TSFlags & SystemZII::IsLogical) != 0;
486 if (Value == 0 &&
487 !IsLogical &&
Richard Sandiforda5901252013-08-16 10:22:54 +0000488 removeIPMBasedCompare(Compare, SrcReg, MRI, TM.getRegisterInfo()))
Richard Sandiford564681c2013-08-12 10:28:10 +0000489 return true;
490 return false;
491}
492
Richard Sandifordf2404162013-07-25 09:11:15 +0000493// If Opcode is a move that has a conditional variant, return that variant,
494// otherwise return 0.
495static unsigned getConditionalMove(unsigned Opcode) {
496 switch (Opcode) {
497 case SystemZ::LR: return SystemZ::LOCR;
498 case SystemZ::LGR: return SystemZ::LOCGR;
499 default: return 0;
500 }
501}
502
503bool SystemZInstrInfo::isPredicable(MachineInstr *MI) const {
504 unsigned Opcode = MI->getOpcode();
505 if (TM.getSubtargetImpl()->hasLoadStoreOnCond() &&
506 getConditionalMove(Opcode))
507 return true;
508 return false;
509}
510
511bool SystemZInstrInfo::
512isProfitableToIfCvt(MachineBasicBlock &MBB,
513 unsigned NumCycles, unsigned ExtraPredCycles,
514 const BranchProbability &Probability) const {
515 // For now only convert single instructions.
516 return NumCycles == 1;
517}
518
519bool SystemZInstrInfo::
520isProfitableToIfCvt(MachineBasicBlock &TMBB,
521 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
522 MachineBasicBlock &FMBB,
523 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
524 const BranchProbability &Probability) const {
525 // For now avoid converting mutually-exclusive cases.
526 return false;
527}
528
529bool SystemZInstrInfo::
530PredicateInstruction(MachineInstr *MI,
531 const SmallVectorImpl<MachineOperand> &Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000532 assert(Pred.size() == 2 && "Invalid condition");
533 unsigned CCValid = Pred[0].getImm();
534 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000535 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
536 unsigned Opcode = MI->getOpcode();
537 if (TM.getSubtargetImpl()->hasLoadStoreOnCond()) {
538 if (unsigned CondOpcode = getConditionalMove(Opcode)) {
539 MI->setDesc(get(CondOpcode));
Richard Sandiford3d768e32013-07-31 12:30:20 +0000540 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000541 .addImm(CCValid).addImm(CCMask)
542 .addReg(SystemZ::CC, RegState::Implicit);;
Richard Sandifordf2404162013-07-25 09:11:15 +0000543 return true;
544 }
545 }
546 return false;
547}
548
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000549void
550SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
551 MachineBasicBlock::iterator MBBI, DebugLoc DL,
552 unsigned DestReg, unsigned SrcReg,
553 bool KillSrc) const {
554 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
555 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000556 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
557 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
558 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
559 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000560 return;
561 }
562
Richard Sandiford0755c932013-10-01 11:26:28 +0000563 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
564 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
565 return;
566 }
567
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000568 // Everything else needs only one instruction.
569 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000570 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000571 Opcode = SystemZ::LGR;
572 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
573 Opcode = SystemZ::LER;
574 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
575 Opcode = SystemZ::LDR;
576 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
577 Opcode = SystemZ::LXR;
578 else
579 llvm_unreachable("Impossible reg-to-reg copy");
580
581 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
582 .addReg(SrcReg, getKillRegState(KillSrc));
583}
584
585void
586SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
587 MachineBasicBlock::iterator MBBI,
588 unsigned SrcReg, bool isKill,
589 int FrameIdx,
590 const TargetRegisterClass *RC,
591 const TargetRegisterInfo *TRI) const {
592 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
593
594 // Callers may expect a single instruction, so keep 128-bit moves
595 // together for now and lower them after register allocation.
596 unsigned LoadOpcode, StoreOpcode;
597 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
598 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
599 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
600}
601
602void
603SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
604 MachineBasicBlock::iterator MBBI,
605 unsigned DestReg, int FrameIdx,
606 const TargetRegisterClass *RC,
607 const TargetRegisterInfo *TRI) const {
608 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
609
610 // Callers may expect a single instruction, so keep 128-bit moves
611 // together for now and lower them after register allocation.
612 unsigned LoadOpcode, StoreOpcode;
613 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
614 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
615 FrameIdx);
616}
617
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000618// Return true if MI is a simple load or store with a 12-bit displacement
619// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
620static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
621 const MCInstrDesc &MCID = MI->getDesc();
622 return ((MCID.TSFlags & Flag) &&
623 isUInt<12>(MI->getOperand(2).getImm()) &&
624 MI->getOperand(3).getReg() == 0);
625}
626
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000627namespace {
628 struct LogicOp {
629 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
630 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
631 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
632
633 operator bool() const { return RegSize; }
634
635 unsigned RegSize, ImmLSB, ImmSize;
636 };
637}
638
639static LogicOp interpretAndImmediate(unsigned Opcode) {
640 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000641 case SystemZ::NILMux: return LogicOp(32, 0, 16);
642 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000643 case SystemZ::NILL64: return LogicOp(64, 0, 16);
644 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000645 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
646 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
647 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000648 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000649 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000650 default: return LogicOp();
651 }
652}
653
654// Used to return from convertToThreeAddress after replacing two-address
655// instruction OldMI with three-address instruction NewMI.
656static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
657 MachineInstr *NewMI,
658 LiveVariables *LV) {
659 if (LV) {
660 unsigned NumOps = OldMI->getNumOperands();
661 for (unsigned I = 1; I < NumOps; ++I) {
662 MachineOperand &Op = OldMI->getOperand(I);
663 if (Op.isReg() && Op.isKill())
664 LV->replaceKillInstruction(Op.getReg(), OldMI, NewMI);
665 }
666 }
667 return NewMI;
668}
669
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000670MachineInstr *
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000671SystemZInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
672 MachineBasicBlock::iterator &MBBI,
673 LiveVariables *LV) const {
674 MachineInstr *MI = MBBI;
675 MachineBasicBlock *MBB = MI->getParent();
Richard Sandiford42a694f2013-10-01 14:53:46 +0000676 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000677
678 unsigned Opcode = MI->getOpcode();
679 unsigned NumOps = MI->getNumOperands();
680
681 // Try to convert something like SLL into SLLK, if supported.
682 // We prefer to keep the two-operand form where possible both
683 // because it tends to be shorter and because some instructions
684 // have memory forms that can be used during spilling.
685 if (TM.getSubtargetImpl()->hasDistinctOps()) {
Richard Sandiford42a694f2013-10-01 14:53:46 +0000686 MachineOperand &Dest = MI->getOperand(0);
687 MachineOperand &Src = MI->getOperand(1);
688 unsigned DestReg = Dest.getReg();
689 unsigned SrcReg = Src.getReg();
690 // AHIMux is only really a three-operand instruction when both operands
691 // are low registers. Try to constrain both operands to be low if
692 // possible.
693 if (Opcode == SystemZ::AHIMux &&
694 TargetRegisterInfo::isVirtualRegister(DestReg) &&
695 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
696 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
697 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
698 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
699 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
700 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000701 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
702 if (ThreeOperandOpcode >= 0) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000703 MachineInstrBuilder MIB =
704 BuildMI(*MBB, MBBI, MI->getDebugLoc(), get(ThreeOperandOpcode))
705 .addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000706 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000707 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000708 // Keep the remaining operands as-is.
709 for (unsigned I = 2; I < NumOps; ++I)
710 MIB.addOperand(MI->getOperand(I));
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000711 return finishConvertToThreeAddress(MI, MIB, LV);
712 }
713 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000714
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000715 // Try to convert an AND into an RISBG-type instruction.
716 if (LogicOp And = interpretAndImmediate(Opcode)) {
Richard Sandiford70284282013-10-01 14:20:41 +0000717 uint64_t Imm = MI->getOperand(2).getImm() << And.ImmLSB;
718 // AND IMMEDIATE leaves the other bits of the register unchanged.
719 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
720 unsigned Start, End;
721 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
722 unsigned NewOpcode;
723 if (And.RegSize == 64)
724 NewOpcode = SystemZ::RISBG;
725 else {
726 NewOpcode = SystemZ::RISBMux;
727 Start &= 31;
728 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000729 }
Richard Sandiford70284282013-10-01 14:20:41 +0000730 MachineOperand &Dest = MI->getOperand(0);
731 MachineOperand &Src = MI->getOperand(1);
732 MachineInstrBuilder MIB =
733 BuildMI(*MBB, MI, MI->getDebugLoc(), get(NewOpcode))
734 .addOperand(Dest).addReg(0)
735 .addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg())
736 .addImm(Start).addImm(End + 128).addImm(0);
737 return finishConvertToThreeAddress(MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000738 }
739 }
740 return 0;
741}
742
743MachineInstr *
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000744SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
745 MachineInstr *MI,
746 const SmallVectorImpl<unsigned> &Ops,
747 int FrameIndex) const {
748 const MachineFrameInfo *MFI = MF.getFrameInfo();
749 unsigned Size = MFI->getObjectSize(FrameIndex);
750
751 // Eary exit for cases we don't care about
752 if (Ops.size() != 1)
753 return 0;
754
755 unsigned OpNum = Ops[0];
NAKAMURA Takumiddcba562013-07-03 02:20:49 +0000756 assert(Size == MF.getRegInfo()
757 .getRegClass(MI->getOperand(OpNum).getReg())->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000758 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000759
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000760 unsigned Opcode = MI->getOpcode();
761 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
762 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
763 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
764 // If we're spilling the destination of an LDGR or LGDR, store the
765 // source register instead.
766 if (OpNum == 0) {
767 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
768 return BuildMI(MF, MI->getDebugLoc(), get(StoreOpcode))
769 .addOperand(MI->getOperand(1)).addFrameIndex(FrameIndex)
770 .addImm(0).addReg(0);
771 }
772 // If we're spilling the source of an LDGR or LGDR, load the
773 // destination register instead.
774 if (OpNum == 1) {
775 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
776 unsigned Dest = MI->getOperand(0).getReg();
777 return BuildMI(MF, MI->getDebugLoc(), get(LoadOpcode), Dest)
778 .addFrameIndex(FrameIndex).addImm(0).addReg(0);
779 }
780 }
781
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000782 // Look for cases where the source of a simple store or the destination
783 // of a simple load is being spilled. Try to use MVC instead.
784 //
785 // Although MVC is in practice a fast choice in these cases, it is still
786 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +0000787 // load or store is volatile. We also wouldn't be able to use MVC if
788 // the two memories partially overlap, but that case cannot occur here,
789 // because we know that one of the memories is a full frame index.
790 //
791 // For performance reasons, we also want to avoid using MVC if the addresses
792 // might be equal. We don't worry about that case here, because spill slot
793 // coloring happens later, and because we have special code to remove
794 // MVCs that turn out to be redundant.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000795 if (OpNum == 0 && MI->hasOneMemOperand()) {
796 MachineMemOperand *MMO = *MI->memoperands_begin();
797 if (MMO->getSize() == Size && !MMO->isVolatile()) {
798 // Handle conversion of loads.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000799 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXLoad)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000800 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000801 .addFrameIndex(FrameIndex).addImm(0).addImm(Size)
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000802 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000803 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000804 }
805 // Handle conversion of stores.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000806 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXStore)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000807 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
808 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000809 .addImm(Size).addFrameIndex(FrameIndex).addImm(0)
810 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000811 }
812 }
813 }
814
Richard Sandiforded1fab62013-07-03 10:10:02 +0000815 // If the spilled operand is the final one, try to change <INSN>R
816 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000817 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000818 if (MemOpcode >= 0) {
819 unsigned NumOps = MI->getNumExplicitOperands();
820 if (OpNum == NumOps - 1) {
821 const MCInstrDesc &MemDesc = get(MemOpcode);
822 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
823 assert(AccessBytes != 0 && "Size of access should be known");
824 assert(AccessBytes <= Size && "Access outside the frame index");
825 uint64_t Offset = Size - AccessBytes;
Richard Sandiforded1fab62013-07-03 10:10:02 +0000826 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), get(MemOpcode));
827 for (unsigned I = 0; I < OpNum; ++I)
828 MIB.addOperand(MI->getOperand(I));
829 MIB.addFrameIndex(FrameIndex).addImm(Offset);
830 if (MemDesc.TSFlags & SystemZII::HasIndex)
831 MIB.addReg(0);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000832 return MIB;
833 }
834 }
835
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000836 return 0;
837}
838
839MachineInstr *
840SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr* MI,
841 const SmallVectorImpl<unsigned> &Ops,
842 MachineInstr* LoadMI) const {
843 return 0;
844}
845
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000846bool
847SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
848 switch (MI->getOpcode()) {
849 case SystemZ::L128:
850 splitMove(MI, SystemZ::LG);
851 return true;
852
853 case SystemZ::ST128:
854 splitMove(MI, SystemZ::STG);
855 return true;
856
857 case SystemZ::LX:
858 splitMove(MI, SystemZ::LD);
859 return true;
860
861 case SystemZ::STX:
862 splitMove(MI, SystemZ::STD);
863 return true;
864
Richard Sandiford89e160d2013-10-01 12:11:47 +0000865 case SystemZ::LBMux:
866 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
867 return true;
868
869 case SystemZ::LHMux:
870 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
871 return true;
872
Richard Sandiford21235a22013-10-01 12:49:07 +0000873 case SystemZ::LLCRMux:
874 expandZExtPseudo(MI, SystemZ::LLCR, 8);
875 return true;
876
877 case SystemZ::LLHRMux:
878 expandZExtPseudo(MI, SystemZ::LLHR, 16);
879 return true;
880
Richard Sandiford0d46b1a2013-10-01 12:19:08 +0000881 case SystemZ::LLCMux:
882 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
883 return true;
884
885 case SystemZ::LLHMux:
886 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
887 return true;
888
Richard Sandiford0755c932013-10-01 11:26:28 +0000889 case SystemZ::LMux:
890 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
891 return true;
892
Richard Sandiford5469c392013-10-01 12:22:49 +0000893 case SystemZ::STCMux:
894 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
895 return true;
896
897 case SystemZ::STHMux:
898 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
899 return true;
900
Richard Sandiford0755c932013-10-01 11:26:28 +0000901 case SystemZ::STMux:
902 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
903 return true;
904
Richard Sandiford01240232013-10-01 13:02:28 +0000905 case SystemZ::LHIMux:
906 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
907 return true;
908
909 case SystemZ::IIFMux:
910 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
911 return true;
912
Richard Sandiford1a569312013-10-01 13:18:56 +0000913 case SystemZ::IILMux:
914 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
915 return true;
916
917 case SystemZ::IIHMux:
918 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
919 return true;
920
Richard Sandiford70284282013-10-01 14:20:41 +0000921 case SystemZ::NIFMux:
922 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
923 return true;
924
925 case SystemZ::NILMux:
926 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
927 return true;
928
929 case SystemZ::NIHMux:
930 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
931 return true;
932
Richard Sandiford6e96ac62013-10-01 13:22:41 +0000933 case SystemZ::OIFMux:
934 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
935 return true;
936
937 case SystemZ::OILMux:
938 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
939 return true;
940
941 case SystemZ::OIHMux:
942 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
943 return true;
944
Richard Sandiford5718dac2013-10-01 14:08:44 +0000945 case SystemZ::XIFMux:
946 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
947 return true;
948
Richard Sandiford2cac7632013-10-01 14:41:52 +0000949 case SystemZ::TMLMux:
950 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
951 return true;
952
953 case SystemZ::TMHMux:
954 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
955 return true;
956
Richard Sandiford42a694f2013-10-01 14:53:46 +0000957 case SystemZ::AHIMux:
958 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
959 return true;
960
961 case SystemZ::AHIMuxK:
962 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
963 return true;
964
965 case SystemZ::AFIMux:
966 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
967 return true;
968
Richard Sandiforda9ac0e02013-10-01 14:56:23 +0000969 case SystemZ::CFIMux:
970 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
971 return true;
972
973 case SystemZ::CLFIMux:
974 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
975 return true;
976
Richard Sandifordb63e3002013-10-01 15:00:44 +0000977 case SystemZ::CMux:
978 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
979 return true;
980
981 case SystemZ::CLMux:
982 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
983 return true;
984
Richard Sandiford70284282013-10-01 14:20:41 +0000985 case SystemZ::RISBMux: {
986 bool DestIsHigh = isHighReg(MI->getOperand(0).getReg());
987 bool SrcIsHigh = isHighReg(MI->getOperand(2).getReg());
988 if (SrcIsHigh == DestIsHigh)
989 MI->setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
990 else {
991 MI->setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
992 MI->getOperand(5).setImm(MI->getOperand(5).getImm() ^ 32);
993 }
994 return true;
995 }
996
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000997 case SystemZ::ADJDYNALLOC:
998 splitAdjDynAlloc(MI);
999 return true;
1000
1001 default:
1002 return false;
1003 }
1004}
1005
Richard Sandiford312425f2013-05-20 14:23:08 +00001006uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
1007 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
1008 const MachineFunction *MF = MI->getParent()->getParent();
1009 const char *AsmStr = MI->getOperand(0).getSymbolName();
1010 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1011 }
1012 return MI->getDesc().getSize();
1013}
1014
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001015SystemZII::Branch
1016SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001017 switch (MI->getOpcode()) {
1018 case SystemZ::BR:
1019 case SystemZ::J:
1020 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001021 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Richard Sandiford3d768e32013-07-31 12:30:20 +00001022 SystemZ::CCMASK_ANY, &MI->getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001023
1024 case SystemZ::BRC:
1025 case SystemZ::BRCL:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001026 return SystemZII::Branch(SystemZII::BranchNormal,
Richard Sandiford3d768e32013-07-31 12:30:20 +00001027 MI->getOperand(0).getImm(),
1028 MI->getOperand(1).getImm(), &MI->getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001029
Richard Sandifordc2121252013-08-05 11:23:46 +00001030 case SystemZ::BRCT:
1031 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
1032 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
1033
1034 case SystemZ::BRCTG:
1035 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
1036 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
1037
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001038 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001039 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001040 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
1041 MI->getOperand(2).getImm(), &MI->getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001042
Richard Sandiford93183ee2013-09-18 09:56:40 +00001043 case SystemZ::CLIJ:
1044 case SystemZ::CLRJ:
1045 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
1046 MI->getOperand(2).getImm(), &MI->getOperand(3));
1047
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001048 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001049 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001050 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
1051 MI->getOperand(2).getImm(), &MI->getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001052
Richard Sandiford93183ee2013-09-18 09:56:40 +00001053 case SystemZ::CLGIJ:
1054 case SystemZ::CLGRJ:
1055 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
1056 MI->getOperand(2).getImm(), &MI->getOperand(3));
1057
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001058 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001059 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001060 }
1061}
1062
1063void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1064 unsigned &LoadOpcode,
1065 unsigned &StoreOpcode) const {
1066 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1067 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001068 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001069 } else if (RC == &SystemZ::GRH32BitRegClass) {
1070 LoadOpcode = SystemZ::LFH;
1071 StoreOpcode = SystemZ::STFH;
1072 } else if (RC == &SystemZ::GRX32BitRegClass) {
1073 LoadOpcode = SystemZ::LMux;
1074 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001075 } else if (RC == &SystemZ::GR64BitRegClass ||
1076 RC == &SystemZ::ADDR64BitRegClass) {
1077 LoadOpcode = SystemZ::LG;
1078 StoreOpcode = SystemZ::STG;
1079 } else if (RC == &SystemZ::GR128BitRegClass ||
1080 RC == &SystemZ::ADDR128BitRegClass) {
1081 LoadOpcode = SystemZ::L128;
1082 StoreOpcode = SystemZ::ST128;
1083 } else if (RC == &SystemZ::FP32BitRegClass) {
1084 LoadOpcode = SystemZ::LE;
1085 StoreOpcode = SystemZ::STE;
1086 } else if (RC == &SystemZ::FP64BitRegClass) {
1087 LoadOpcode = SystemZ::LD;
1088 StoreOpcode = SystemZ::STD;
1089 } else if (RC == &SystemZ::FP128BitRegClass) {
1090 LoadOpcode = SystemZ::LX;
1091 StoreOpcode = SystemZ::STX;
1092 } else
1093 llvm_unreachable("Unsupported regclass to load or store");
1094}
1095
1096unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1097 int64_t Offset) const {
1098 const MCInstrDesc &MCID = get(Opcode);
1099 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1100 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1101 // Get the instruction to use for unsigned 12-bit displacements.
1102 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1103 if (Disp12Opcode >= 0)
1104 return Disp12Opcode;
1105
1106 // All address-related instructions can use unsigned 12-bit
1107 // displacements.
1108 return Opcode;
1109 }
1110 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1111 // Get the instruction to use for signed 20-bit displacements.
1112 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1113 if (Disp20Opcode >= 0)
1114 return Disp20Opcode;
1115
1116 // Check whether Opcode allows signed 20-bit displacements.
1117 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1118 return Opcode;
1119 }
1120 return 0;
1121}
1122
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001123unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1124 switch (Opcode) {
1125 case SystemZ::L: return SystemZ::LT;
1126 case SystemZ::LY: return SystemZ::LT;
1127 case SystemZ::LG: return SystemZ::LTG;
1128 case SystemZ::LGF: return SystemZ::LTGF;
1129 case SystemZ::LR: return SystemZ::LTR;
1130 case SystemZ::LGFR: return SystemZ::LTGFR;
1131 case SystemZ::LGR: return SystemZ::LTGR;
Richard Sandiford0897fce2013-08-07 11:10:06 +00001132 case SystemZ::LER: return SystemZ::LTEBR;
1133 case SystemZ::LDR: return SystemZ::LTDBR;
1134 case SystemZ::LXR: return SystemZ::LTXBR;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001135 default: return 0;
1136 }
1137}
1138
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001139// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1140// have already been filtered out. Store the first set bit in LSB and
1141// the number of set bits in Length if so.
1142static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1143 unsigned First = findFirstSet(Mask);
1144 uint64_t Top = (Mask >> First) + 1;
1145 if ((Top & -Top) == Top) {
1146 LSB = First;
1147 Length = findFirstSet(Top);
1148 return true;
1149 }
1150 return false;
1151}
1152
1153bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1154 unsigned &Start, unsigned &End) const {
1155 // Reject trivial all-zero masks.
1156 if (Mask == 0)
1157 return false;
1158
1159 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1160 // the msb and End specifies the index of the lsb.
1161 unsigned LSB, Length;
1162 if (isStringOfOnes(Mask, LSB, Length)) {
1163 Start = 63 - (LSB + Length - 1);
1164 End = 63 - LSB;
1165 return true;
1166 }
1167
1168 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1169 // of the low 1s and End specifies the lsb of the high 1s.
1170 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1171 assert(LSB > 0 && "Bottom bit must be set");
1172 assert(LSB + Length < BitSize && "Top bit must be set");
1173 Start = 63 - (LSB - 1);
1174 End = 63 - (LSB + Length);
1175 return true;
1176 }
1177
1178 return false;
1179}
1180
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001181unsigned SystemZInstrInfo::getCompareAndBranch(unsigned Opcode,
1182 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001183 switch (Opcode) {
1184 case SystemZ::CR:
1185 return SystemZ::CRJ;
1186 case SystemZ::CGR:
1187 return SystemZ::CGRJ;
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001188 case SystemZ::CHI:
1189 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CIJ : 0;
1190 case SystemZ::CGHI:
1191 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CGIJ : 0;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001192 case SystemZ::CLR:
1193 return SystemZ::CLRJ;
1194 case SystemZ::CLGR:
1195 return SystemZ::CLGRJ;
1196 case SystemZ::CLFI:
1197 return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLIJ : 0;
1198 case SystemZ::CLGFI:
1199 return MI && isUInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CLGIJ : 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001200 default:
1201 return 0;
1202 }
1203}
1204
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001205void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1206 MachineBasicBlock::iterator MBBI,
1207 unsigned Reg, uint64_t Value) const {
1208 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1209 unsigned Opcode;
1210 if (isInt<16>(Value))
1211 Opcode = SystemZ::LGHI;
1212 else if (SystemZ::isImmLL(Value))
1213 Opcode = SystemZ::LLILL;
1214 else if (SystemZ::isImmLH(Value)) {
1215 Opcode = SystemZ::LLILH;
1216 Value >>= 16;
1217 } else {
1218 assert(isInt<32>(Value) && "Huge values not handled yet");
1219 Opcode = SystemZ::LGFI;
1220 }
1221 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1222}