LLVM API Documentation

MSP430RegisterInfo.cpp
Go to the documentation of this file.
00001 //===-- MSP430RegisterInfo.cpp - MSP430 Register Information --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the MSP430 implementation of the TargetRegisterInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "msp430-reg-info"
00015 
00016 #include "MSP430RegisterInfo.h"
00017 #include "MSP430.h"
00018 #include "MSP430MachineFunctionInfo.h"
00019 #include "MSP430TargetMachine.h"
00020 #include "llvm/ADT/BitVector.h"
00021 #include "llvm/CodeGen/MachineFrameInfo.h"
00022 #include "llvm/CodeGen/MachineFunction.h"
00023 #include "llvm/CodeGen/MachineInstrBuilder.h"
00024 #include "llvm/IR/Function.h"
00025 #include "llvm/Support/ErrorHandling.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include "llvm/Target/TargetOptions.h"
00028 
00029 #define GET_REGINFO_TARGET_DESC
00030 #include "MSP430GenRegisterInfo.inc"
00031 
00032 using namespace llvm;
00033 
00034 // FIXME: Provide proper call frame setup / destroy opcodes.
00035 MSP430RegisterInfo::MSP430RegisterInfo(MSP430TargetMachine &tm,
00036                                        const TargetInstrInfo &tii)
00037   : MSP430GenRegisterInfo(MSP430::PCW), TM(tm), TII(tii) {
00038   StackAlign = TM.getFrameLowering()->getStackAlignment();
00039 }
00040 
00041 const uint16_t*
00042 MSP430RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
00043   const TargetFrameLowering *TFI = MF->getTarget().getFrameLowering();
00044   const Function* F = MF->getFunction();
00045   static const uint16_t CalleeSavedRegs[] = {
00046     MSP430::FPW, MSP430::R5W, MSP430::R6W, MSP430::R7W,
00047     MSP430::R8W, MSP430::R9W, MSP430::R10W, MSP430::R11W,
00048     0
00049   };
00050   static const uint16_t CalleeSavedRegsFP[] = {
00051     MSP430::R5W, MSP430::R6W, MSP430::R7W,
00052     MSP430::R8W, MSP430::R9W, MSP430::R10W, MSP430::R11W,
00053     0
00054   };
00055   static const uint16_t CalleeSavedRegsIntr[] = {
00056     MSP430::FPW,  MSP430::R5W,  MSP430::R6W,  MSP430::R7W,
00057     MSP430::R8W,  MSP430::R9W,  MSP430::R10W, MSP430::R11W,
00058     MSP430::R12W, MSP430::R13W, MSP430::R14W, MSP430::R15W,
00059     0
00060   };
00061   static const uint16_t CalleeSavedRegsIntrFP[] = {
00062     MSP430::R5W,  MSP430::R6W,  MSP430::R7W,
00063     MSP430::R8W,  MSP430::R9W,  MSP430::R10W, MSP430::R11W,
00064     MSP430::R12W, MSP430::R13W, MSP430::R14W, MSP430::R15W,
00065     0
00066   };
00067 
00068   if (TFI->hasFP(*MF))
00069     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
00070             CalleeSavedRegsIntrFP : CalleeSavedRegsFP);
00071   else
00072     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
00073             CalleeSavedRegsIntr : CalleeSavedRegs);
00074 
00075 }
00076 
00077 BitVector MSP430RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
00078   BitVector Reserved(getNumRegs());
00079   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
00080 
00081   // Mark 4 special registers with subregisters as reserved.
00082   Reserved.set(MSP430::PCB);
00083   Reserved.set(MSP430::SPB);
00084   Reserved.set(MSP430::SRB);
00085   Reserved.set(MSP430::CGB);
00086   Reserved.set(MSP430::PCW);
00087   Reserved.set(MSP430::SPW);
00088   Reserved.set(MSP430::SRW);
00089   Reserved.set(MSP430::CGW);
00090 
00091   // Mark frame pointer as reserved if needed.
00092   if (TFI->hasFP(MF))
00093     Reserved.set(MSP430::FPW);
00094 
00095   return Reserved;
00096 }
00097 
00098 const TargetRegisterClass *
00099 MSP430RegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
00100                                                                          const {
00101   return &MSP430::GR16RegClass;
00102 }
00103 
00104 void
00105 MSP430RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
00106                                         int SPAdj, unsigned FIOperandNum,
00107                                         RegScavenger *RS) const {
00108   assert(SPAdj == 0 && "Unexpected");
00109 
00110   MachineInstr &MI = *II;
00111   MachineBasicBlock &MBB = *MI.getParent();
00112   MachineFunction &MF = *MBB.getParent();
00113   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
00114   DebugLoc dl = MI.getDebugLoc();
00115   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
00116 
00117   unsigned BasePtr = (TFI->hasFP(MF) ? MSP430::FPW : MSP430::SPW);
00118   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
00119 
00120   // Skip the saved PC
00121   Offset += 2;
00122 
00123   if (!TFI->hasFP(MF))
00124     Offset += MF.getFrameInfo()->getStackSize();
00125   else
00126     Offset += 2; // Skip the saved FPW
00127 
00128   // Fold imm into offset
00129   Offset += MI.getOperand(FIOperandNum + 1).getImm();
00130 
00131   if (MI.getOpcode() == MSP430::ADD16ri) {
00132     // This is actually "load effective address" of the stack slot
00133     // instruction. We have only two-address instructions, thus we need to
00134     // expand it into mov + add
00135 
00136     MI.setDesc(TII.get(MSP430::MOV16rr));
00137     MI.getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
00138 
00139     if (Offset == 0)
00140       return;
00141 
00142     // We need to materialize the offset via add instruction.
00143     unsigned DstReg = MI.getOperand(0).getReg();
00144     if (Offset < 0)
00145       BuildMI(MBB, llvm::next(II), dl, TII.get(MSP430::SUB16ri), DstReg)
00146         .addReg(DstReg).addImm(-Offset);
00147     else
00148       BuildMI(MBB, llvm::next(II), dl, TII.get(MSP430::ADD16ri), DstReg)
00149         .addReg(DstReg).addImm(Offset);
00150 
00151     return;
00152   }
00153 
00154   MI.getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
00155   MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
00156 }
00157 
00158 unsigned MSP430RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
00159   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
00160 
00161   return TFI->hasFP(MF) ? MSP430::FPW : MSP430::SPW;
00162 }