Remove some redundant checks, add a couple of new ones. This allows us to
[oota-llvm.git] / lib / Target / Alpha / AlphaRegisterInfo.cpp
index 58a1a8d9a455647c456fd657b9b68883537b69e9..96a05938f1a59f32fe447963f64885a67c82443a 100644 (file)
@@ -1,4 +1,4 @@
-//===- PPC64RegisterInfo.cpp - PowerPC64 Register Information ---*- C++ -*-===//
+//===- AlphaRegisterInfo.cpp - Alpha Register Information -------*- C++ -*-===//
 // 
 //                     The LLVM Compiler Infrastructure
 //
@@ -7,13 +7,12 @@
 // 
 //===----------------------------------------------------------------------===//
 //
-// This file contains the PowerPC64 implementation of the MRegisterInfo class.
+// This file contains the Alpha implementation of the MRegisterInfo class.
 //
 //===----------------------------------------------------------------------===//
 
 #define DEBUG_TYPE "reginfo"
 #include "Alpha.h"
-#include "AlphaInstrBuilder.h"
 #include "AlphaRegisterInfo.h"
 #include "llvm/Constants.h"
 #include "llvm/Type.h"
 #include <iostream>
 using namespace llvm;
 
+//These describe LDAx
+static const int IMM_LOW  = -32768;
+static const int IMM_HIGH = 32767;
+static const int IMM_MULT = 65536;
+
+static long getUpper16(long l)
+{
+  long y = l / IMM_MULT;
+  if (l % IMM_MULT > IMM_HIGH)
+    ++y;
+  return y;
+}
+
+static long getLower16(long l)
+{
+  long h = getUpper16(l);
+  return l - h * IMM_MULT;
+}
 
 AlphaRegisterInfo::AlphaRegisterInfo()
   : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP)
