1//===-- MBlazeRegisterInfo.cpp - MBlaze Register Information --------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the MBlaze implementation of the TargetRegisterInfo
11// class.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "mblaze-frame-info"
16
17#include "MBlazeRegisterInfo.h"
18#include "MBlaze.h"
19#include "MBlazeMachineFunction.h"
20#include "MBlazeSubtarget.h"
21#include "llvm/ADT/BitVector.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/ValueTypes.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/Function.h"
29#include "llvm/IR/Type.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
34#include "llvm/Target/TargetFrameLowering.h"
35#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
38
39#define GET_REGINFO_TARGET_DESC
40#include "MBlazeGenRegisterInfo.inc"
41
42using namespace llvm;
43
44MBlazeRegisterInfo::
45MBlazeRegisterInfo(const MBlazeSubtarget &ST, const TargetInstrInfo &tii)
46  : MBlazeGenRegisterInfo(MBlaze::R15), Subtarget(ST), TII(tii) {}
47
48unsigned MBlazeRegisterInfo::getPICCallReg() {
49  return MBlaze::R20;
50}
51
52//===----------------------------------------------------------------------===//
53// Callee Saved Registers methods
54//===----------------------------------------------------------------------===//
55
56/// MBlaze Callee Saved Registers
57const uint16_t* MBlazeRegisterInfo::
58getCalleeSavedRegs(const MachineFunction *MF) const {
59  // MBlaze callee-save register range is R20 - R31
60  static const uint16_t CalleeSavedRegs[] = {
61    MBlaze::R20, MBlaze::R21, MBlaze::R22, MBlaze::R23,
62    MBlaze::R24, MBlaze::R25, MBlaze::R26, MBlaze::R27,
63    MBlaze::R28, MBlaze::R29, MBlaze::R30, MBlaze::R31,
64    0
65  };
66
67  return CalleeSavedRegs;
68}
69
70BitVector MBlazeRegisterInfo::
71getReservedRegs(const MachineFunction &MF) const {
72  BitVector Reserved(getNumRegs());
73  Reserved.set(MBlaze::R0);
74  Reserved.set(MBlaze::R1);
75  Reserved.set(MBlaze::R2);
76  Reserved.set(MBlaze::R13);
77  Reserved.set(MBlaze::R14);
78  Reserved.set(MBlaze::R15);
79  Reserved.set(MBlaze::R16);
80  Reserved.set(MBlaze::R17);
81  Reserved.set(MBlaze::R18);
82  Reserved.set(MBlaze::R19);
83  return Reserved;
84}
85
86// FrameIndex represent objects inside a abstract stack.
87// We must replace FrameIndex with an stack/frame pointer
88// direct reference.
89void MBlazeRegisterInfo::
90eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
91                    unsigned FIOperandNum, RegScavenger *RS) const {
92  MachineInstr &MI = *II;
93  MachineFunction &MF = *MI.getParent()->getParent();
94  MachineFrameInfo *MFI = MF.getFrameInfo();
95  unsigned OFIOperandNum = FIOperandNum == 2 ? 1 : 2;
96
97  DEBUG(dbgs() << "\nFunction : " << MF.getName() << "\n";
98        dbgs() << "<--------->\n" << MI);
99
100  int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
101  int stackSize  = MFI->getStackSize();
102  int spOffset   = MFI->getObjectOffset(FrameIndex);
103
104  DEBUG(MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
105        dbgs() << "FrameIndex : " << FrameIndex << "\n"
106               << "spOffset   : " << spOffset << "\n"
107               << "stackSize  : " << stackSize << "\n"
108               << "isFixed    : " << MFI->isFixedObjectIndex(FrameIndex) << "\n"
109               << "isLiveIn   : " << MBlazeFI->isLiveIn(FrameIndex) << "\n"
110               << "isSpill    : " << MFI->isSpillSlotObjectIndex(FrameIndex)
111               << "\n" );
112
113  // as explained on LowerFormalArguments, detect negative offsets
114  // and adjust SPOffsets considering the final stack size.
115  int Offset = (spOffset < 0) ? (stackSize - spOffset) : spOffset;
116  Offset += MI.getOperand(OFIOperandNum).getImm();
117
118  DEBUG(dbgs() << "Offset     : " << Offset << "\n" << "<--------->\n");
119
120  MI.getOperand(OFIOperandNum).ChangeToImmediate(Offset);
121  MI.getOperand(FIOperandNum).ChangeToRegister(getFrameRegister(MF), false);
122}
123
124void MBlazeRegisterInfo::
125processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *) const {
126  // Set the stack offset where GP must be saved/loaded from.
127  MachineFrameInfo *MFI = MF.getFrameInfo();
128  MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
129  if (MBlazeFI->needGPSaveRestore())
130    MFI->setObjectOffset(MBlazeFI->getGPFI(), MBlazeFI->getGPStackOffset());
131}
132
133unsigned MBlazeRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
134  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
135
136  return TFI->hasFP(MF) ? MBlaze::R19 : MBlaze::R1;
137}
138
139unsigned MBlazeRegisterInfo::getEHExceptionRegister() const {
140  llvm_unreachable("What is the exception register");
141}
142
143unsigned MBlazeRegisterInfo::getEHHandlerRegister() const {
144  llvm_unreachable("What is the exception handler register");
145}
146