blob: e13a3d958645a6b43fe311e56c4ecb603c40de3b [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.
127 unsigned ThisCond;
128 const MachineOperand *ThisTarget;
129 if (!isBranch(I, ThisCond, ThisTarget))
130 return true;
131
132 // Can't handle indirect branches.
133 if (!ThisTarget->isMBB())
134 return true;
135
136 if (ThisCond == SystemZ::CCMASK_ANY) {
137 // Handle unconditional branches.
138 if (!AllowModify) {
139 TBB = ThisTarget->getMBB();
140 continue;
141 }
142
143 // If the block has any instructions after a JMP, delete them.
144 while (llvm::next(I) != MBB.end())
145 llvm::next(I)->eraseFromParent();
146
147 Cond.clear();
148 FBB = 0;
149
150 // Delete the JMP if it's equivalent to a fall-through.
151 if (MBB.isLayoutSuccessor(ThisTarget->getMBB())) {
152 TBB = 0;
153 I->eraseFromParent();
154 I = MBB.end();
155 continue;
156 }
157
158 // TBB is used to indicate the unconditinal destination.
159 TBB = ThisTarget->getMBB();
160 continue;
161 }
162
163 // Working from the bottom, handle the first conditional branch.
164 if (Cond.empty()) {
165 // FIXME: add X86-style branch swap
166 FBB = TBB;
167 TBB = ThisTarget->getMBB();
168 Cond.push_back(MachineOperand::CreateImm(ThisCond));
169 continue;
170 }
171
172 // Handle subsequent conditional branches.
173 assert(Cond.size() == 1);
174 assert(TBB);
175
176 // Only handle the case where all conditional branches branch to the same
177 // destination.
178 if (TBB != ThisTarget->getMBB())
179 return true;
180
181 // If the conditions are the same, we can leave them alone.
182 unsigned OldCond = Cond[0].getImm();
183 if (OldCond == ThisCond)
184 continue;
185
186 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
187 }
188
189 return false;
190}
191
192unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
193 // Most of the code and comments here are boilerplate.
194 MachineBasicBlock::iterator I = MBB.end();
195 unsigned Count = 0;
196
197 while (I != MBB.begin()) {
198 --I;
199 if (I->isDebugValue())
200 continue;
201 unsigned Cond;
202 const MachineOperand *Target;
203 if (!isBranch(I, Cond, Target))
204 break;
205 if (!Target->isMBB())
206 break;
207 // Remove the branch.
208 I->eraseFromParent();
209 I = MBB.end();
210 ++Count;
211 }
212
213 return Count;
214}
215
216unsigned
217SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
218 MachineBasicBlock *FBB,
219 const SmallVectorImpl<MachineOperand> &Cond,
220 DebugLoc DL) const {
221 // In this function we output 32-bit branches, which should always
222 // have enough range. They can be shortened and relaxed by later code
223 // in the pipeline, if desired.
224
225 // Shouldn't be a fall through.
226 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
227 assert((Cond.size() == 1 || Cond.size() == 0) &&
228 "SystemZ branch conditions have one component!");
229
230 if (Cond.empty()) {
231 // Unconditional branch?
232 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000233 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000234 return 1;
235 }
236
237 // Conditional branch.
238 unsigned Count = 0;
239 unsigned CC = Cond[0].getImm();
Richard Sandiford312425f2013-05-20 14:23:08 +0000240 BuildMI(&MBB, DL, get(SystemZ::BRC)).addImm(CC).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000241 ++Count;
242
243 if (FBB) {
244 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000245 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000246 ++Count;
247 }
248 return Count;
249}
250
251void
252SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
253 MachineBasicBlock::iterator MBBI, DebugLoc DL,
254 unsigned DestReg, unsigned SrcReg,
255 bool KillSrc) const {
256 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
257 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
258 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_high),
259 RI.getSubReg(SrcReg, SystemZ::subreg_high), KillSrc);
260 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_low),
261 RI.getSubReg(SrcReg, SystemZ::subreg_low), KillSrc);
262 return;
263 }
264
265 // Everything else needs only one instruction.
266 unsigned Opcode;
267 if (SystemZ::GR32BitRegClass.contains(DestReg, SrcReg))
268 Opcode = SystemZ::LR;
269 else if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
270 Opcode = SystemZ::LGR;
271 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
272 Opcode = SystemZ::LER;
273 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
274 Opcode = SystemZ::LDR;
275 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
276 Opcode = SystemZ::LXR;
277 else
278 llvm_unreachable("Impossible reg-to-reg copy");
279
280 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
281 .addReg(SrcReg, getKillRegState(KillSrc));
282}
283
284void
285SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
286 MachineBasicBlock::iterator MBBI,
287 unsigned SrcReg, bool isKill,
288 int FrameIdx,
289 const TargetRegisterClass *RC,
290 const TargetRegisterInfo *TRI) const {
291 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
292
293 // Callers may expect a single instruction, so keep 128-bit moves
294 // together for now and lower them after register allocation.
295 unsigned LoadOpcode, StoreOpcode;
296 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
297 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
298 .addReg(SrcReg, getKillRegState(isKill)), FrameIdx);
299}
300
301void
302SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
303 MachineBasicBlock::iterator MBBI,
304 unsigned DestReg, int FrameIdx,
305 const TargetRegisterClass *RC,
306 const TargetRegisterInfo *TRI) const {
307 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
308
309 // Callers may expect a single instruction, so keep 128-bit moves
310 // together for now and lower them after register allocation.
311 unsigned LoadOpcode, StoreOpcode;
312 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
313 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
314 FrameIdx);
315}
316
317bool
318SystemZInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
319 switch (MI->getOpcode()) {
320 case SystemZ::L128:
321 splitMove(MI, SystemZ::LG);
322 return true;
323
324 case SystemZ::ST128:
325 splitMove(MI, SystemZ::STG);
326 return true;
327
328 case SystemZ::LX:
329 splitMove(MI, SystemZ::LD);
330 return true;
331
332 case SystemZ::STX:
333 splitMove(MI, SystemZ::STD);
334 return true;
335
336 case SystemZ::ADJDYNALLOC:
337 splitAdjDynAlloc(MI);
338 return true;
339
340 default:
341 return false;
342 }
343}
344
345bool SystemZInstrInfo::
346ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
347 assert(Cond.size() == 1 && "Invalid branch condition!");
348 Cond[0].setImm(Cond[0].getImm() ^ SystemZ::CCMASK_ANY);
349 return false;
350}
351
Richard Sandiford312425f2013-05-20 14:23:08 +0000352uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr *MI) const {
353 if (MI->getOpcode() == TargetOpcode::INLINEASM) {
354 const MachineFunction *MF = MI->getParent()->getParent();
355 const char *AsmStr = MI->getOperand(0).getSymbolName();
356 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
357 }
358 return MI->getDesc().getSize();
359}
360
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000361bool SystemZInstrInfo::isBranch(const MachineInstr *MI, unsigned &Cond,
362 const MachineOperand *&Target) const {
363 switch (MI->getOpcode()) {
364 case SystemZ::BR:
365 case SystemZ::J:
366 case SystemZ::JG:
367 Cond = SystemZ::CCMASK_ANY;
368 Target = &MI->getOperand(0);
369 return true;
370
371 case SystemZ::BRC:
372 case SystemZ::BRCL:
373 Cond = MI->getOperand(0).getImm();
374 Target = &MI->getOperand(1);
375 return true;
376
377 default:
378 assert(!MI->getDesc().isBranch() && "Unknown branch opcode");
379 return false;
380 }
381}
382
383void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
384 unsigned &LoadOpcode,
385 unsigned &StoreOpcode) const {
386 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
387 LoadOpcode = SystemZ::L;
388 StoreOpcode = SystemZ::ST32;
389 } else if (RC == &SystemZ::GR64BitRegClass ||
390 RC == &SystemZ::ADDR64BitRegClass) {
391 LoadOpcode = SystemZ::LG;
392 StoreOpcode = SystemZ::STG;
393 } else if (RC == &SystemZ::GR128BitRegClass ||
394 RC == &SystemZ::ADDR128BitRegClass) {
395 LoadOpcode = SystemZ::L128;
396 StoreOpcode = SystemZ::ST128;
397 } else if (RC == &SystemZ::FP32BitRegClass) {
398 LoadOpcode = SystemZ::LE;
399 StoreOpcode = SystemZ::STE;
400 } else if (RC == &SystemZ::FP64BitRegClass) {
401 LoadOpcode = SystemZ::LD;
402 StoreOpcode = SystemZ::STD;
403 } else if (RC == &SystemZ::FP128BitRegClass) {
404 LoadOpcode = SystemZ::LX;
405 StoreOpcode = SystemZ::STX;
406 } else
407 llvm_unreachable("Unsupported regclass to load or store");
408}
409
410unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
411 int64_t Offset) const {
412 const MCInstrDesc &MCID = get(Opcode);
413 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
414 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
415 // Get the instruction to use for unsigned 12-bit displacements.
416 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
417 if (Disp12Opcode >= 0)
418 return Disp12Opcode;
419
420 // All address-related instructions can use unsigned 12-bit
421 // displacements.
422 return Opcode;
423 }
424 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
425 // Get the instruction to use for signed 20-bit displacements.
426 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
427 if (Disp20Opcode >= 0)
428 return Disp20Opcode;
429
430 // Check whether Opcode allows signed 20-bit displacements.
431 if (MCID.TSFlags & SystemZII::Has20BitOffset)
432 return Opcode;
433 }
434 return 0;
435}
436
437void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
438 MachineBasicBlock::iterator MBBI,
439 unsigned Reg, uint64_t Value) const {
440 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
441 unsigned Opcode;
442 if (isInt<16>(Value))
443 Opcode = SystemZ::LGHI;
444 else if (SystemZ::isImmLL(Value))
445 Opcode = SystemZ::LLILL;
446 else if (SystemZ::isImmLH(Value)) {
447 Opcode = SystemZ::LLILH;
448 Value >>= 16;
449 } else {
450 assert(isInt<32>(Value) && "Huge values not handled yet");
451 Opcode = SystemZ::LGFI;
452 }
453 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
454}