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