blob: 6296e4a48007e34beb438392b64cb8edae994d86 [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 Sandiford312425f2013-05-20 14:23:08 +000016#include "llvm/Target/TargetMachine.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000017
18#define GET_INSTRINFO_CTOR
19#define GET_INSTRMAP_INFO
20#include "SystemZGenInstrInfo.inc"
21
22using namespace llvm;
23
24SystemZInstrInfo::SystemZInstrInfo(SystemZTargetMachine &tm)
25 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
26 RI(tm, *this) {
27}
28
29// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
30// each having the opcode given by NewOpcode.
31void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
32 unsigned NewOpcode) const {
33 MachineBasicBlock *MBB = MI->getParent();
34 MachineFunction &MF = *MBB->getParent();
35
36 // Get two load or store instructions. Use the original instruction for one
37 // of them (arbitarily the second here) and create a clone for the other.
38 MachineInstr *EarlierMI = MF.CloneMachineInstr(MI);
39 MBB->insert(MI, EarlierMI);
40
41 // Set up the two 64-bit registers.
42 MachineOperand &HighRegOp = EarlierMI->getOperand(0);
43 MachineOperand &LowRegOp = MI->getOperand(0);
44 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_high));
45 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_low));
46
47 // The address in the first (high) instruction is already correct.
48 // Adjust the offset in the second (low) instruction.
49 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
50 MachineOperand &LowOffsetOp = MI->getOperand(2);
51 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
52
53 // Set the opcodes.
54 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
55 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
56 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
57
58 EarlierMI->setDesc(get(HighOpcode));
59 MI->setDesc(get(LowOpcode));
60}
61
62// Split ADJDYNALLOC instruction MI.
63void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
64 MachineBasicBlock *MBB = MI->getParent();
65 MachineFunction &MF = *MBB->getParent();
66 MachineFrameInfo *MFFrame = MF.getFrameInfo();
67 MachineOperand &OffsetMO = MI->getOperand(2);
68
69 uint64_t Offset = (MFFrame->getMaxCallFrameSize() +
70 SystemZMC::CallFrameSize +
71 OffsetMO.getImm());
72 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
73 assert(NewOpcode && "No support for huge argument lists yet");
74 MI->setDesc(get(NewOpcode));
75 OffsetMO.setImm(Offset);
76}
77
78// If MI is a simple load or store for a frame object, return the register
79// it loads or stores and set FrameIndex to the index of the frame object.
80// Return 0 otherwise.
81//
82// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
83static int isSimpleMove(const MachineInstr *MI, int &FrameIndex, int Flag) {
84 const MCInstrDesc &MCID = MI->getDesc();
85 if ((MCID.TSFlags & Flag) &&
86 MI->getOperand(1).isFI() &&
87 MI->getOperand(2).getImm() == 0 &&
88 MI->getOperand(3).getReg() == 0) {
89 FrameIndex = MI->getOperand(1).getIndex();
90 return MI->getOperand(0).getReg();
91 }
92 return 0;
93}
94
95unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
96 int &FrameIndex) const {
97 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
98}
99
100unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
101 int &FrameIndex) const {
102 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
103}
104
105bool SystemZInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
106 MachineBasicBlock *&TBB,
107 MachineBasicBlock *&FBB,
108 SmallVectorImpl<MachineOperand> &Cond,
109 bool AllowModify) const {
110 // Most of the code and comments here are boilerplate.
111
112 // Start from the bottom of the block and work up, examining the
113 // terminator instructions.
114 MachineBasicBlock::iterator I = MBB.end();
115 while (I != MBB.begin()) {
116 --I;
117 if (I->isDebugValue())
118 continue;
119
120 // Working from the bottom, when we see a non-terminator instruction, we're
121 // done.
122 if (!isUnpredicatedTerminator(I))
123 break;
124
125 // A terminator that isn't a branch can't easily be handled by this
126 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000127 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000128 return true;
129
130 // Can't handle indirect branches.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000131 SystemZII::Branch Branch(getBranchInfo(I));
132 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133 return true;
134
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000135 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000136 // Handle unconditional branches.
137 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000138 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000139 continue;
140 }
141
142 // If the block has any instructions after a JMP, delete them.
143 while (llvm::next(I) != MBB.end())
144 llvm::next(I)->eraseFromParent();
145
146 Cond.clear();
147 FBB = 0;
148
149 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000150 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000151 TBB = 0;
152 I->eraseFromParent();
153 I = MBB.end();
154 continue;
155 }
156
157 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000158 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000159 continue;
160 }
161
162 // Working from the bottom, handle the first conditional branch.
163 if (Cond.empty()) {
164 // FIXME: add X86-style branch swap
165 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000166 TBB = Branch.Target->getMBB();
167 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000168 continue;
169 }
170
171 // Handle subsequent conditional branches.
172 assert(Cond.size() == 1);
173 assert(TBB);
174
175 // Only handle the case where all conditional branches branch to the same
176 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000177 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000178 return true;
179
180 // If the conditions are the same, we can leave them alone.
181 unsigned OldCond = Cond[0].getImm();
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000182 if (OldCond == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000183 continue;
184
185 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
186 }
187
188 return false;
189}
190
191unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
192 // Most of the code and comments here are boilerplate.
193 MachineBasicBlock::iterator I = MBB.end();
194 unsigned Count = 0;
195
196 while (I != MBB.begin()) {
197 --I;
198 if (I->isDebugValue())
199 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000200 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000201 break;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000202 if (!getBranchInfo(I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000203 break;
204 // Remove the branch.
205 I->eraseFromParent();
206 I = MBB.end();
207 ++Count;
208 }
209
210 return Count;
211}
212
213unsigned
214SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
215 MachineBasicBlock *FBB,
216 const SmallVectorImpl<MachineOperand> &Cond,
217 DebugLoc DL) const {
218 // In this function we output 32-bit branches, which should always
219 // have enough range. They can be shortened and relaxed by later code
220 // in the pipeline, if desired.
221
222 // Shouldn't be a fall through.
223 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
224 assert((Cond.size() == 1 || Cond.size() == 0) &&
225 "SystemZ branch conditions have one component!");
226
227 if (Cond.empty()) {
228 // Unconditional branch?
229 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000230 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000231 return 1;
232 }
233
234 // Conditional branch.
235 unsigned Count = 0;
236 unsigned CC = Cond[0].getImm();
Richard Sandiford312425f2013-05-20 14:23:08 +0000237 BuildMI(&MBB, DL, get(SystemZ::BRC)).addImm(CC).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000238 ++Count;
239
240 if (FBB) {
241 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000242 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000243 ++Count;
244 }
245 return Count;
246}
247
248void
249SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
250 MachineBasicBlock::iterator MBBI, DebugLoc DL,
251 unsigned DestReg, unsigned SrcReg,
252 bool KillSrc) const {
253 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
254 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
255 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_high),
256 RI.getSubReg(SrcReg, SystemZ::subreg_high), KillSrc);
257 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_low),
258 RI.getSubReg(SrcReg, SystemZ::subreg_low), KillSrc);
259 return;
260 }
261
262 // Everything else needs only one instruction.
263 unsigned Opcode;
264 if (SystemZ::GR32BitRegClass.contains(DestReg, SrcReg))
265 Opcode = SystemZ::LR;
266 else if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
267 Opcode = SystemZ::LGR;
268 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
269 Opcode = SystemZ::LER;
270 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
271 Opcode = SystemZ::LDR;
272 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
273 Opcode = SystemZ::LXR;
274 else
275 llvm_unreachable("Impossible reg-to-reg copy");
276
277 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
278 .addReg(SrcReg, getKillRegState(KillSrc));
279}
280
281void
282SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
283 MachineBasicBlock::iterator MBBI,
284 unsigned SrcReg, bool isKill,
285 int FrameIdx,
286 const TargetRegisterClass *RC,
287 const TargetRegisterInfo *TRI) const {
288 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
289
290 // Callers may expect a single instruction, so keep 128-bit moves
291 // together for now and lower them after register allocation.
292 unsigned LoadOpcode, StoreOpcode;
293 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
294 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
295 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
296}
297
298void
299SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
300 MachineBasicBlock::iterator MBBI,
301 unsigned DestReg, int FrameIdx,
302 const TargetRegisterClass *RC,
303 const TargetRegisterInfo *TRI) const {
304 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
305
306 // Callers may expect a single instruction, so keep 128-bit moves
307 // together for now and lower them after register allocation.
308 unsigned LoadOpcode, StoreOpcode;
309 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
310 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
311 FrameIdx);
312}
313
314bool
315SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
316 switch (MI->getOpcode()) {
317 case SystemZ::L128:
318 splitMove(MI, SystemZ::LG);
319 return true;
320
321 case SystemZ::ST128:
322 splitMove(MI, SystemZ::STG);
323 return true;
324
325 case SystemZ::LX:
326 splitMove(MI, SystemZ::LD);
327 return true;
328
329 case SystemZ::STX:
330 splitMove(MI, SystemZ::STD);
331 return true;
332
333 case SystemZ::ADJDYNALLOC:
334 splitAdjDynAlloc(MI);
335 return true;
336
337 default:
338 return false;
339 }
340}
341
342bool SystemZInstrInfo::
343ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
344 assert(Cond.size() == 1 && "Invalid branch condition!");
345 Cond[0].setImm(Cond[0].getImm() ^ SystemZ::CCMASK_ANY);
346 return false;
347}
348
Richard Sandiford312425f2013-05-20 14:23:08 +0000349uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
350 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
351 const MachineFunction *MF = MI->getParent()->getParent();
352 const char *AsmStr = MI->getOperand(0).getSymbolName();
353 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
354 }
355 return MI->getDesc().getSize();
356}
357
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000358SystemZII::Branch
359SystemZInstrInfo::getBranchInfo(const MachineInstr *MI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000360 switch (MI->getOpcode()) {
361 case SystemZ::BR:
362 case SystemZ::J:
363 case SystemZ::JG:
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000364 return SystemZII::Branch(SystemZ::CCMASK_ANY, &MI->getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000365
366 case SystemZ::BRC:
367 case SystemZ::BRCL:
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000368 return SystemZII::Branch(MI->getOperand(0).getImm(), &MI->getOperand(1));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000369
370 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000371 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000372 }
373}
374
375void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
376 unsigned &LoadOpcode,
377 unsigned &StoreOpcode) const {
378 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
379 LoadOpcode = SystemZ::L;
380 StoreOpcode = SystemZ::ST32;
381 } else if (RC == &SystemZ::GR64BitRegClass ||
382 RC == &SystemZ::ADDR64BitRegClass) {
383 LoadOpcode = SystemZ::LG;
384 StoreOpcode = SystemZ::STG;
385 } else if (RC == &SystemZ::GR128BitRegClass ||
386 RC == &SystemZ::ADDR128BitRegClass) {
387 LoadOpcode = SystemZ::L128;
388 StoreOpcode = SystemZ::ST128;
389 } else if (RC == &SystemZ::FP32BitRegClass) {
390 LoadOpcode = SystemZ::LE;
391 StoreOpcode = SystemZ::STE;
392 } else if (RC == &SystemZ::FP64BitRegClass) {
393 LoadOpcode = SystemZ::LD;
394 StoreOpcode = SystemZ::STD;
395 } else if (RC == &SystemZ::FP128BitRegClass) {
396 LoadOpcode = SystemZ::LX;
397 StoreOpcode = SystemZ::STX;
398 } else
399 llvm_unreachable("Unsupported regclass to load or store");
400}
401
402unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
403 int64_t Offset) const {
404 const MCInstrDesc &MCID = get(Opcode);
405 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
406 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
407 // Get the instruction to use for unsigned 12-bit displacements.
408 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
409 if (Disp12Opcode >= 0)
410 return Disp12Opcode;
411
412 // All address-related instructions can use unsigned 12-bit
413 // displacements.
414 return Opcode;
415 }
416 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
417 // Get the instruction to use for signed 20-bit displacements.
418 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
419 if (Disp20Opcode >= 0)
420 return Disp20Opcode;
421
422 // Check whether Opcode allows signed 20-bit displacements.
423 if (MCID.TSFlags & SystemZII::Has20BitOffset)
424 return Opcode;
425 }
426 return 0;
427}
428
429void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
430 MachineBasicBlock::iterator MBBI,
431 unsigned Reg, uint64_t Value) const {
432 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
433 unsigned Opcode;
434 if (isInt<16>(Value))
435 Opcode = SystemZ::LGHI;
436 else if (SystemZ::isImmLL(Value))
437 Opcode = SystemZ::LLILL;
438 else if (SystemZ::isImmLH(Value)) {
439 Opcode = SystemZ::LLILH;
440 Value >>= 16;
441 } else {
442 assert(isInt<32>(Value) && "Huge values not handled yet");
443 Opcode = SystemZ::LGFI;
444 }
445 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
446}