blob: 92418ac11f194f98dfd20db921220e37992d3183 [file] [log] [blame]
Anton Korobeynikov4403b932009-07-16 13:27:25 +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 "SystemZ.h"
Anton Korobeynikov4b730162009-07-16 14:01:27 +000015#include "SystemZInstrBuilder.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000016#include "SystemZInstrInfo.h"
17#include "SystemZMachineFunctionInfo.h"
18#include "SystemZTargetMachine.h"
19#include "SystemZGenInstrInfo.inc"
20#include "llvm/Function.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/PseudoSourceValue.h"
25
26using namespace llvm;
27
28SystemZInstrInfo::SystemZInstrInfo(SystemZTargetMachine &tm)
29 : TargetInstrInfoImpl(SystemZInsts, array_lengthof(SystemZInsts)),
Anton Korobeynikovef5deca2009-07-16 13:51:12 +000030 RI(tm, *this), TM(tm) {
31 // Fill the spill offsets map
32 static const unsigned SpillOffsTab[][2] = {
33 { SystemZ::R2D, 0x10 },
34 { SystemZ::R3D, 0x18 },
35 { SystemZ::R4D, 0x20 },
36 { SystemZ::R5D, 0x28 },
37 { SystemZ::R6D, 0x30 },
38 { SystemZ::R7D, 0x38 },
39 { SystemZ::R8D, 0x40 },
40 { SystemZ::R9D, 0x48 },
41 { SystemZ::R10D, 0x50 },
42 { SystemZ::R11D, 0x58 },
43 { SystemZ::R12D, 0x60 },
44 { SystemZ::R13D, 0x68 },
45 { SystemZ::R14D, 0x70 },
46 { SystemZ::R15D, 0x78 }
47 };
48
49 RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
50
51 for (unsigned i = 0, e = array_lengthof(SpillOffsTab); i != e; ++i)
52 RegSpillOffsets[SpillOffsTab[i][0]] = SpillOffsTab[i][1];
53}
Anton Korobeynikov4403b932009-07-16 13:27:25 +000054
55void SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
56 MachineBasicBlock::iterator MI,
57 unsigned SrcReg, bool isKill, int FrameIdx,
58 const TargetRegisterClass *RC) const {
Anton Korobeynikov4b730162009-07-16 14:01:27 +000059 DebugLoc DL = DebugLoc::getUnknownLoc();
60 if (MI != MBB.end()) DL = MI->getDebugLoc();
61
62 unsigned Opc = 0;
63 if (RC == &SystemZ::GR32RegClass ||
64 RC == &SystemZ::ADDR32RegClass)
65 Opc = SystemZ::MOV32mr;
66 else if (RC == &SystemZ::GR64RegClass ||
67 RC == &SystemZ::ADDR64RegClass) {
68 Opc = SystemZ::MOV64mr;
69 } else
70 assert(0 && "Unsupported regclass to store");
71
72 addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx)
73 .addReg(SrcReg, getKillRegState(isKill));
Anton Korobeynikov4403b932009-07-16 13:27:25 +000074}
75
76void SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
77 MachineBasicBlock::iterator MI,
78 unsigned DestReg, int FrameIdx,
79 const TargetRegisterClass *RC) const{
Anton Korobeynikov4b730162009-07-16 14:01:27 +000080 DebugLoc DL = DebugLoc::getUnknownLoc();
81 if (MI != MBB.end()) DL = MI->getDebugLoc();
82
83 unsigned Opc = 0;
84 if (RC == &SystemZ::GR32RegClass ||
85 RC == &SystemZ::ADDR32RegClass)
86 Opc = SystemZ::MOV32rm;
87 else if (RC == &SystemZ::GR64RegClass ||
88 RC == &SystemZ::ADDR64RegClass) {
89 Opc = SystemZ::MOV64rm;
90 } else
91 assert(0 && "Unsupported regclass to store");
92
93 addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000094}
95
96bool SystemZInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +000097 MachineBasicBlock::iterator I,
98 unsigned DestReg, unsigned SrcReg,
99 const TargetRegisterClass *DestRC,
100 const TargetRegisterClass *SrcRC) const {
101 DebugLoc DL = DebugLoc::getUnknownLoc();
102 if (I != MBB.end()) DL = I->getDebugLoc();
103
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000104 // Determine if DstRC and SrcRC have a common superclass.
105 const TargetRegisterClass *CommonRC = DestRC;
106 if (DestRC == SrcRC)
107 /* Same regclass for source and dest */;
108 else if (CommonRC->hasSuperClass(SrcRC))
109 CommonRC = SrcRC;
110 else if (!CommonRC->hasSubClass(SrcRC))
111 CommonRC = 0;
112
113 if (CommonRC) {
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000114 if (CommonRC == &SystemZ::GR64RegClass ||
115 CommonRC == &SystemZ::ADDR64RegClass) {
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000116 BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000117 } else if (CommonRC == &SystemZ::GR32RegClass ||
118 CommonRC == &SystemZ::ADDR32RegClass) {
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000119 BuildMI(MBB, I, DL, get(SystemZ::MOV32rr), DestReg).addReg(SrcReg);
120 } else if (CommonRC == &SystemZ::GR64PRegClass) {
121 BuildMI(MBB, I, DL, get(SystemZ::MOV64rrP), DestReg).addReg(SrcReg);
122 } else if (CommonRC == &SystemZ::GR128RegClass) {
123 BuildMI(MBB, I, DL, get(SystemZ::MOV128rr), DestReg).addReg(SrcReg);
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000124 } else {
125 return false;
126 }
127
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000128 return true;
129 }
130
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000131 if ((SrcRC == &SystemZ::GR64RegClass &&
132 DestRC == &SystemZ::ADDR64RegClass) ||
133 (DestRC == &SystemZ::GR64RegClass &&
134 SrcRC == &SystemZ::ADDR64RegClass)) {
135 BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg);
136 return true;
137 } else if ((SrcRC == &SystemZ::GR32RegClass &&
138 DestRC == &SystemZ::ADDR32RegClass) ||
139 (DestRC == &SystemZ::GR32RegClass &&
140 SrcRC == &SystemZ::ADDR32RegClass)) {
141 BuildMI(MBB, I, DL, get(SystemZ::MOV32rr), DestReg).addReg(SrcReg);
142 return true;
143 }
144
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000145 return false;
146}
147
148bool
149SystemZInstrInfo::isMoveInstr(const MachineInstr& MI,
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000150 unsigned &SrcReg, unsigned &DstReg,
151 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000152 switch (MI.getOpcode()) {
153 default:
154 return false;
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000155 case SystemZ::MOV32rr:
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000156 case SystemZ::MOV64rr:
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000157 case SystemZ::MOV64rrP:
158 case SystemZ::MOV128rr:
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000159 assert(MI.getNumOperands() >= 2 &&
160 MI.getOperand(0).isReg() &&
161 MI.getOperand(1).isReg() &&
162 "invalid register-register move instruction");
163 SrcReg = MI.getOperand(1).getReg();
164 DstReg = MI.getOperand(0).getReg();
Anton Korobeynikov54cea742009-07-16 14:12:54 +0000165 SrcSubIdx = MI.getOperand(1).getSubReg();
166 DstSubIdx = MI.getOperand(0).getSubReg();
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000167 return true;
168 }
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000169}
170
171bool
172SystemZInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
173 MachineBasicBlock::iterator MI,
174 const std::vector<CalleeSavedInfo> &CSI) const {
Anton Korobeynikovef5deca2009-07-16 13:51:12 +0000175 DebugLoc DL = DebugLoc::getUnknownLoc();
176 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000177
178 MachineFunction &MF = *MBB.getParent();
179 SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
180 MFI->setCalleeSavedFrameSize(CSI.size() * 8);
181
Anton Korobeynikovef5deca2009-07-16 13:51:12 +0000182 // Scan the callee-saved and find the bounds of register spill area.
183 unsigned LowReg = 0, HighReg = 0, StartOffset = -1U, EndOffset = 0;
184 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
185 unsigned Reg = CSI[i].getReg();
186 unsigned Offset = RegSpillOffsets[Reg];
187 if (StartOffset > Offset) {
188 LowReg = Reg; StartOffset = Offset;
189 }
190 if (EndOffset < Offset) {
191 HighReg = Reg; EndOffset = RegSpillOffsets[Reg];
192 }
193 }
194
195 // Save information for epilogue inserter.
196 MFI->setLowReg(LowReg); MFI->setHighReg(HighReg);
197
198 // Build a store instruction. Use STORE MULTIPLE instruction if there are many
199 // registers to store, otherwise - just STORE.
200 MachineInstrBuilder MIB =
201 BuildMI(MBB, MI, DL, get((LowReg == HighReg ?
202 SystemZ::MOV64mr : SystemZ::MOV64mrm)));
203
204 // Add store operands.
205 MIB.addReg(SystemZ::R15D).addImm(StartOffset);
206 if (LowReg == HighReg)
207 MIB.addReg(0);
208 MIB.addReg(LowReg, RegState::Kill);
209 if (LowReg != HighReg)
210 MIB.addReg(HighReg, RegState::Kill);
211
212 // Do a second scan adding regs as being killed by instruction
213 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
214 unsigned Reg = CSI[i].getReg();
215 // Add the callee-saved register as live-in. It's killed at the spill.
216 MBB.addLiveIn(Reg);
217 if (Reg != LowReg && Reg != HighReg)
218 MIB.addReg(Reg, RegState::ImplicitKill);
219 }
220
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000221 return true;
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000222}
223
224bool
225SystemZInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
226 MachineBasicBlock::iterator MI,
227 const std::vector<CalleeSavedInfo> &CSI) const {
Anton Korobeynikovef5deca2009-07-16 13:51:12 +0000228 if (CSI.empty())
229 return false;
230
231 DebugLoc DL = DebugLoc::getUnknownLoc();
232 if (MI != MBB.end()) DL = MI->getDebugLoc();
233
234 MachineFunction &MF = *MBB.getParent();
235 const TargetRegisterInfo *RegInfo= MF.getTarget().getRegisterInfo();
236 SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
237
238 unsigned LowReg = MFI->getLowReg(), HighReg = MFI->getHighReg();
239 unsigned StartOffset = RegSpillOffsets[LowReg];
240
241 // Build a load instruction. Use LOAD MULTIPLE instruction if there are many
242 // registers to load, otherwise - just LOAD.
243 MachineInstrBuilder MIB =
244 BuildMI(MBB, MI, DL, get((LowReg == HighReg ?
245 SystemZ::MOV64rm : SystemZ::MOV64rmm)));
246 // Add store operands.
247 MIB.addReg(LowReg, RegState::Define);
248 if (LowReg != HighReg)
249 MIB.addReg(HighReg, RegState::Define);
250
251 MIB.addReg((RegInfo->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D));
252 MIB.addImm(StartOffset);
253 if (LowReg == HighReg)
254 MIB.addReg(0);
255
256 // Do a second scan adding regs as being defined by instruction
257 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
258 unsigned Reg = CSI[i].getReg();
259 if (Reg != LowReg && Reg != HighReg)
260 MIB.addReg(Reg, RegState::ImplicitDefine);
261 }
262
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000263 return true;
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000264}
265
266unsigned
267SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
268 MachineBasicBlock *FBB,
269 const SmallVectorImpl<MachineOperand> &Cond) const {
Anton Korobeynikov64d52d42009-07-16 14:00:10 +0000270 // FIXME this should probably have a DebugLoc operand
271 DebugLoc dl = DebugLoc::getUnknownLoc();
272 // Shouldn't be a fall through.
273 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
274 assert((Cond.size() == 1 || Cond.size() == 0) &&
275 "SystemZ branch conditions have one component!");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000276
Anton Korobeynikov64d52d42009-07-16 14:00:10 +0000277 if (Cond.empty()) {
278 // Unconditional branch?
279 assert(!FBB && "Unconditional branch with multiple successors!");
280 BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(TBB);
281 return 1;
282 }
283
284 // Conditional branch.
285 unsigned Count = 0;
286 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)Cond[0].getImm();
287 BuildMI(&MBB, dl, getBrCond(CC)).addMBB(TBB);
288 ++Count;
289
290 if (FBB) {
291 // Two-way Conditional branch. Insert the second branch.
292 BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(FBB);
293 ++Count;
294 }
295 return Count;
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000296}
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000297
298const TargetInstrDesc&
299SystemZInstrInfo::getBrCond(SystemZCC::CondCodes CC) const {
300 unsigned Opc;
301 switch (CC) {
302 default:
303 assert(0 && "Unknown condition code!");
304 case SystemZCC::E:
305 Opc = SystemZ::JE;
306 break;
307 case SystemZCC::NE:
308 Opc = SystemZ::JNE;
309 break;
310 case SystemZCC::H:
311 Opc = SystemZ::JH;
312 break;
313 case SystemZCC::L:
314 Opc = SystemZ::JL;
315 break;
316 case SystemZCC::HE:
317 Opc = SystemZ::JHE;
318 break;
319 case SystemZCC::LE:
320 Opc = SystemZ::JLE;
321 break;
322 }
323
324 return get(Opc);
325}
Anton Korobeynikov5a11e022009-07-16 14:09:56 +0000326
327const TargetInstrDesc&
328SystemZInstrInfo::getLongDispOpc(unsigned Opc) const {
329 switch (Opc) {
330 case SystemZ::MOV32mr:
331 Opc = SystemZ::MOV32mry;
332 break;
333 case SystemZ::MOV32rm:
334 Opc = SystemZ::MOV32rmy;
335 break;
336 case SystemZ::MOVSX32rm16:
337 Opc = SystemZ::MOVSX32rm16y;
338 break;
339 case SystemZ::MOV32m8r:
340 Opc = SystemZ::MOV32m8ry;
341 break;
342 case SystemZ::MOV32m16r:
343 Opc = SystemZ::MOV32m16ry;
344 break;
345 case SystemZ::MOV64m8r:
346 Opc = SystemZ::MOV64m8ry;
347 break;
348 case SystemZ::MOV64m16r:
349 Opc = SystemZ::MOV64m16ry;
350 break;
351 case SystemZ::MOV64m32r:
352 Opc = SystemZ::MOV64m32ry;
353 break;
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000354 case SystemZ::MOV8mi:
355 Opc = SystemZ::MOV8miy;
356 break;
Anton Korobeynikov5a11e022009-07-16 14:09:56 +0000357 case SystemZ::MUL32rm:
358 Opc = SystemZ::MUL32rmy;
359 break;
360 case SystemZ::CMP32rm:
361 Opc = SystemZ::CMP32rmy;
362 break;
363 case SystemZ::UCMP32rm:
364 Opc = SystemZ::UCMP32rmy;
365 break;
366 default:
367 break;
368 }
369
370 return get(Opc);
371}
372