@@ -48,20 +65,27 @@ void
 AlphaRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
                                        MachineBasicBlock::iterator MI,
                                        unsigned SrcReg, int FrameIdx) const {
-  std::cerr << "Trying to store " << getPrettyName(SrcReg) << " to " << FrameIdx << "\n";
+  //std::cerr << "Trying to store " << getPrettyName(SrcReg) << " to " << FrameIdx << "\n";
   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
-  BuildMI(MBB, MI, Alpha::STQ, 3).addReg(SrcReg).addImm(FrameIdx * 8).addReg(Alpha::R30);
-  //  assert(0 && "TODO");
+  if (getClass(SrcReg) == Alpha::FPRCRegisterClass)
+    BuildMI(MBB, MI, Alpha::STT, 3).addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
+  else if (getClass(SrcReg) == Alpha::GPRCRegisterClass)
+    BuildMI(MBB, MI, Alpha::STQ, 3).addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
+  else
+    abort();
 }
 
 void
 AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
                                         MachineBasicBlock::iterator MI,
                                         unsigned DestReg, int FrameIdx) const{
-  std::cerr << "Trying to load " << getPrettyName(DestReg) << " to " << FrameIdx << "\n";
-  //BuildMI(MBB, MI, Alpha::WTF, 0, DestReg);
-  BuildMI(MBB, MI, Alpha::LDQ, 2, DestReg).addImm(FrameIdx * 8).addReg(Alpha::R30);
-  //  assert(0 && "TODO");
+  //std::cerr << "Trying to load " << getPrettyName(DestReg) << " to " << FrameIdx << "\n";
+  if (getClass(DestReg) == Alpha::FPRCRegisterClass)
+    BuildMI(MBB, MI, Alpha::LDT, 2, DestReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
+  else if (getClass(DestReg) == Alpha::GPRCRegisterClass)
+    BuildMI(MBB, MI, Alpha::LDQ, 2, DestReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
+  else
+    abort();
 }
 
 void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
@@ -71,8 +95,8 @@ void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
   //  std::cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
   if (RC == Alpha::GPRCRegisterClass) {
     BuildMI(MBB, MI, Alpha::BIS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
-//   } else if (RC == Alpha::FPRCRegisterClass) {
-//     BuildMI(MBB, MI, PPC::FMR, 1, DestReg).addReg(SrcReg);
+  } else if (RC == Alpha::FPRCRegisterClass) {
+    BuildMI(MBB, MI, Alpha::CPYS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
   } else { 
     std::cerr << "Attempt to copy register that is not GPR or FPR";
      abort();
@@ -96,7 +120,6 @@ void AlphaRegisterInfo::
 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
                               MachineBasicBlock::iterator I) const {
   if (hasFP(MF)) {
-    assert(0 && "TODO");
     // If we have a frame pointer, turn the adjcallstackup instruction into a
     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
     // <amt>'
@@ -110,15 +133,15 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
       Amount = (Amount+Align-1)/Align*Align;
 
       MachineInstr *New;
-//       if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) {
-//     New=BuildMI(X86::SUB32ri, 1, X86::ESP, MachineOperand::UseAndDef)
-//               .addZImm(Amount);
-//       } else {
-//     assert(Old->getOpcode() == X86::ADJCALLSTACKUP);
-//     New=BuildMI(X86::ADD32ri, 1, X86::ESP, MachineOperand::UseAndDef)
-//               .addZImm(Amount);
-//       }
-
+      if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
+       New=BuildMI(Alpha::LDA, 2, Alpha::R30)
+          .addImm(-Amount).addReg(Alpha::R30);
+      } else {
+       assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
+       New=BuildMI(Alpha::LDA, 2, Alpha::R30)
+          .addImm(Amount).addReg(Alpha::R30);
+      }
+      
       // Replace the pseudo instruction with a new instruction...
       MBB.insert(I, New);
     }
@@ -127,55 +150,56 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
   MBB.erase(I);
 }
 
+//Alpha has a slightly funny stack:
+//Args 
+//<- incoming SP
+//fixed locals (and spills, callee saved, etc)
+//<- FP
+//variable locals
+//<- SP
+
 void
 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
-  assert(0 && "TODO");
-//   unsigned i = 0;
-//   MachineInstr &MI = *II;
-//   MachineBasicBlock &MBB = *MI.getParent();
-//   MachineFunction &MF = *MBB.getParent();
-  
-//   while (!MI.getOperand(i).isFrameIndex()) {
-//     ++i;
-//     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
-//   }
+  unsigned i = 0;
+  MachineInstr &MI = *II;
+  MachineBasicBlock &MBB = *MI.getParent();
+  MachineFunction &MF = *MBB.getParent();
+  bool FP = hasFP(MF);
 
-//   int FrameIndex = MI.getOperand(i).getFrameIndex();
+  while (!MI.getOperand(i).isFrameIndex()) {
+    ++i;
+    assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
+  }
 
-//   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
-//   MI.SetMachineOperandReg(i, hasFP(MF) ? PPC::R31 : PPC::R1);
+  int FrameIndex = MI.getOperand(i).getFrameIndex();
 
-//   // Take into account whether it's an add or mem instruction
-//   unsigned OffIdx = (i == 2) ? 1 : 2;
+  // Add the base register of R30 (SP) or R15 (FP).
+  MI.SetMachineOperandReg(i + 1, FP ? Alpha::R15 : Alpha::R30);
+  
+  // Now add the frame object offset to the offset from the virtual frame index.
+  int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
 
-//   // Now add the frame object offset to the offset from r1.
-//   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
-//                MI.getOperand(OffIdx).getImmedValue();
+  DEBUG(std::cerr << "FI: " << FrameIndex << " Offset: " << Offset << "\n");
 
-//   // If we're not using a Frame Pointer that has been set to the value of the
-//   // SP before having the stack size subtracted from it, then add the stack size
-//   // to Offset to get the correct offset.
-//   Offset += MF.getFrameInfo()->getStackSize();
+  Offset += MF.getFrameInfo()->getStackSize();
   
-//   if (Offset > 32767 || Offset < -32768) {
-//     // Insert a set of r0 with the full offset value before the ld, st, or add
-//     MachineBasicBlock *MBB = MI.getParent();
-//     MBB->insert(II, BuildMI(PPC::LIS, 1, PPC::R0).addSImm(Offset >> 16));
-//     MBB->insert(II, BuildMI(PPC::ORI, 2, PPC::R0).addReg(PPC::R0)
-//       .addImm(Offset));
-//     // convert into indexed form of the instruction
-//     // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
-//     // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
-//     unsigned NewOpcode = 
-//       const_cast<std::map<unsigned, unsigned>& >(ImmToIdxMap)[MI.getOpcode()];
-//     assert(NewOpcode && "No indexed form of load or store available!");
-//     MI.setOpcode(NewOpcode);
-//     MI.SetMachineOperandReg(1, MI.getOperand(i).getReg());
-//     MI.SetMachineOperandReg(2, PPC::R0);
-//   } else {
-//     MI.SetMachineOperandConst(OffIdx, MachineOperand::MO_SignExtendedImmed,
-//                               Offset);
-//   }
+  DEBUG(std::cerr << "Corrected Offset " << Offset << 
+        " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n");
+
+  if (Offset > IMM_HIGH || Offset < IMM_LOW) {
+    //so in this case, we need to use a temporary register, and move the original
+    //inst off the SP/FP
+    //fix up the old:
+    MI.SetMachineOperandReg(i + 1, Alpha::R28);
+    MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed, 
+                              getLower16(Offset));
+    //insert the new
+    MachineInstr* nMI=BuildMI(Alpha::LDAH, 2, Alpha::R28)
+      .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
+    MBB.insert(II, nMI);
+  } else {
+    MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed, Offset);
+  }
 }
 
 
