Change MachineInstr ctor's to take a TargetInstrDescriptor reference instead
[oota-llvm.git] / lib / Target / Alpha / AlphaRegisterInfo.cpp
1 //===- AlphaRegisterInfo.cpp - Alpha Register Information -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the Alpha implementation of the MRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "reginfo"
15 #include "Alpha.h"
16 #include "AlphaRegisterInfo.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Type.h"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineLocation.h"
25 #include "llvm/Target/TargetFrameInfo.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/Target/TargetInstrInfo.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include <cstdlib>
33 #include <iostream>
34 using namespace llvm;
35
36 //These describe LDAx
37 static const int IMM_LOW  = -32768;
38 static const int IMM_HIGH = 32767;
39 static const int IMM_MULT = 65536;
40
41 static long getUpper16(long l)
42 {
43   long y = l / IMM_MULT;
44   if (l % IMM_MULT > IMM_HIGH)
45     ++y;
46   return y;
47 }
48
49 static long getLower16(long l)
50 {
51   long h = getUpper16(l);
52   return l - h * IMM_MULT;
53 }
54
55 AlphaRegisterInfo::AlphaRegisterInfo(const TargetInstrInfo &tii)
56   : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP),
57     TII(tii)
58 {
59 }
60
61 void
62 AlphaRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
63                                        MachineBasicBlock::iterator MI,
64                                        unsigned SrcReg, int FrameIdx,
65                                        const TargetRegisterClass *RC) const {
66   //std::cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
67   //<< FrameIdx << "\n";
68   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
69   if (RC == Alpha::F4RCRegisterClass)
70     BuildMI(MBB, MI, TII.get(Alpha::STS))
71       .addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
72   else if (RC == Alpha::F8RCRegisterClass)
73     BuildMI(MBB, MI, TII.get(Alpha::STT))
74       .addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
75   else if (RC == Alpha::GPRCRegisterClass)
76     BuildMI(MBB, MI, TII.get(Alpha::STQ))
77       .addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
78   else
79     abort();
80 }
81
82 void
83 AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
84                                         MachineBasicBlock::iterator MI,
85                                         unsigned DestReg, int FrameIdx,
86                                         const TargetRegisterClass *RC) const {
87   //std::cerr << "Trying to load " << getPrettyName(DestReg) << " to "
88   //<< FrameIdx << "\n";
89   if (RC == Alpha::F4RCRegisterClass)
90     BuildMI(MBB, MI, TII.get(Alpha::LDS), DestReg)
91       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
92   else if (RC == Alpha::F8RCRegisterClass)
93     BuildMI(MBB, MI, TII.get(Alpha::LDT), DestReg)
94       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
95   else if (RC == Alpha::GPRCRegisterClass)
96     BuildMI(MBB, MI, TII.get(Alpha::LDQ), DestReg)
97       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
98   else
99     abort();
100 }
101
102 MachineInstr *AlphaRegisterInfo::foldMemoryOperand(MachineInstr *MI,
103                                                  unsigned OpNum,
104                                                  int FrameIndex) const {
105    // Make sure this is a reg-reg copy.
106    unsigned Opc = MI->getOpcode();
107
108    MachineInstr *NewMI = NULL;
109    switch(Opc) {
110    default:
111      break;
112    case Alpha::BISr:
113    case Alpha::CPYSS:
114    case Alpha::CPYST:
115      if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
116        if (OpNum == 0) {  // move -> store
117          unsigned InReg = MI->getOperand(1).getReg();
118          Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 
119            ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
120          NewMI = BuildMI(TII.get(Opc)).addReg(InReg).addFrameIndex(FrameIndex)
121            .addReg(Alpha::F31);
122        } else {           // load -> move
123          unsigned OutReg = MI->getOperand(0).getReg();
124          Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 
125            ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
126          NewMI = BuildMI(TII.get(Opc), OutReg).addFrameIndex(FrameIndex)
127            .addReg(Alpha::F31);
128        }
129      }
130      break;
131    }
132   if (NewMI)
133     NewMI->copyKillDeadInfo(MI);
134   return 0;
135 }
136
137
138 void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
139                                      MachineBasicBlock::iterator MI,
140                                      unsigned DestReg, unsigned SrcReg,
141                                      const TargetRegisterClass *RC) const {
142   //  std::cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
143   if (RC == Alpha::GPRCRegisterClass) {
144     BuildMI(MBB, MI, TII.get(Alpha::BISr), DestReg).addReg(SrcReg).addReg(SrcReg);
145   } else if (RC == Alpha::F4RCRegisterClass) {
146     BuildMI(MBB, MI, TII.get(Alpha::CPYSS), DestReg).addReg(SrcReg).addReg(SrcReg);
147   } else if (RC == Alpha::F8RCRegisterClass) {
148     BuildMI(MBB, MI, TII.get(Alpha::CPYST), DestReg).addReg(SrcReg).addReg(SrcReg);
149   } else {
150     std::cerr << "Attempt to copy register that is not GPR or FPR";
151      abort();
152   }
153 }
154
155 const unsigned* AlphaRegisterInfo::getCalleeSaveRegs() const {
156   static const unsigned CalleeSaveRegs[] = {
157     Alpha::R9, Alpha::R10,
158     Alpha::R11, Alpha::R12,
159     Alpha::R13, Alpha::R14,
160     Alpha::F2, Alpha::F3,
161     Alpha::F4, Alpha::F5,
162     Alpha::F6, Alpha::F7,
163     Alpha::F8, Alpha::F9,  0
164   };
165   return CalleeSaveRegs;
166 }
167
168 const TargetRegisterClass* const*
169 AlphaRegisterInfo::getCalleeSaveRegClasses() const {
170   static const TargetRegisterClass * const CalleeSaveRegClasses[] = {
171     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
172     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
173     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
174     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
175     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
176     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
177     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,  0
178   };
179   return CalleeSaveRegClasses;
180 }
181
182 //===----------------------------------------------------------------------===//
183 // Stack Frame Processing methods
184 //===----------------------------------------------------------------------===//
185
186 // hasFP - Return true if the specified function should have a dedicated frame
187 // pointer register.  This is true if the function has variable sized allocas or
188 // if frame pointer elimination is disabled.
189 //
190 static bool hasFP(const MachineFunction &MF) {
191   MachineFrameInfo *MFI = MF.getFrameInfo();
192   return MFI->hasVarSizedObjects();
193 }
194
195 void AlphaRegisterInfo::
196 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
197                               MachineBasicBlock::iterator I) const {
198   if (hasFP(MF)) {
199     // If we have a frame pointer, turn the adjcallstackup instruction into a
200     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
201     // <amt>'
202     MachineInstr *Old = I;
203     uint64_t Amount = Old->getOperand(0).getImmedValue();
204     if (Amount != 0) {
205       // We need to keep the stack aligned properly.  To do this, we round the
206       // amount of space needed for the outgoing arguments up to the next
207       // alignment boundary.
208       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
209       Amount = (Amount+Align-1)/Align*Align;
210
211       MachineInstr *New;
212       if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
213         New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
214           .addImm(-Amount).addReg(Alpha::R30);
215       } else {
216          assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
217          New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
218           .addImm(Amount).addReg(Alpha::R30);
219       }
220
221       // Replace the pseudo instruction with a new instruction...
222       MBB.insert(I, New);
223     }
224   }
225
226   MBB.erase(I);
227 }
228
229 //Alpha has a slightly funny stack:
230 //Args
231 //<- incoming SP
232 //fixed locals (and spills, callee saved, etc)
233 //<- FP
234 //variable locals
235 //<- SP
236
237 void
238 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
239   unsigned i = 0;
240   MachineInstr &MI = *II;
241   MachineBasicBlock &MBB = *MI.getParent();
242   MachineFunction &MF = *MBB.getParent();
243   bool FP = hasFP(MF);
244
245   while (!MI.getOperand(i).isFrameIndex()) {
246     ++i;
247     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
248   }
249
250   int FrameIndex = MI.getOperand(i).getFrameIndex();
251
252   // Add the base register of R30 (SP) or R15 (FP).
253   MI.getOperand(i + 1).ChangeToRegister(FP ? Alpha::R15 : Alpha::R30, false);
254
255   // Now add the frame object offset to the offset from the virtual frame index.
256   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
257
258   DEBUG(std::cerr << "FI: " << FrameIndex << " Offset: " << Offset << "\n");
259
260   Offset += MF.getFrameInfo()->getStackSize();
261
262   DEBUG(std::cerr << "Corrected Offset " << Offset <<
263         " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n");
264
265   if (Offset > IMM_HIGH || Offset < IMM_LOW) {
266     DEBUG(std::cerr << "Unconditionally using R28 for evil purposes Offset: "
267           << Offset << "\n");
268     //so in this case, we need to use a temporary register, and move the
269     //original inst off the SP/FP
270     //fix up the old:
271     MI.getOperand(i + 1).ChangeToRegister(Alpha::R28, false);
272     MI.getOperand(i).ChangeToImmediate(getLower16(Offset));
273     //insert the new
274     MachineInstr* nMI=BuildMI(TII.get(Alpha::LDAH), Alpha::R28)
275       .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
276     MBB.insert(II, nMI);
277   } else {
278     MI.getOperand(i).ChangeToImmediate(Offset);
279   }
280 }
281
282
283 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
284   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
285   MachineBasicBlock::iterator MBBI = MBB.begin();
286   MachineFrameInfo *MFI = MF.getFrameInfo();
287   bool FP = hasFP(MF);
288
289   static int curgpdist = 0;
290
291   //handle GOP offset
292   BuildMI(MBB, MBBI, TII.get(Alpha::LDAHg), Alpha::R29)
293     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
294     .addReg(Alpha::R27).addImm(++curgpdist);
295   BuildMI(MBB, MBBI, TII.get(Alpha::LDAg), Alpha::R29)
296     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
297     .addReg(Alpha::R29).addImm(curgpdist);
298
299   //evil const_cast until MO stuff setup to handle const
300   BuildMI(MBB, MBBI, TII.get(Alpha::ALTENT))
301     .addGlobalAddress(const_cast<Function*>(MF.getFunction()));
302
303   // Get the number of bytes to allocate from the FrameInfo
304   long NumBytes = MFI->getStackSize();
305
306   if (MFI->hasCalls() && !FP) {
307     // We reserve argument space for call sites in the function immediately on
308     // entry to the current function.  This eliminates the need for add/sub
309     // brackets around call sites.
310     //If there is a frame pointer, then we don't do this
311     NumBytes += MFI->getMaxCallFrameSize();
312     DEBUG(std::cerr << "Added " << MFI->getMaxCallFrameSize()
313           << " to the stack due to calls\n");
314   }
315
316   if (FP)
317     NumBytes += 8; //reserve space for the old FP
318
319   // Do we need to allocate space on the stack?
320   if (NumBytes == 0) return;
321
322   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
323   NumBytes = (NumBytes+Align-1)/Align*Align;
324
325   // Update frame info to pretend that this is part of the stack...
326   MFI->setStackSize(NumBytes);
327
328   // adjust stack pointer: r30 -= numbytes
329   NumBytes = -NumBytes;
330   if (NumBytes >= IMM_LOW) {
331     BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
332       .addReg(Alpha::R30);
333   } else if (getUpper16(NumBytes) >= IMM_LOW) {
334     BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30).addImm(getUpper16(NumBytes))
335       .addReg(Alpha::R30);
336     BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(getLower16(NumBytes))
337       .addReg(Alpha::R30);
338   } else {
339     std::cerr << "Too big a stack frame at " << NumBytes << "\n";
340     abort();
341   }
342
343   //now if we need to, save the old FP and set the new
344   if (FP)
345   {
346     BuildMI(MBB, MBBI, TII.get(Alpha::STQ))
347       .addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
348     //this must be the last instr in the prolog
349     BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R15)
350       .addReg(Alpha::R30).addReg(Alpha::R30);
351   }
352
353 }
354
355 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
356                                      MachineBasicBlock &MBB) const {
357   const MachineFrameInfo *MFI = MF.getFrameInfo();
358   MachineBasicBlock::iterator MBBI = prior(MBB.end());
359   assert(MBBI->getOpcode() == Alpha::RETDAG ||
360          MBBI->getOpcode() == Alpha::RETDAGp
361          && "Can only insert epilog into returning blocks");
362
363   bool FP = hasFP(MF);
364
365   // Get the number of bytes allocated from the FrameInfo...
366   long NumBytes = MFI->getStackSize();
367
368   //now if we need to, restore the old FP
369   if (FP)
370   {
371     //copy the FP into the SP (discards allocas)
372     BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R30).addReg(Alpha::R15)
373       .addReg(Alpha::R15);
374     //restore the FP
375     BuildMI(MBB, MBBI, TII.get(Alpha::LDQ), Alpha::R15).addImm(0).addReg(Alpha::R15);
376   }
377
378    if (NumBytes != 0)
379      {
380        if (NumBytes <= IMM_HIGH) {
381          BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
382            .addReg(Alpha::R30);
383        } else if (getUpper16(NumBytes) <= IMM_HIGH) {
384          BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30)
385            .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
386          BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30)
387            .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
388        } else {
389          std::cerr << "Too big a stack frame at " << NumBytes << "\n";
390          abort();
391        }
392      }
393 }
394
395 unsigned AlphaRegisterInfo::getRARegister() const {
396   assert(0 && "What is the return address register");
397   return 0;
398 }
399
400 unsigned AlphaRegisterInfo::getFrameRegister(MachineFunction &MF) const {
401   return hasFP(MF) ? Alpha::R15 : Alpha::R30;
402 }
403
404 #include "AlphaGenRegisterInfo.inc"
405
406 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
407 {
408   std::string s(RegisterDescriptors[reg].Name);
409   return s;
410 }