blob: 81d72e05740f3343088ffc1a0227abb176b69414 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZInstrInfo.cpp - SystemZ instruction information ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the SystemZ implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SystemZInstrInfo.h"
15#include "SystemZInstrBuilder.h"
Richard Sandifordf6bae1e2013-07-02 15:28:56 +000016#include "llvm/CodeGen/MachineRegisterInfo.h"
Richard Sandiford312425f2013-05-20 14:23:08 +000017#include "llvm/Target/TargetMachine.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000018
19#define GET_INSTRINFO_CTOR
20#define GET_INSTRMAP_INFO
21#include "SystemZGenInstrInfo.inc"
22
23using namespace llvm;
24
25SystemZInstrInfo::SystemZInstrInfo(SystemZTargetMachine &tm)
26 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
Bill Wendling637d97d2013-06-07 20:42:15 +000027 RI(tm) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000028}
29
30// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
31// each having the opcode given by NewOpcode.
32void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
33 unsigned NewOpcode) const {
34 MachineBasicBlock *MBB = MI->getParent();
35 MachineFunction &MF = *MBB->getParent();
36
37 // Get two load or store instructions. Use the original instruction for one
38 // of them (arbitarily the second here) and create a clone for the other.
39 MachineInstr *EarlierMI = MF.CloneMachineInstr(MI);
40 MBB->insert(MI, EarlierMI);
41
42 // Set up the two 64-bit registers.
43 MachineOperand &HighRegOp = EarlierMI->getOperand(0);
44 MachineOperand &LowRegOp = MI->getOperand(0);
45 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_high));
46 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_low));
47
48 // The address in the first (high) instruction is already correct.
49 // Adjust the offset in the second (low) instruction.
50 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
51 MachineOperand &LowOffsetOp = MI->getOperand(2);
52 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
53
54 // Set the opcodes.
55 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
56 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
57 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
58
59 EarlierMI->setDesc(get(HighOpcode));
60 MI->setDesc(get(LowOpcode));
61}
62
63// Split ADJDYNALLOC instruction MI.
64void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
65 MachineBasicBlock *MBB = MI->getParent();
66 MachineFunction &MF = *MBB->getParent();
67 MachineFrameInfo *MFFrame = MF.getFrameInfo();
68 MachineOperand &OffsetMO = MI->getOperand(2);
69
70 uint64_t Offset = (MFFrame->getMaxCallFrameSize() +
71 SystemZMC::CallFrameSize +
72 OffsetMO.getImm());
73 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
74 assert(NewOpcode && "No support for huge argument lists yet");
75 MI->setDesc(get(NewOpcode));
76 OffsetMO.setImm(Offset);
77}
78
79// If MI is a simple load or store for a frame object, return the register
80// it loads or stores and set FrameIndex to the index of the frame object.
81// Return 0 otherwise.
82//
83// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +000084static int isSimpleMove(const MachineInstr *MI, int &FrameIndex,
85 unsigned Flag) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000086 const MCInstrDesc &MCID = MI->getDesc();
87 if ((MCID.TSFlags & Flag) &&
88 MI->getOperand(1).isFI() &&
89 MI->getOperand(2).getImm() == 0 &&
90 MI->getOperand(3).getReg() == 0) {
91 FrameIndex = MI->getOperand(1).getIndex();
92 return MI->getOperand(0).getReg();
93 }
94 return 0;
95}
96
97unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
98 int &FrameIndex) const {
99 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
100}
101
102unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
103 int &FrameIndex) const {
104 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
105}
106
107bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
108 MachineBasicBlock *&TBB,
109 MachineBasicBlock *&FBB,
110 SmallVectorImpl<MachineOperand> &Cond,
111 bool AllowModify) const {
112 // Most of the code and comments here are boilerplate.
113
114 // Start from the bottom of the block and work up, examining the
115 // terminator instructions.
116 MachineBasicBlock::iterator I = MBB.end();
117 while (I != MBB.begin()) {
118 --I;
119 if (I->isDebugValue())
120 continue;
121
122 // Working from the bottom, when we see a non-terminator instruction, we're
123 // done.
124 if (!isUnpredicatedTerminator(I))
125 break;
126
127 // A terminator that isn't a branch can't easily be handled by this
128 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000129 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000130 return true;
131
132 // Can't handle indirect branches.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000133 SystemZII::Branch Branch(getBranchInfo(I));
134 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000135 return true;
136
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000137 // Punt on compound branches.
138 if (Branch.Type != SystemZII::BranchNormal)
139 return true;
140
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000141 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000142 // Handle unconditional branches.
143 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000144 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000145 continue;
146 }
147
148 // If the block has any instructions after a JMP, delete them.
149 while (llvm::next(I) != MBB.end())
150 llvm::next(I)->eraseFromParent();
151
152 Cond.clear();
153 FBB = 0;
154
155 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000156 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000157 TBB = 0;
158 I->eraseFromParent();
159 I = MBB.end();
160 continue;
161 }
162
163 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000164 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000165 continue;
166 }
167
168 // Working from the bottom, handle the first conditional branch.
169 if (Cond.empty()) {
170 // FIXME: add X86-style branch swap
171 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000172 TBB = Branch.Target->getMBB();
173 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000174 continue;
175 }
176
177 // Handle subsequent conditional branches.
178 assert(Cond.size() == 1);
179 assert(TBB);
180
181 // Only handle the case where all conditional branches branch to the same
182 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000183 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000184 return true;
185
186 // If the conditions are the same, we can leave them alone.
187 unsigned OldCond = Cond[0].getImm();
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000188 if (OldCond == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000189 continue;
190
191 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
192 }
193
194 return false;
195}
196
197unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
198 // Most of the code and comments here are boilerplate.
199 MachineBasicBlock::iterator I = MBB.end();
200 unsigned Count = 0;
201
202 while (I != MBB.begin()) {
203 --I;
204 if (I->isDebugValue())
205 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000206 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000207 break;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000208 if (!getBranchInfo(I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000209 break;
210 // Remove the branch.
211 I->eraseFromParent();
212 I = MBB.end();
213 ++Count;
214 }
215
216 return Count;
217}
218
219unsigned
220SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
221 MachineBasicBlock *FBB,
222 const SmallVectorImpl<MachineOperand> &Cond,
223 DebugLoc DL) const {
224 // In this function we output 32-bit branches, which should always
225 // have enough range. They can be shortened and relaxed by later code
226 // in the pipeline, if desired.
227
228 // Shouldn't be a fall through.
229 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
230 assert((Cond.size() == 1 || Cond.size() == 0) &&
231 "SystemZ branch conditions have one component!");
232
233 if (Cond.empty()) {
234 // Unconditional branch?
235 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000236 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000237 return 1;
238 }
239
240 // Conditional branch.
241 unsigned Count = 0;
242 unsigned CC = Cond[0].getImm();
Richard Sandiford312425f2013-05-20 14:23:08 +0000243 BuildMI(&MBB, DL, get(SystemZ::BRC)).addImm(CC).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000244 ++Count;
245
246 if (FBB) {
247 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000248 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000249 ++Count;
250 }
251 return Count;
252}
253
254void
255SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
256 MachineBasicBlock::iterator MBBI, DebugLoc DL,
257 unsigned DestReg, unsigned SrcReg,
258 bool KillSrc) const {
259 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
260 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
261 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_high),
262 RI.getSubReg(SrcReg, SystemZ::subreg_high), KillSrc);
263 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_low),
264 RI.getSubReg(SrcReg, SystemZ::subreg_low), KillSrc);
265 return;
266 }
267
268 // Everything else needs only one instruction.
269 unsigned Opcode;
270 if (SystemZ::GR32BitRegClass.contains(DestReg, SrcReg))
271 Opcode = SystemZ::LR;
272 else if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
273 Opcode = SystemZ::LGR;
274 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
275 Opcode = SystemZ::LER;
276 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
277 Opcode = SystemZ::LDR;
278 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
279 Opcode = SystemZ::LXR;
280 else
281 llvm_unreachable("Impossible reg-to-reg copy");
282
283 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
284 .addReg(SrcReg, getKillRegState(KillSrc));
285}
286
287void
288SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
289 MachineBasicBlock::iterator MBBI,
290 unsigned SrcReg, bool isKill,
291 int FrameIdx,
292 const TargetRegisterClass *RC,
293 const TargetRegisterInfo *TRI) const {
294 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
295
296 // Callers may expect a single instruction, so keep 128-bit moves
297 // together for now and lower them after register allocation.
298 unsigned LoadOpcode, StoreOpcode;
299 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
300 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
301 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
302}
303
304void
305SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
306 MachineBasicBlock::iterator MBBI,
307 unsigned DestReg, int FrameIdx,
308 const TargetRegisterClass *RC,
309 const TargetRegisterInfo *TRI) const {
310 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
311
312 // Callers may expect a single instruction, so keep 128-bit moves
313 // together for now and lower them after register allocation.
314 unsigned LoadOpcode, StoreOpcode;
315 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
316 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
317 FrameIdx);
318}
319
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000320// Return true if MI is a simple load or store with a 12-bit displacement
321// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
322static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
323 const MCInstrDesc &MCID = MI->getDesc();
324 return ((MCID.TSFlags & Flag) &&
325 isUInt<12>(MI->getOperand(2).getImm()) &&
326 MI->getOperand(3).getReg() == 0);
327}
328
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000329MachineInstr *
330SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
331 MachineInstr *MI,
332 const SmallVectorImpl<unsigned> &Ops,
333 int FrameIndex) const {
334 const MachineFrameInfo *MFI = MF.getFrameInfo();
335 unsigned Size = MFI->getObjectSize(FrameIndex);
336
337 // Eary exit for cases we don't care about
338 if (Ops.size() != 1)
339 return 0;
340
341 unsigned OpNum = Ops[0];
NAKAMURA Takumiddcba562013-07-03 02:20:49 +0000342 assert(Size == MF.getRegInfo()
343 .getRegClass(MI->getOperand(OpNum).getReg())->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000344 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000345
346 // Look for cases where the source of a simple store or the destination
347 // of a simple load is being spilled. Try to use MVC instead.
348 //
349 // Although MVC is in practice a fast choice in these cases, it is still
350 // logically a bytewise copy. This means that we cannot use it if the
351 // load or store is volatile. It also means that the transformation is
352 // not valid in cases where the two memories partially overlap; however,
353 // that is not a problem here, because we know that one of the memories
354 // is a full frame index.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000355 if (OpNum == 0 && MI->hasOneMemOperand()) {
356 MachineMemOperand *MMO = *MI->memoperands_begin();
357 if (MMO->getSize() == Size && !MMO->isVolatile()) {
358 // Handle conversion of loads.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000359 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXLoad)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000360 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000361 .addFrameIndex(FrameIndex).addImm(0).addImm(Size)
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000362 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000363 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000364 }
365 // Handle conversion of stores.
Richard Sandiford8976ea72013-07-05 14:02:01 +0000366 if (isSimpleBD12Move(MI, SystemZII::SimpleBDXStore)) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000367 return BuildMI(MF, MI->getDebugLoc(), get(SystemZ::MVC))
368 .addOperand(MI->getOperand(1)).addImm(MI->getOperand(2).getImm())
Richard Sandiford1ca6dea2013-07-05 14:31:24 +0000369 .addImm(Size).addFrameIndex(FrameIndex).addImm(0)
370 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000371 }
372 }
373 }
374
Richard Sandiforded1fab62013-07-03 10:10:02 +0000375 // If the spilled operand is the final one, try to change <INSN>R
376 // into <INSN>.
377 int MemOpcode = SystemZ::getMemOpcode(MI->getOpcode());
378 if (MemOpcode >= 0) {
379 unsigned NumOps = MI->getNumExplicitOperands();
380 if (OpNum == NumOps - 1) {
381 const MCInstrDesc &MemDesc = get(MemOpcode);
382 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
383 assert(AccessBytes != 0 && "Size of access should be known");
384 assert(AccessBytes <= Size && "Access outside the frame index");
385 uint64_t Offset = Size - AccessBytes;
Richard Sandiforded1fab62013-07-03 10:10:02 +0000386 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), get(MemOpcode));
387 for (unsigned I = 0; I < OpNum; ++I)
388 MIB.addOperand(MI->getOperand(I));
389 MIB.addFrameIndex(FrameIndex).addImm(Offset);
390 if (MemDesc.TSFlags & SystemZII::HasIndex)
391 MIB.addReg(0);
Richard Sandiforded1fab62013-07-03 10:10:02 +0000392 return MIB;
393 }
394 }
395
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000396 return 0;
397}
398
399MachineInstr *
400SystemZInstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr* MI,
401 const SmallVectorImpl<unsigned> &Ops,
402 MachineInstr* LoadMI) const {
403 return 0;
404}
405
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000406bool
407SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
408 switch (MI->getOpcode()) {
409 case SystemZ::L128:
410 splitMove(MI, SystemZ::LG);
411 return true;
412
413 case SystemZ::ST128:
414 splitMove(MI, SystemZ::STG);
415 return true;
416
417 case SystemZ::LX:
418 splitMove(MI, SystemZ::LD);
419 return true;
420
421 case SystemZ::STX:
422 splitMove(MI, SystemZ::STD);
423 return true;
424
425 case SystemZ::ADJDYNALLOC:
426 splitAdjDynAlloc(MI);
427 return true;
428
429 default:
430 return false;
431 }
432}
433
434bool SystemZInstrInfo::
435ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
436 assert(Cond.size() == 1 && "Invalid branch condition!");
437 Cond[0].setImm(Cond[0].getImm() ^ SystemZ::CCMASK_ANY);
438 return false;
439}
440
Richard Sandiford312425f2013-05-20 14:23:08 +0000441uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
442 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
443 const MachineFunction *MF = MI->getParent()->getParent();
444 const char *AsmStr = MI->getOperand(0).getSymbolName();
445 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
446 }
447 return MI->getDesc().getSize();
448}
449
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000450SystemZII::Branch
451SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000452 switch (MI->getOpcode()) {
453 case SystemZ::BR:
454 case SystemZ::J:
455 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000456 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
457 &MI->getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000458
459 case SystemZ::BRC:
460 case SystemZ::BRCL:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000461 return SystemZII::Branch(SystemZII::BranchNormal,
462 MI->getOperand(0).getImm(), &MI->getOperand(1));
463
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000464 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000465 case SystemZ::CRJ:
466 return SystemZII::Branch(SystemZII::BranchC, MI->getOperand(2).getImm(),
467 &MI->getOperand(3));
468
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000469 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000470 case SystemZ::CGRJ:
471 return SystemZII::Branch(SystemZII::BranchCG, MI->getOperand(2).getImm(),
472 &MI->getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000473
474 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000475 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000476 }
477}
478
479void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
480 unsigned &LoadOpcode,
481 unsigned &StoreOpcode) const {
482 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
483 LoadOpcode = SystemZ::L;
484 StoreOpcode = SystemZ::ST32;
485 } else if (RC == &SystemZ::GR64BitRegClass ||
486 RC == &SystemZ::ADDR64BitRegClass) {
487 LoadOpcode = SystemZ::LG;
488 StoreOpcode = SystemZ::STG;
489 } else if (RC == &SystemZ::GR128BitRegClass ||
490 RC == &SystemZ::ADDR128BitRegClass) {
491 LoadOpcode = SystemZ::L128;
492 StoreOpcode = SystemZ::ST128;
493 } else if (RC == &SystemZ::FP32BitRegClass) {
494 LoadOpcode = SystemZ::LE;
495 StoreOpcode = SystemZ::STE;
496 } else if (RC == &SystemZ::FP64BitRegClass) {
497 LoadOpcode = SystemZ::LD;
498 StoreOpcode = SystemZ::STD;
499 } else if (RC == &SystemZ::FP128BitRegClass) {
500 LoadOpcode = SystemZ::LX;
501 StoreOpcode = SystemZ::STX;
502 } else
503 llvm_unreachable("Unsupported regclass to load or store");
504}
505
506unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
507 int64_t Offset) const {
508 const MCInstrDesc &MCID = get(Opcode);
509 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
510 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
511 // Get the instruction to use for unsigned 12-bit displacements.
512 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
513 if (Disp12Opcode >= 0)
514 return Disp12Opcode;
515
516 // All address-related instructions can use unsigned 12-bit
517 // displacements.
518 return Opcode;
519 }
520 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
521 // Get the instruction to use for signed 20-bit displacements.
522 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
523 if (Disp20Opcode >= 0)
524 return Disp20Opcode;
525
526 // Check whether Opcode allows signed 20-bit displacements.
527 if (MCID.TSFlags & SystemZII::Has20BitOffset)
528 return Opcode;
529 }
530 return 0;
531}
532
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000533unsigned SystemZInstrInfo::getCompareAndBranch(unsigned Opcode,
534 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000535 switch (Opcode) {
536 case SystemZ::CR:
537 return SystemZ::CRJ;
538 case SystemZ::CGR:
539 return SystemZ::CGRJ;
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000540 case SystemZ::CHI:
541 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CIJ : 0;
542 case SystemZ::CGHI:
543 return MI && isInt<8>(MI->getOperand(1).getImm()) ? SystemZ::CGIJ : 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000544 default:
545 return 0;
546 }
547}
548
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000549void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
550 MachineBasicBlock::iterator MBBI,
551 unsigned Reg, uint64_t Value) const {
552 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
553 unsigned Opcode;
554 if (isInt<16>(Value))
555 Opcode = SystemZ::LGHI;
556 else if (SystemZ::isImmLL(Value))
557 Opcode = SystemZ::LLILL;
558 else if (SystemZ::isImmLH(Value)) {
559 Opcode = SystemZ::LLILH;
560 Value >>= 16;
561 } else {
562 assert(isInt<32>(Value) && "Huge values not handled yet");
563 Opcode = SystemZ::LGFI;
564 }
565 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
566}