Mips16RegisterInfo.cpp revision 239310
1//===-- Mips16RegisterInfo.cpp - MIPS16 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 MIPS16 implementation of the TargetRegisterInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Mips16RegisterInfo.h"
15#include "Mips.h"
16#include "MipsAnalyzeImmediate.h"
17#include "MipsInstrInfo.h"
18#include "MipsSubtarget.h"
19#include "MipsMachineFunction.h"
20#include "llvm/Constants.h"
21#include "llvm/DebugInfo.h"
22#include "llvm/Type.h"
23#include "llvm/Function.h"
24#include "llvm/CodeGen/ValueTypes.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/Target/TargetFrameLowering.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetOptions.h"
31#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
36#include "llvm/ADT/BitVector.h"
37#include "llvm/ADT/STLExtras.h"
38
39using namespace llvm;
40
41Mips16RegisterInfo::Mips16RegisterInfo(const MipsSubtarget &ST,
42                                       const TargetInstrInfo &TII)
43  : MipsRegisterInfo(ST, TII) {}
44
45// This function eliminate ADJCALLSTACKDOWN,
46// ADJCALLSTACKUP pseudo instructions
47void Mips16RegisterInfo::
48eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
49                              MachineBasicBlock::iterator I) const {
50  // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
51  MBB.erase(I);
52}
53
54void Mips16RegisterInfo::eliminateFI(MachineBasicBlock::iterator II,
55                                     unsigned OpNo, int FrameIndex,
56                                     uint64_t StackSize,
57                                     int64_t SPOffset) const {
58      MachineInstr &MI = *II;
59      MachineFunction &MF = *MI.getParent()->getParent();
60      MachineFrameInfo *MFI = MF.getFrameInfo();
61      MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
62
63      const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
64      int MinCSFI = 0;
65      int MaxCSFI = -1;
66
67      if (CSI.size()) {
68        MinCSFI = CSI[0].getFrameIdx();
69        MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
70      }
71
72      // The following stack frame objects are always
73      // referenced relative to $sp:
74      //  1. Outgoing arguments.
75      //  2. Pointer to dynamically allocated stack space.
76      //  3. Locations for callee-saved registers.
77      // Everything else is referenced relative to whatever register
78      // getFrameRegister() returns.
79      unsigned FrameReg;
80
81      if (MipsFI->isOutArgFI(FrameIndex) ||
82         (FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI))
83        FrameReg = Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP;
84      else
85        FrameReg = getFrameRegister(MF);
86
87      // Calculate final offset.
88      // - There is no need to change the offset if the frame object
89      //   is one of the
90      //   following: an outgoing argument, pointer to a dynamically allocated
91      //   stack space or a $gp restore location,
92      // - If the frame object is any of the following,
93      //   its offset must be adjusted
94      //   by adding the size of the stack:
95      //   incoming argument, callee-saved register location or local variable.
96      int64_t Offset;
97
98      if (MipsFI->isOutArgFI(FrameIndex))
99        Offset = SPOffset;
100      else
101        Offset = SPOffset + (int64_t)StackSize;
102
103      Offset    += MI.getOperand(OpNo + 1).getImm();
104
105      DEBUG(errs() << "Offset     : " << Offset << "\n" << "<--------->\n");
106
107      MI.getOperand(OpNo).ChangeToRegister(FrameReg, false);
108      MI.getOperand(OpNo + 1).ChangeToImmediate(Offset);
109
110
111}
112