Fix fallout from 12-bit stuff landing: decide whether 20 bit displacements are needed...
[oota-llvm.git] / lib / Target / SystemZ / SystemZInstrInfo.cpp
1 //===- 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"
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"
25
26 using namespace llvm;
27
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 }
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 }
54
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();
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));
74 }
75
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();
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);
94 }
95
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();
103
104   // 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) {
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);
124     } else {
125       return false;
126     }
127
128     return true;
129   }
130
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);
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
145   return false;
146 }
147
148 bool
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.
153
154   switch (MI.getOpcode()) {
155   default:
156     return false;
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();
167     return true;
168   }
169 }
170
171 bool
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();
177
178   MachineFunction &MF = *MBB.getParent();
179   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
180   MFI->setCalleeSavedFrameSize(CSI.size() * 8);
181
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;
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
221   return true;
222 }
223
224 bool
225 SystemZInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
226                                              MachineBasicBlock::iterator MI,
227                                 const std::vector<CalleeSavedInfo> &CSI) const {
228   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
263   return true;
264 }
265
266 unsigned
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!");
276
277   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;
296 }
297
298 const TargetInstrDesc&
299 SystemZInstrInfo::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 }
326
327 const TargetInstrDesc&
328 SystemZInstrInfo::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;
354   case SystemZ::MUL32rm:
355     Opc = SystemZ::MUL32rmy;
356     break;
357   case SystemZ::CMP32rm:
358     Opc = SystemZ::CMP32rmy;
359     break;
360   case SystemZ::UCMP32rm:
361     Opc = SystemZ::UCMP32rmy;
362     break;
363   default:
364     break;
365   }
366
367   return get(Opc);
368 }
369