@@ -184,35 +208,59 @@ void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
   MachineBasicBlock::iterator MBBI = MBB.begin();
   MachineFrameInfo *MFI = MF.getFrameInfo();
   MachineInstr *MI;
+  bool FP = hasFP(MF);
   
   //handle GOP offset
   MI = BuildMI(Alpha::LDGP, 0);
   MBB.insert(MBBI, MI);
 
   // Get the number of bytes to allocate from the FrameInfo
-  unsigned NumBytes = MFI->getStackSize();
+  long NumBytes = MFI->getStackSize();
+
+  if (MFI->hasCalls() && !FP) {
+    // We reserve argument space for call sites in the function immediately on 
+    // entry to the current function.  This eliminates the need for add/sub 
+    // brackets around call sites.
+    //If there is a frame pointer, then we don't do this
+    NumBytes += MFI->getMaxCallFrameSize();
+    DEBUG(std::cerr << "Added " << MFI->getMaxCallFrameSize() 
+          << " to the stack due to calls\n");
+  }
+
+  if (FP)
+    NumBytes += 8; //reserve space for the old FP
 
   // Do we need to allocate space on the stack?
   if (NumBytes == 0) return;
 
-  // Add the size of R30 to  NumBytes size for the store of R30 to the 
-  // stack
-//   std::cerr << "Spillsize of R30 is " << getSpillSize(Alpha::R30) << "\n";
-//   NumBytes = NumBytes + getSpillSize(Alpha::R30)/8;
-
   // Update frame info to pretend that this is part of the stack...
   MFI->setStackSize(NumBytes);
-  
+
   // adjust stack pointer: r30 -= numbytes
-  
-  if (NumBytes <= 32000) //FIXME: do this better 
-    {
-      MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(-NumBytes).addReg(Alpha::R30);
-      MBB.insert(MBBI, MI);
-    } else {
-      std::cerr << "Too big a stack frame\n";
-      abort();
-    }
+  NumBytes = -NumBytes;
+  if (NumBytes >= IMM_LOW) {
+    MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
+    MBB.insert(MBBI, MI);
+  } else if (getUpper16(NumBytes) >= IMM_LOW) {
+    MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
+    MBB.insert(MBBI, MI);
+    MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(getLower16(NumBytes)).addReg(Alpha::R30);
+    MBB.insert(MBBI, MI);
+  } else {
+    std::cerr << "Too big a stack frame at " << NumBytes << "\n";
+    abort();
+  }
+
+  //now if we need to, save the old FP and set the new
+  if (FP)
+  {
+    MI=BuildMI(Alpha::STQ, 3).addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
+    MBB.insert(MBBI, MI);
+    //this must be the last instr in the prolog
+    MI=BuildMI(Alpha::BIS, 2, Alpha::R15).addReg(Alpha::R30).addReg(Alpha::R30);
+    MBB.insert(MBBI, MI);
+  }
+
 }
 
 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
@@ -223,19 +271,36 @@ void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
   assert((MBBI->getOpcode() == Alpha::RET || MBBI->getOpcode() == Alpha::RETURN) &&
         "Can only insert epilog into returning blocks");
   
+  bool FP = hasFP(MF);
   // Get the number of bytes allocated from the FrameInfo...
-  unsigned NumBytes = MFI->getStackSize();
+  long NumBytes = MFI->getStackSize();
+
+  //now if we need to, restore the old FP
+  if (FP)
+  {
+    //copy the FP into the SP (discards allocas)
+    MI=BuildMI(Alpha::BIS, 2, Alpha::R30).addReg(Alpha::R15).addReg(Alpha::R15);
+    MBB.insert(MBBI, MI);
+    //restore the FP
+    MI=BuildMI(Alpha::LDQ, 2, Alpha::R15).addImm(0).addReg(Alpha::R15);
+    MBB.insert(MBBI, MI);
+  }
 
    if (NumBytes != 0) 
      {
-       if (NumBytes <= 32000) //FIXME: do this better 
-        {
-          MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
-          MBB.insert(MBBI, MI);
-        } else {
-          std::cerr << "Too big a stack frame\n";
-          abort();
-        }
+       if (NumBytes <= IMM_HIGH) {
+         MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
+         MBB.insert(MBBI, MI);
+       } else if (getUpper16(NumBytes) <= IMM_HIGH) {
+         MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
+         MBB.insert(MBBI, MI);
+         MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(getLower16(NumBytes)).addReg(Alpha::R30);
+         MBB.insert(MBBI, MI);
+       } else {
+         std::cerr << "Too big a stack frame at " << NumBytes << "\n";
+         abort();
+       }
      }
 }