Path: blob/main/contrib/llvm-project/llvm/lib/Target/MSP430/MSP430RegisterInfo.cpp
96353 views
//===-- MSP430RegisterInfo.cpp - MSP430 Register Information --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains the MSP430 implementation of the TargetRegisterInfo class.9//10//===----------------------------------------------------------------------===//1112#include "MSP430RegisterInfo.h"13#include "MSP430.h"14#include "MSP430MachineFunctionInfo.h"15#include "MSP430TargetMachine.h"16#include "llvm/ADT/BitVector.h"17#include "llvm/CodeGen/MachineFrameInfo.h"18#include "llvm/CodeGen/MachineFunction.h"19#include "llvm/CodeGen/MachineInstrBuilder.h"20#include "llvm/IR/Function.h"21#include "llvm/Support/ErrorHandling.h"22#include "llvm/Target/TargetMachine.h"23#include "llvm/Target/TargetOptions.h"2425using namespace llvm;2627#define DEBUG_TYPE "msp430-reg-info"2829#define GET_REGINFO_TARGET_DESC30#include "MSP430GenRegisterInfo.inc"3132// FIXME: Provide proper call frame setup / destroy opcodes.33MSP430RegisterInfo::MSP430RegisterInfo()34: MSP430GenRegisterInfo(MSP430::PC) {}3536const MCPhysReg*37MSP430RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {38const MSP430FrameLowering *TFI = getFrameLowering(*MF);39const Function* F = &MF->getFunction();40static const MCPhysReg CalleeSavedRegs[] = {41MSP430::R4, MSP430::R5, MSP430::R6, MSP430::R7,42MSP430::R8, MSP430::R9, MSP430::R10,43044};45static const MCPhysReg CalleeSavedRegsFP[] = {46MSP430::R5, MSP430::R6, MSP430::R7,47MSP430::R8, MSP430::R9, MSP430::R10,48049};50static const MCPhysReg CalleeSavedRegsIntr[] = {51MSP430::R4, MSP430::R5, MSP430::R6, MSP430::R7,52MSP430::R8, MSP430::R9, MSP430::R10, MSP430::R11,53MSP430::R12, MSP430::R13, MSP430::R14, MSP430::R15,54055};56static const MCPhysReg CalleeSavedRegsIntrFP[] = {57MSP430::R5, MSP430::R6, MSP430::R7,58MSP430::R8, MSP430::R9, MSP430::R10, MSP430::R11,59MSP430::R12, MSP430::R13, MSP430::R14, MSP430::R15,60061};6263if (TFI->hasFP(*MF))64return (F->getCallingConv() == CallingConv::MSP430_INTR ?65CalleeSavedRegsIntrFP : CalleeSavedRegsFP);66else67return (F->getCallingConv() == CallingConv::MSP430_INTR ?68CalleeSavedRegsIntr : CalleeSavedRegs);6970}7172BitVector MSP430RegisterInfo::getReservedRegs(const MachineFunction &MF) const {73BitVector Reserved(getNumRegs());74const MSP430FrameLowering *TFI = getFrameLowering(MF);7576// Mark 4 special registers with subregisters as reserved.77Reserved.set(MSP430::PCB);78Reserved.set(MSP430::SPB);79Reserved.set(MSP430::SRB);80Reserved.set(MSP430::CGB);81Reserved.set(MSP430::PC);82Reserved.set(MSP430::SP);83Reserved.set(MSP430::SR);84Reserved.set(MSP430::CG);8586// Mark frame pointer as reserved if needed.87if (TFI->hasFP(MF)) {88Reserved.set(MSP430::R4B);89Reserved.set(MSP430::R4);90}9192return Reserved;93}9495const TargetRegisterClass *96MSP430RegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)97const {98return &MSP430::GR16RegClass;99}100101bool102MSP430RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,103int SPAdj, unsigned FIOperandNum,104RegScavenger *RS) const {105assert(SPAdj == 0 && "Unexpected");106107MachineInstr &MI = *II;108MachineBasicBlock &MBB = *MI.getParent();109MachineFunction &MF = *MBB.getParent();110const MSP430FrameLowering *TFI = getFrameLowering(MF);111DebugLoc dl = MI.getDebugLoc();112int FrameIndex = MI.getOperand(FIOperandNum).getIndex();113114unsigned BasePtr = (TFI->hasFP(MF) ? MSP430::R4 : MSP430::SP);115int Offset = MF.getFrameInfo().getObjectOffset(FrameIndex);116117// Skip the saved PC118Offset += 2;119120if (!TFI->hasFP(MF))121Offset += MF.getFrameInfo().getStackSize();122else123Offset += 2; // Skip the saved FP124125// Fold imm into offset126Offset += MI.getOperand(FIOperandNum + 1).getImm();127128if (MI.getOpcode() == MSP430::ADDframe) {129// This is actually "load effective address" of the stack slot130// instruction. We have only two-address instructions, thus we need to131// expand it into mov + add132const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();133134MI.setDesc(TII.get(MSP430::MOV16rr));135MI.getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);136137// Remove the now unused Offset operand.138MI.removeOperand(FIOperandNum + 1);139140if (Offset == 0)141return false;142143// We need to materialize the offset via add instruction.144Register DstReg = MI.getOperand(0).getReg();145if (Offset < 0)146BuildMI(MBB, std::next(II), dl, TII.get(MSP430::SUB16ri), DstReg)147.addReg(DstReg).addImm(-Offset);148else149BuildMI(MBB, std::next(II), dl, TII.get(MSP430::ADD16ri), DstReg)150.addReg(DstReg).addImm(Offset);151152return false;153}154155MI.getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);156MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);157return false;158}159160Register MSP430RegisterInfo::getFrameRegister(const MachineFunction &MF) const {161const MSP430FrameLowering *TFI = getFrameLowering(MF);162return TFI->hasFP(MF) ? MSP430::R4 : MSP430::SP;163}164165166