From aa29915b5822bd9af2a2b80eb95be59368743935 Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Sun, 3 May 2009 13:09:57 +0000 Subject: [PATCH] First draft of stack slot loads / stores lowering git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70735 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/MSP430/MSP430InstrInfo.cpp | 40 +++++++++++++++++++++-- lib/Target/MSP430/MSP430InstrInfo.h | 9 +++++ lib/Target/MSP430/MSP430RegisterInfo.cpp | 32 +++++++++++++++++- lib/Target/MSP430/MSP430RegisterInfo.h | 1 + lib/Target/MSP430/MSP430TargetMachine.cpp | 2 +- 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/lib/Target/MSP430/MSP430InstrInfo.cpp b/lib/Target/MSP430/MSP430InstrInfo.cpp index dbbf29cdeea..c32dbdca2e2 100644 --- a/lib/Target/MSP430/MSP430InstrInfo.cpp +++ b/lib/Target/MSP430/MSP430InstrInfo.cpp @@ -16,10 +16,10 @@ #include "MSP430TargetMachine.h" #include "MSP430GenInstrInfo.inc" #include "llvm/Function.h" -#include "llvm/CodeGen/MachineFunction.h" +#include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/CodeGen/MachineRegisterInfo.h" - +#include "llvm/CodeGen/PseudoSourceValue.h" using namespace llvm; @@ -27,6 +27,42 @@ MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm) : TargetInstrInfoImpl(MSP430Insts, array_lengthof(MSP430Insts)), RI(tm, *this), TM(tm) {} +void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MI, + unsigned SrcReg, bool isKill, int FrameIdx, + const TargetRegisterClass *RC) const { + DebugLoc DL = DebugLoc::getUnknownLoc(); + if (MI != MBB.end()) DL = MI->getDebugLoc(); + + if (RC == &MSP430::GR16RegClass) + BuildMI(MBB, MI, DL, get(MSP430::MOV16mr)) + .addFrameIndex(FrameIdx).addImm(0) + .addReg(SrcReg, false, false, isKill); + else if (RC == &MSP430::GR8RegClass) + BuildMI(MBB, MI, DL, get(MSP430::MOV8mr)) + .addFrameIndex(FrameIdx).addImm(0) + .addReg(SrcReg, false, false, isKill); + else + assert(0 && "Cannot store this register to stack slot!"); +} + +void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MI, + unsigned DestReg, int FrameIdx, + const TargetRegisterClass *RC) const{ + DebugLoc DL = DebugLoc::getUnknownLoc(); + if (MI != MBB.end()) DL = MI->getDebugLoc(); + + if (RC == &MSP430::GR16RegClass) + BuildMI(MBB, MI, DL, get(MSP430::MOV16rm)) + .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0); + else if (RC == &MSP430::GR8RegClass) + BuildMI(MBB, MI, DL, get(MSP430::MOV8rm)) + .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0); + else + assert(0 && "Cannot store this register to stack slot!"); +} + bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg, unsigned SrcReg, diff --git a/lib/Target/MSP430/MSP430InstrInfo.h b/lib/Target/MSP430/MSP430InstrInfo.h index 0ed2944f4c2..656e8dfb9da 100644 --- a/lib/Target/MSP430/MSP430InstrInfo.h +++ b/lib/Target/MSP430/MSP430InstrInfo.h @@ -41,6 +41,15 @@ public: bool isMoveInstr(const MachineInstr& MI, unsigned &SrcReg, unsigned &DstReg, unsigned &SrcSubIdx, unsigned &DstSubIdx) const; + + virtual void storeRegToStackSlot(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MI, + unsigned SrcReg, bool isKill, int FrameIndex, + const TargetRegisterClass *RC) const; + virtual void loadRegFromStackSlot(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MI, + unsigned DestReg, int FrameIdx, + const TargetRegisterClass *RC) const; }; } diff --git a/lib/Target/MSP430/MSP430RegisterInfo.cpp b/lib/Target/MSP430/MSP430RegisterInfo.cpp index c7efa5e351b..be187fa8e66 100644 --- a/lib/Target/MSP430/MSP430RegisterInfo.cpp +++ b/lib/Target/MSP430/MSP430RegisterInfo.cpp @@ -74,6 +74,11 @@ MSP430RegisterInfo::getReservedRegs(const MachineFunction &MF) const { return Reserved; } +const TargetRegisterClass* MSP430RegisterInfo::getPointerRegClass() const { + return &MSP430::GR16RegClass; +} + + bool MSP430RegisterInfo::hasFP(const MachineFunction &MF) const { return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects(); } @@ -143,7 +148,32 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, void MSP430RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, RegScavenger *RS) const { - assert(0 && "Not implemented yet!"); + assert(SPAdj == 0 && "Unexpected"); + + unsigned i = 0; + MachineInstr &MI = *II; + MachineFunction &MF = *MI.getParent()->getParent(); + while (!MI.getOperand(i).isFI()) { + ++i; + assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); + } + + int FrameIndex = MI.getOperand(i).getIndex(); + + unsigned BasePtr = (hasFP(MF) ? MSP430::FPW : MSP430::SPW); + int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex); + + if (!hasFP(MF)) + Offset += MF.getFrameInfo()->getStackSize(); + + // Skip the saved PC + Offset += 2; + + MI.getOperand(i).ChangeToRegister(BasePtr, false); + + // Fold imm into offset + Offset += MI.getOperand(i+1).getImm(); + MI.getOperand(i+1).ChangeToImmediate(Offset); } void MSP430RegisterInfo::emitPrologue(MachineFunction &MF) const { diff --git a/lib/Target/MSP430/MSP430RegisterInfo.h b/lib/Target/MSP430/MSP430RegisterInfo.h index 0dc061c6742..6038af3d08d 100644 --- a/lib/Target/MSP430/MSP430RegisterInfo.h +++ b/lib/Target/MSP430/MSP430RegisterInfo.h @@ -40,6 +40,7 @@ public: getCalleeSavedRegClasses(const MachineFunction *MF = 0) const; BitVector getReservedRegs(const MachineFunction &MF) const; + const TargetRegisterClass* getPointerRegClass() const; bool hasFP(const MachineFunction &MF) const; bool hasReservedCallFrame(MachineFunction &MF) const; diff --git a/lib/Target/MSP430/MSP430TargetMachine.cpp b/lib/Target/MSP430/MSP430TargetMachine.cpp index a2dee33c6c9..90819cff999 100644 --- a/lib/Target/MSP430/MSP430TargetMachine.cpp +++ b/lib/Target/MSP430/MSP430TargetMachine.cpp @@ -41,7 +41,7 @@ MSP430TargetMachine::MSP430TargetMachine(const Module &M, // FIXME: Check TargetData string. DataLayout("e-p:16:8:8-i8:8:8-i16:8:8-i32:8:8"), InstrInfo(*this), TLInfo(*this), - FrameInfo(TargetFrameInfo::StackGrowsUp, 8, 0) { } + FrameInfo(TargetFrameInfo::StackGrowsDown, 8, 0) { } const TargetAsmInfo *MSP430TargetMachine::createTargetAsmInfo() const { return new MSP430TargetAsmInfo(*this);