blob: f2e877fd29b01e9eeda702f9de5838b46aa4f5a9 [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
Ulrich Weigand5f613df2013-05-06 16:15:19 +000031SystemZInstrInfo::SystemZInstrInfo(SystemZTargetMachine &tm)
32 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
Richard Sandifordff6c5a52013-07-19 16:12:08 +000033 RI(tm), TM(tm) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000034}
35
36// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
37// each having the opcode given by NewOpcode.
38void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
39 unsigned NewOpcode) const {
40 MachineBasicBlock *MBB = MI->getParent();
41 MachineFunction &MF = *MBB->getParent();
42
43 // Get two load or store instructions. Use the original instruction for one
44 // of them (arbitarily the second here) and create a clone for the other.
45 MachineInstr *EarlierMI = MF.CloneMachineInstr(MI);
46 MBB->insert(MI, EarlierMI);
47
48 // Set up the two 64-bit registers.
49 MachineOperand &HighRegOp = EarlierMI->getOperand(0);
50 MachineOperand &LowRegOp = MI->getOperand(0);
51 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_high));
52 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_low));
53
54 // The address in the first (high) instruction is already correct.
55 // Adjust the offset in the second (low) instruction.
56 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
57 MachineOperand &LowOffsetOp = MI->getOperand(2);
58 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
59
60 // Set the opcodes.
61 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
62 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
63 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
64
65 EarlierMI->setDesc(get(HighOpcode));
66 MI->setDesc(get(LowOpcode));
67}
68
69// Split ADJDYNALLOC instruction MI.
70void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
71 MachineBasicBlock *MBB = MI->getParent();
72 MachineFunction &MF = *MBB->getParent();
73 MachineFrameInfo *MFFrame = MF.getFrameInfo();
74 MachineOperand &OffsetMO = MI->getOperand(2);
75
76 uint64_t Offset = (MFFrame->getMaxCallFrameSize() +
77 SystemZMC::CallFrameSize +
78 OffsetMO.getImm());
79 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
80 assert(NewOpcode && "No support for huge argument lists yet");
81 MI->setDesc(get(NewOpcode));
82 OffsetMO.setImm(Offset);
83}
84
85// If MI is a simple load or store for a frame object, return the register
86// it loads or stores and set FrameIndex to the index of the frame object.
87// Return 0 otherwise.
88//
89// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +000090static int isSimpleMove(const MachineInstr *MI, int &FrameIndex,
91 unsigned Flag) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000092 const MCInstrDesc &MCID = MI->getDesc();
93 if ((MCID.TSFlags & Flag) &&
94 MI->getOperand(1).isFI() &&
95 MI->getOperand(2).getImm() == 0 &&
96 MI->getOperand(3).getReg() == 0) {
97 FrameIndex = MI->getOperand(1).getIndex();
98 return MI->getOperand(0).getReg();
99 }
100 return 0;
101}
102
103unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
104 int &FrameIndex) const {
105 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
106}
107
108unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
109 int &FrameIndex) const {
110 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
111}
112
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000113bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr *MI,
114 int &DestFrameIndex,
115 int &SrcFrameIndex) const {
116 // Check for MVC 0(Length,FI1),0(FI2)
117 const MachineFrameInfo *MFI = MI->getParent()->getParent()->getFrameInfo();
118 if (MI->getOpcode() != SystemZ::MVC ||
119 !MI->getOperand(0).isFI() ||
120 MI->getOperand(1).getImm() != 0 ||
121 !MI->getOperand(3).isFI() ||
122 MI->getOperand(4).getImm() != 0)
123 return false;
124
125 // Check that Length covers the full slots.
126 int64_t Length = MI->getOperand(2).getImm();
127 unsigned FI1 = MI->getOperand(0).getIndex();
128 unsigned FI2 = MI->getOperand(3).getIndex();
129 if (MFI->getObjectSize(FI1) != Length ||
130 MFI->getObjectSize(FI2) != Length)
131 return false;
132
133 DestFrameIndex = FI1;
134 SrcFrameIndex = FI2;
135 return true;
136}
137
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000138bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
139 MachineBasicBlock *&TBB,
140 MachineBasicBlock *&FBB,
141 SmallVectorImpl<MachineOperand> &Cond,
142 bool AllowModify) const {
143 // Most of the code and comments here are boilerplate.
144
145 // Start from the bottom of the block and work up, examining the
146 // terminator instructions.
147 MachineBasicBlock::iterator I = MBB.end();
148 while (I != MBB.begin()) {
149 --I;
150 if (I->isDebugValue())
151 continue;
152
153 // Working from the bottom, when we see a non-terminator instruction, we're
154 // done.
155 if (!isUnpredicatedTerminator(I))
156 break;
157
158 // A terminator that isn't a branch can't easily be handled by this
159 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000160 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000161 return true;
162
163 // Can't handle indirect branches.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000164 SystemZII::Branch Branch(getBranchInfo(I));
165 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000166 return true;
167
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000168 // Punt on compound branches.
169 if (Branch.Type != SystemZII::BranchNormal)
170 return true;
171
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000172 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000173 // Handle unconditional branches.
174 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000175 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000176 continue;
177 }
178
179 // If the block has any instructions after a JMP, delete them.
180 while (llvm::next(I) != MBB.end())
181 llvm::next(I)->eraseFromParent();
182
183 Cond.clear();
184 FBB = 0;
185
186 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000187 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000188 TBB = 0;
189 I->eraseFromParent();
190 I = MBB.end();
191 continue;
192 }
193
194 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000195 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 continue;
197 }
198
199 // Working from the bottom, handle the first conditional branch.
200 if (Cond.empty()) {
201 // FIXME: add X86-style branch swap
202 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000203 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000204 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000205 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000206 continue;
207 }
208
209 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000210 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000211
212 // Only handle the case where all conditional branches branch to the same
213 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000214 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000215 return true;
216
217 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000218 unsigned OldCCValid = Cond[0].getImm();
219 unsigned OldCCMask = Cond[1].getImm();
220 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000221 continue;
222
223 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000224 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000225 }
226
227 return false;
228}
229
230unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
231 // Most of the code and comments here are boilerplate.
232 MachineBasicBlock::iterator I = MBB.end();
233 unsigned Count = 0;
234
235 while (I != MBB.begin()) {
236 --I;
237 if (I->isDebugValue())
238 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000239 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000240 break;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000241 if (!getBranchInfo(I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000242 break;
243 // Remove the branch.
244 I->eraseFromParent();
245 I = MBB.end();
246 ++Count;
247 }
248
249 return Count;
250}
251
Richard Sandiford3d768e32013-07-31 12:30:20 +0000252bool SystemZInstrInfo::
253ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
254 assert(Cond.size() == 2 && "Invalid condition");
255 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
256 return false;
257}
258
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000259unsigned
260SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
261 MachineBasicBlock *FBB,
262 const SmallVectorImpl<MachineOperand> &Cond,
263 DebugLoc DL) const {
264 // In this function we output 32-bit branches, which should always
265 // have enough range. They can be shortened and relaxed by later code
266 // in the pipeline, if desired.
267
268 // Shouldn't be a fall through.
269 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000270 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000271 "SystemZ branch conditions have one component!");
272
273 if (Cond.empty()) {
274 // Unconditional branch?
275 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000276 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000277 return 1;
278 }
279
280 // Conditional branch.
281 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000282 unsigned CCValid = Cond[0].getImm();
283 unsigned CCMask = Cond[1].getImm();
284 BuildMI(&MBB, DL, get(SystemZ::BRC))
285 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000286 ++Count;
287
288 if (FBB) {
289 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000290 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000291 ++Count;
292 }
293 return Count;
294}
295
Richard Sandiford564681c2013-08-12 10:28:10 +0000296bool SystemZInstrInfo::analyzeCompare(const MachineInstr *MI,
297 unsigned &SrcReg, unsigned &SrcReg2,
298 int &Mask, int &Value) const {
299 assert(MI->isCompare() && "Caller should have checked for a comparison");
300
301 if (MI->getNumExplicitOperands() == 2 &&
302 MI->getOperand(0).isReg() &&
303 MI->getOperand(1).isImm()) {
304 SrcReg = MI->getOperand(0).getReg();
305 SrcReg2 = 0;
306 Value = MI->getOperand(1).getImm();
307 Mask = ~0;
308 return true;
309 }
310
311 return false;
312}
313
Richard Sandiforda5901252013-08-16 10:22:54 +0000314// If Reg is a virtual register, return its definition, otherwise return null.
315static MachineInstr *getDef(unsigned Reg,
316 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000317 if (TargetRegisterInfo::isPhysicalRegister(Reg))
318 return 0;
Richard Sandiford564681c2013-08-12 10:28:10 +0000319 return MRI->getUniqueVRegDef(Reg);
320}
321
322// Return true if MI is a shift of type Opcode by Imm bits.
323static bool isShift(MachineInstr *MI, int Opcode, int64_t Imm) {
324 return (MI->getOpcode() == Opcode &&
325 !MI->getOperand(2).getReg() &&
326 MI->getOperand(3).getImm() == Imm);
327}
328
Richard Sandiforda5901252013-08-16 10:22:54 +0000329// If the destination of MI has no uses, delete it as dead.
330static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
331 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
332 MI->eraseFromParent();
333}
334
Richard Sandiford564681c2013-08-12 10:28:10 +0000335// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000336// the result of an IPM sequence whose input CC survives until Compare,
337// and whether Compare is therefore redundant. Delete it and return
338// true if so.
339static bool removeIPMBasedCompare(MachineInstr *Compare, unsigned SrcReg,
340 const MachineRegisterInfo *MRI,
341 const TargetRegisterInfo *TRI) {
342 MachineInstr *RLL = getDef(SrcReg, MRI);
343 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000344 return false;
345
Richard Sandiforda5901252013-08-16 10:22:54 +0000346 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
347 if (!SRL || !isShift(SRL, SystemZ::SRL, 28))
Richard Sandiford564681c2013-08-12 10:28:10 +0000348 return false;
349
Richard Sandiforda5901252013-08-16 10:22:54 +0000350 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000351 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
352 return false;
353
354 // Check that there are no assignments to CC between the IPM and Compare,
Richard Sandiford564681c2013-08-12 10:28:10 +0000355 if (IPM->getParent() != Compare->getParent())
356 return false;
357 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare;
358 for (++MBBI; MBBI != MBBE; ++MBBI) {
359 MachineInstr *MI = MBBI;
Richard Sandiforda5901252013-08-16 10:22:54 +0000360 if (MI->modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000361 return false;
362 }
363
Richard Sandiford564681c2013-08-12 10:28:10 +0000364 Compare->eraseFromParent();
Richard Sandiforda5901252013-08-16 10:22:54 +0000365 eraseIfDead(RLL, MRI);
366 eraseIfDead(SRL, MRI);
367 eraseIfDead(IPM, MRI);
368
Richard Sandiford564681c2013-08-12 10:28:10 +0000369 return true;
370}
371
372bool
373SystemZInstrInfo::optimizeCompareInstr(MachineInstr *Compare,
374 unsigned SrcReg, unsigned SrcReg2,
375 int Mask, int Value,
376 const MachineRegisterInfo *MRI) const {
377 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
378 bool IsLogical = (Compare->getDesc().TSFlags & SystemZII::IsLogical) != 0;
379 if (Value == 0 &&
380 !IsLogical &&
Richard Sandiforda5901252013-08-16 10:22:54 +0000381 removeIPMBasedCompare(Compare, SrcReg, MRI, TM.getRegisterInfo()))
Richard Sandiford564681c2013-08-12 10:28:10 +0000382 return true;
383 return false;
384}
385
Richard Sandifordf2404162013-07-25 09:11:15 +0000386// If Opcode is a move that has a conditional variant, return that variant,
387// otherwise return 0.
388static unsigned getConditionalMove(unsigned Opcode) {
389 switch (Opcode) {
390 case SystemZ::LR: return SystemZ::LOCR;
391 case SystemZ::LGR: return SystemZ::LOCGR;
392 default: return 0;
393 }
394}
395
396bool SystemZInstrInfo::isPredicable(MachineInstr *MI) const {
397 unsigned Opcode = MI->getOpcode();
398 if (TM.getSubtargetImpl()->hasLoadStoreOnCond() &&
399 getConditionalMove(Opcode))
400 return true;
401 return false;
402}
403
404bool SystemZInstrInfo::
405isProfitableToIfCvt(MachineBasicBlock &MBB,
406 unsigned NumCycles, unsigned ExtraPredCycles,
407 const BranchProbability &Probability) const {
408 // For now only convert single instructions.
409 return NumCycles == 1;
410}
411
412bool SystemZInstrInfo::
413isProfitableToIfCvt(MachineBasicBlock &TMBB,
414 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
415 MachineBasicBlock &FMBB,
416 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
417 const BranchProbability &Probability) const {
418 // For now avoid converting mutually-exclusive cases.
419 return false;
420}
421
422bool SystemZInstrInfo::
423PredicateInstruction(MachineInstr *MI,
424 const SmallVectorImpl<MachineOperand> &Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000425 assert(Pred.size() == 2 && "Invalid condition");
426 unsigned CCValid = Pred[0].getImm();
427 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000428 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
429 unsigned Opcode = MI->getOpcode();
430 if (TM.getSubtargetImpl()->hasLoadStoreOnCond()) {
431 if (unsigned CondOpcode = getConditionalMove(Opcode)) {
432 MI->setDesc(get(CondOpcode));
Richard Sandiford3d768e32013-07-31 12:30:20 +0000433 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +0000434 .addImm(CCValid).addImm(CCMask)
435 .addReg(SystemZ::CC, RegState::Implicit);;
Richard Sandifordf2404162013-07-25 09:11:15 +0000436 return true;
437 }
438 }
439 return false;
440}
441
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000442void
443SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
444 MachineBasicBlock::iterator MBBI, DebugLoc DL,
445 unsigned DestReg, unsigned SrcReg,
446 bool KillSrc) const {
447 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
448 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
449 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_high),
450 RI.getSubReg(SrcReg, SystemZ::subreg_high), KillSrc);
451 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_low),
452 RI.getSubReg(SrcReg, SystemZ::subreg_low), KillSrc);
453 return;
454 }
455
456 // Everything else needs only one instruction.
457 unsigned Opcode;
458 if (SystemZ::GR32BitRegClass.contains(DestReg, SrcReg))
459 Opcode = SystemZ::LR;
460 else if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
461 Opcode = SystemZ::LGR;
462 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
463 Opcode = SystemZ::LER;
464 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
465 Opcode = SystemZ::LDR;
466 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
467 Opcode = SystemZ::LXR;
468 else
469 llvm_unreachable("Impossible reg-to-reg copy");
470
471 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
472 .addReg(SrcReg, getKillRegState(KillSrc));
473}
474
475void
476SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
477 MachineBasicBlock::iterator MBBI,
478 unsigned SrcReg, bool isKill,
479 int FrameIdx,
480 const TargetRegisterClass *RC,
481 const TargetRegisterInfo *TRI) const {
482 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
483
484 // Callers may expect a single instruction, so keep 128-bit moves
485 // together for now and lower them after register allocation.
486 unsigned LoadOpcode, StoreOpcode;
487 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
488 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
489 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
490}
491
492void
493SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
494 MachineBasicBlock::iterator MBBI,
495 unsigned DestReg, int FrameIdx,
496 const TargetRegisterClass *RC,
497 const TargetRegisterInfo *TRI) const {
498 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
499
500 // Callers may expect a single instruction, so keep 128-bit moves
501 // together for now and lower them after register allocation.
502 unsigned LoadOpcode, StoreOpcode;
503 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
504 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
505 FrameIdx);
506}
507
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000508// Return true if MI is a simple load or store with a 12-bit displacement
509// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
510static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
511 const MCInstrDesc &MCID = MI->getDesc();
512 return ((MCID.TSFlags & Flag) &&
513 isUInt<12>(MI->getOperand(2).getImm()) &&
514 MI->getOperand(3).getReg() == 0);
515}
516
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000517namespace {
518 struct LogicOp {
519 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
520 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
521 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
522
523 operator bool() const { return RegSize; }
524
525 unsigned RegSize, ImmLSB, ImmSize;
526 };
527}
528
529static LogicOp interpretAndImmediate(unsigned Opcode) {
530 switch (Opcode) {
531 case SystemZ::NILL32: return LogicOp(32, 0, 16);
532 case SystemZ::NILH32: return LogicOp(32, 16, 16);
533 case SystemZ::NILL: return LogicOp(64, 0, 16);
534 case SystemZ::NILH: return LogicOp(64, 16, 16);
535 case SystemZ::NIHL: return LogicOp(64, 32, 16);
536 case SystemZ::NIHH: return LogicOp(64, 48, 16);
537 case SystemZ::NILF32: return LogicOp(32, 0, 32);
538 case SystemZ::NILF: return LogicOp(64, 0, 32);
539 case SystemZ::NIHF: return LogicOp(64, 32, 32);
540 default: return LogicOp();
541 }
542}
543
544// Used to return from convertToThreeAddress after replacing two-address
545// instruction OldMI with three-address instruction NewMI.
546static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
547 MachineInstr *NewMI,
548 LiveVariables *LV) {
549 if (LV) {
550 unsigned NumOps = OldMI->getNumOperands();
551 for (unsigned I = 1; I < NumOps; ++I) {
552 MachineOperand &Op = OldMI->getOperand(I);
553 if (Op.isReg() && Op.isKill())
554 LV->replaceKillInstruction(Op.getReg(), OldMI, NewMI);
555 }
556 }
557 return NewMI;
558}
559
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000560MachineInstr *
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000561SystemZInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
562 MachineBasicBlock::iterator &MBBI,
563 LiveVariables *LV) const {
564 MachineInstr *MI = MBBI;
565 MachineBasicBlock *MBB = MI->getParent();
566
567 unsigned Opcode = MI->getOpcode();
568 unsigned NumOps = MI->getNumOperands();
569
570 // Try to convert something like SLL into SLLK, if supported.
571 // We prefer to keep the two-operand form where possible both
572 // because it tends to be shorter and because some instructions
573 // have memory forms that can be used during spilling.
574 if (TM.getSubtargetImpl()->hasDistinctOps()) {
575 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
576 if (ThreeOperandOpcode >= 0) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000577 MachineOperand &Dest = MI->getOperand(0);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000578 MachineOperand &Src = MI->getOperand(1);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000579 MachineInstrBuilder MIB =
580 BuildMI(*MBB, MBBI, MI->getDebugLoc(), get(ThreeOperandOpcode))
581 .addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000582 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000583 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000584 // Keep the remaining operands as-is.
585 for (unsigned I = 2; I < NumOps; ++I)
586 MIB.addOperand(MI->getOperand(I));
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000587 return finishConvertToThreeAddress(MI, MIB, LV);
588 }
589 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000590
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000591 // Try to convert an AND into an RISBG-type instruction.
592 if (LogicOp And = interpretAndImmediate(Opcode)) {
593 unsigned NewOpcode;
594 if (And.RegSize == 64)
595 NewOpcode = SystemZ::RISBG;
596 else if (TM.getSubtargetImpl()->hasHighWord())
597 NewOpcode = SystemZ::RISBLG32;
598 else
599 // We can't use RISBG for 32-bit operations because it clobbers the
600 // high word of the destination too.
601 NewOpcode = 0;
602 if (NewOpcode) {
603 uint64_t Imm = MI->getOperand(2).getImm() << And.ImmLSB;
604 // AND IMMEDIATE leaves the other bits of the register unchanged.
605 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
606 unsigned Start, End;
607 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
608 if (NewOpcode == SystemZ::RISBLG32) {
609 Start &= 31;
610 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000611 }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000612 MachineOperand &Dest = MI->getOperand(0);
613 MachineOperand &Src = MI->getOperand(1);
614 MachineInstrBuilder MIB =
615 BuildMI(*MBB, MI, MI->getDebugLoc(), get(NewOpcode))
616 .addOperand(Dest).addReg(0)
617 .addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg())
618 .addImm(Start).addImm(End + 128).addImm(0);
619 return finishConvertToThreeAddress(MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000620 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000621 }
622 }
623 return 0;
624}
625
626MachineInstr *
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000627SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
628 MachineInstr *MI,
629 const SmallVectorImpl<unsigned> &Ops,
630 int FrameIndex) const {
631 const MachineFrameInfo *MFI = MF.getFrameInfo();
632 unsigned Size = MFI->getObjectSize(FrameIndex);
633
634 // Eary exit for cases we don't care about
635 if (Ops.size() != 1)
636 return 0;
637
638 unsigned OpNum = Ops[0];
NAKAMURA Takumiddcba562013-07-03 02:20:49 +0000639 assert(Size == MF.getRegInfo()
640 .getRegClass(MI->getOperand(OpNum).getReg())->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000641 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000642
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000643 unsigned Opcode = MI->getOpcode();
644 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
645 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
646 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
647 // If we're spilling the destination of an LDGR or LGDR, store the
648 // source register instead.
649 if (OpNum == 0) {
650 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
651 return BuildMI(MF, MI->getDebugLoc(), get(StoreOpcode))
652 .addOperand(MI->getOperand(1)).addFrameIndex(FrameIndex)
653 .addImm(0).addReg(0);
654 }
655 // If we're spilling the source of an LDGR or LGDR, load the
656 // destination register instead.
657 if (OpNum == 1) {
658 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
659 unsigned Dest = MI->getOperand(0).getReg();
660 return BuildMI(MF, MI->getDebugLoc(), get(LoadOpcode), Dest)
661 .addFrameIndex(FrameIndex).addImm(0).addReg(0);
662 }
663 }
664
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000665 // Look for cases where the source of a simple store or the destination
666 // of a simple load is being spilled. Try to use MVC instead.
667 //
668 // Although MVC is in practice a fast choice in these cases, it is still
669 // logically a bytewise copy. This means that we cannot use it if the
670 // load or store is volatile. It also means that the transformation is
671 // not valid in cases where the two memories partially overlap; however,
672 // that is not a problem here, because we know that one of the memories
673 // is a full frame index.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000674 if (OpNum == 0 && MI->hasOneMemOperand()) {
675 MachineMemOperand *MMO = *MI->memoperands_begin();
676 if (MMO->getSize() == Size && !MMO->isVolatile()) {
677 // Handle conversion of loads.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000678 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXLoad)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000679 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000680 .addFrameIndex(FrameIndex).addImm(0).addImm(Size)
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000681 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000682 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000683 }
684 // Handle conversion of stores.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000685 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXStore)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000686 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
687 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000688 .addImm(Size).addFrameIndex(FrameIndex).addImm(0)
689 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000690 }
691 }
692 }
693
Richard Sandiforded1fab62013-07-03 10:10:02 +0000694 // If the spilled operand is the final one, try to change <INSN>R
695 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000696 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000697 if (MemOpcode >= 0) {
698 unsigned NumOps = MI->getNumExplicitOperands();
699 if (OpNum == NumOps - 1) {
700 const MCInstrDesc &MemDesc = get(MemOpcode);
701 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
702 assert(AccessBytes != 0 && "Size of access should be known");
703 assert(AccessBytes <= Size && "Access outside the frame index");
704 uint64_t Offset = Size - AccessBytes;
Richard Sandiforded1fab62013-07-03 10:10:02 +0000705 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), get(MemOpcode));
706 for (unsigned I = 0; I < OpNum; ++I)
707 MIB.addOperand(MI->getOperand(I));
708 MIB.addFrameIndex(FrameIndex).addImm(Offset);
709 if (MemDesc.TSFlags & SystemZII::HasIndex)
710 MIB.addReg(0);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000711 return MIB;
712 }
713 }
714
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000715 return 0;
716}
717
718MachineInstr *
719SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr* MI,
720 const SmallVectorImpl<unsigned> &Ops,
721 MachineInstr* LoadMI) const {
722 return 0;
723}
724
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000725bool
726SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
727 switch (MI->getOpcode()) {
728 case SystemZ::L128:
729 splitMove(MI, SystemZ::LG);
730 return true;
731
732 case SystemZ::ST128:
733 splitMove(MI, SystemZ::STG);
734 return true;
735
736 case SystemZ::LX:
737 splitMove(MI, SystemZ::LD);
738 return true;
739
740 case SystemZ::STX:
741 splitMove(MI, SystemZ::STD);
742 return true;
743
744 case SystemZ::ADJDYNALLOC:
745 splitAdjDynAlloc(MI);
746 return true;
747
748 default:
749 return false;
750 }
751}
752
Richard Sandiford312425f2013-05-20 14:23:08 +0000753uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
754 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
755 const MachineFunction *MF = MI->getParent()->getParent();
756 const char *AsmStr = MI->getOperand(0).getSymbolName();
757 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
758 }
759 return MI->getDesc().getSize();
760}
761
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000762SystemZII::Branch
763SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000764 switch (MI->getOpcode()) {
765 case SystemZ::BR:
766 case SystemZ::J:
767 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000768 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Richard Sandiford3d768e32013-07-31 12:30:20 +0000769 SystemZ::CCMASK_ANY, &MI->getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000770
771 case SystemZ::BRC:
772 case SystemZ::BRCL:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000773 return SystemZII::Branch(SystemZII::BranchNormal,
Richard Sandiford3d768e32013-07-31 12:30:20 +0000774 MI->getOperand(0).getImm(),
775 MI->getOperand(1).getImm(), &MI->getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000776
Richard Sandifordc2121252013-08-05 11:23:46 +0000777 case SystemZ::BRCT:
778 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
779 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
780
781 case SystemZ::BRCTG:
782 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
783 SystemZ::CCMASK_CMP_NE, &MI->getOperand(2));
784
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000785 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000786 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +0000787 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
788 MI->getOperand(2).getImm(), &MI->getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000789
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000790 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000791 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +0000792 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
793 MI->getOperand(2).getImm(), &MI->getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000794
795 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000796 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000797 }
798}
799
800void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
801 unsigned &LoadOpcode,
802 unsigned &StoreOpcode) const {
803 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
804 LoadOpcode = SystemZ::L;
805 StoreOpcode = SystemZ::ST32;
806 } else if (RC == &SystemZ::GR64BitRegClass ||
807 RC == &SystemZ::ADDR64BitRegClass) {
808 LoadOpcode = SystemZ::LG;
809 StoreOpcode = SystemZ::STG;
810 } else if (RC == &SystemZ::GR128BitRegClass ||
811 RC == &SystemZ::ADDR128BitRegClass) {
812 LoadOpcode = SystemZ::L128;
813 StoreOpcode = SystemZ::ST128;
814 } else if (RC == &SystemZ::FP32BitRegClass) {
815 LoadOpcode = SystemZ::LE;
816 StoreOpcode = SystemZ::STE;
817 } else if (RC == &SystemZ::FP64BitRegClass) {
818 LoadOpcode = SystemZ::LD;
819 StoreOpcode = SystemZ::STD;
820 } else if (RC == &SystemZ::FP128BitRegClass) {
821 LoadOpcode = SystemZ::LX;
822 StoreOpcode = SystemZ::STX;
823 } else
824 llvm_unreachable("Unsupported regclass to load or store");
825}
826
827unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
828 int64_t Offset) const {
829 const MCInstrDesc &MCID = get(Opcode);
830 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
831 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
832 // Get the instruction to use for unsigned 12-bit displacements.
833 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
834 if (Disp12Opcode >= 0)
835 return Disp12Opcode;
836
837 // All address-related instructions can use unsigned 12-bit
838 // displacements.
839 return Opcode;
840 }
841 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
842 // Get the instruction to use for signed 20-bit displacements.
843 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
844 if (Disp20Opcode >= 0)
845 return Disp20Opcode;
846
847 // Check whether Opcode allows signed 20-bit displacements.
848 if (MCID.TSFlags & SystemZII::Has20BitOffset)
849 return Opcode;
850 }
851 return 0;
852}
853
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000854unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
855 switch (Opcode) {
856 case SystemZ::L: return SystemZ::LT;
857 case SystemZ::LY: return SystemZ::LT;
858 case SystemZ::LG: return SystemZ::LTG;
859 case SystemZ::LGF: return SystemZ::LTGF;
860 case SystemZ::LR: return SystemZ::LTR;
861 case SystemZ::LGFR: return SystemZ::LTGFR;
862 case SystemZ::LGR: return SystemZ::LTGR;
Richard Sandiford0897fce2013-08-07 11:10:06 +0000863 case SystemZ::LER: return SystemZ::LTEBR;
864 case SystemZ::LDR: return SystemZ::LTDBR;
865 case SystemZ::LXR: return SystemZ::LTXBR;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +0000866 default: return 0;
867 }
868}
869
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000870// Return true if Mask matches the regexp 0*1+0*, given that zero masks
871// have already been filtered out. Store the first set bit in LSB and
872// the number of set bits in Length if so.
873static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
874 unsigned First = findFirstSet(Mask);
875 uint64_t Top = (Mask >> First) + 1;
876 if ((Top & -Top) == Top) {
877 LSB = First;
878 Length = findFirstSet(Top);
879 return true;
880 }
881 return false;
882}
883
884bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
885 unsigned &Start, unsigned &End) const {
886 // Reject trivial all-zero masks.
887 if (Mask == 0)
888 return false;
889
890 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
891 // the msb and End specifies the index of the lsb.
892 unsigned LSB, Length;
893 if (isStringOfOnes(Mask, LSB, Length)) {
894 Start = 63 - (LSB + Length - 1);
895 End = 63 - LSB;
896 return true;
897 }
898
899 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
900 // of the low 1s and End specifies the lsb of the high 1s.
901 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
902 assert(LSB > 0 && "Bottom bit must be set");
903 assert(LSB + Length < BitSize && "Top bit must be set");
904 Start = 63 - (LSB - 1);
905 End = 63 - (LSB + Length);
906 return true;
907 }
908
909 return false;
910}
911
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000912unsigned SystemZInstrInfo::getCompareAndBranch(unsigned Opcode,
913 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000914 switch (Opcode) {
915 case SystemZ::CR:
916 return SystemZ::CRJ;
917 case SystemZ::CGR:
918 return SystemZ::CGRJ;
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000919 case SystemZ::CHI:
920 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CIJ : 0;
921 case SystemZ::CGHI:
922 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CGIJ : 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000923 default:
924 return 0;
925 }
926}
927
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000928void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
929 MachineBasicBlock::iterator MBBI,
930 unsigned Reg, uint64_t Value) const {
931 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
932 unsigned Opcode;
933 if (isInt<16>(Value))
934 Opcode = SystemZ::LGHI;
935 else if (SystemZ::isImmLL(Value))
936 Opcode = SystemZ::LLILL;
937 else if (SystemZ::isImmLH(Value)) {
938 Opcode = SystemZ::LLILH;
939 Value >>= 16;
940 } else {
941 assert(isInt<32>(Value) && "Huge values not handled yet");
942 Opcode = SystemZ::LGFI;
943 }
944 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
945}