1 //===- SystemZInstrInfo.cpp - SystemZ Instruction Information --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the SystemZ implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "SystemZInstrBuilder.h"
16 #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"
28 SystemZInstrInfo::SystemZInstrInfo(SystemZTargetMachine &tm)
29 : TargetInstrInfoImpl(SystemZInsts, array_lengthof(SystemZInsts)),
30 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 }
49 RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
51 for (unsigned i = 0, e = array_lengthof(SpillOffsTab); i != e; ++i)
52 RegSpillOffsets[SpillOffsTab[i][0]] = SpillOffsTab[i][1];
55 void SystemZInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
56 MachineBasicBlock::iterator MI,
57 unsigned SrcReg, bool isKill, int FrameIdx,
58 const TargetRegisterClass *RC) const {
59 DebugLoc DL = DebugLoc::getUnknownLoc();
60 if (MI != MBB.end()) DL = MI->getDebugLoc();
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;
70 assert(0 && "Unsupported regclass to store");
72 addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx)
73 .addReg(SrcReg, getKillRegState(isKill));
76 void SystemZInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
77 MachineBasicBlock::iterator MI,
78 unsigned DestReg, int FrameIdx,
79 const TargetRegisterClass *RC) const{
80 DebugLoc DL = DebugLoc::getUnknownLoc();
81 if (MI != MBB.end()) DL = MI->getDebugLoc();
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;
91 assert(0 && "Unsupported regclass to store");
93 addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx);
96 bool SystemZInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
97 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();
104 // Determine if DstRC and SrcRC have a common superclass.
105 const TargetRegisterClass *CommonRC = DestRC;
107 /* Same regclass for source and dest */;
108 else if (CommonRC->hasSuperClass(SrcRC))
110 else if (!CommonRC->hasSubClass(SrcRC))
114 if (CommonRC == &SystemZ::GR64RegClass ||
115 CommonRC == &SystemZ::ADDR64RegClass) {
116 BuildMI(MBB, I, DL, get(SystemZ::MOV64rr), DestReg).addReg(SrcReg);
117 } else if (CommonRC == &SystemZ::GR32RegClass ||
118 CommonRC == &SystemZ::ADDR32RegClass) {
119 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);
131 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);
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);
149 SystemZInstrInfo::isMoveInstr(const MachineInstr& MI,
150 unsigned &SrcReg, unsigned &DstReg,
151 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
152 SrcSubIdx = DstSubIdx = 0; // No sub-registers yet.
154 switch (MI.getOpcode()) {
157 case SystemZ::MOV32rr:
158 case SystemZ::MOV64rr:
159 case SystemZ::MOV64rrP:
160 case SystemZ::MOV128rr:
161 assert(MI.getNumOperands() >= 2 &&
162 MI.getOperand(0).isReg() &&
163 MI.getOperand(1).isReg() &&
164 "invalid register-register move instruction");
165 SrcReg = MI.getOperand(1).getReg();
166 DstReg = MI.getOperand(0).getReg();
172 SystemZInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
173 MachineBasicBlock::iterator MI,
174 const std::vector<CalleeSavedInfo> &CSI) const {
175 DebugLoc DL = DebugLoc::getUnknownLoc();
176 if (MI != MBB.end()) DL = MI->getDebugLoc();
178 MachineFunction &MF = *MBB.getParent();
179 SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
180 MFI->setCalleeSavedFrameSize(CSI.size() * 8);
182 // 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;
190 if (EndOffset < Offset) {
191 HighReg = Reg; EndOffset = RegSpillOffsets[Reg];
195 // Save information for epilogue inserter.
196 MFI->setLowReg(LowReg); MFI->setHighReg(HighReg);
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)));
204 // Add store operands.
205 MIB.addReg(SystemZ::R15D).addImm(StartOffset);
206 if (LowReg == HighReg)
208 MIB.addReg(LowReg, RegState::Kill);
209 if (LowReg != HighReg)
210 MIB.addReg(HighReg, RegState::Kill);
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.
217 if (Reg != LowReg && Reg != HighReg)
218 MIB.addReg(Reg, RegState::ImplicitKill);
225 SystemZInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
226 MachineBasicBlock::iterator MI,
227 const std::vector<CalleeSavedInfo> &CSI) const {
231 DebugLoc DL = DebugLoc::getUnknownLoc();
232 if (MI != MBB.end()) DL = MI->getDebugLoc();
234 MachineFunction &MF = *MBB.getParent();
235 const TargetRegisterInfo *RegInfo= MF.getTarget().getRegisterInfo();
236 SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
238 unsigned LowReg = MFI->getLowReg(), HighReg = MFI->getHighReg();
239 unsigned StartOffset = RegSpillOffsets[LowReg];
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);
251 MIB.addReg((RegInfo->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D));
252 MIB.addImm(StartOffset);
253 if (LowReg == HighReg)
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);
267 SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
268 MachineBasicBlock *FBB,
269 const SmallVectorImpl<MachineOperand> &Cond) const {
270 // 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!");
278 // Unconditional branch?
279 assert(!FBB && "Unconditional branch with multiple successors!");
280 BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(TBB);
284 // Conditional branch.
286 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)Cond[0].getImm();
287 BuildMI(&MBB, dl, getBrCond(CC)).addMBB(TBB);
291 // Two-way Conditional branch. Insert the second branch.
292 BuildMI(&MBB, dl, get(SystemZ::JMP)).addMBB(FBB);
298 const TargetInstrDesc&
299 SystemZInstrInfo::getBrCond(SystemZCC::CondCodes CC) const {
303 assert(0 && "Unknown condition code!");