Mips16RegisterInfo.cpp revision 71eab96bfd4d57a14105324cc0e0cac8eb3f7c8e
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  : MipsRegisterInfo(ST) {}
43
44// This function eliminate ADJCALLSTACKDOWN,
45// ADJCALLSTACKUP pseudo instructions
46void Mips16RegisterInfo::
47eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
48                              MachineBasicBlock::iterator I) const {
49  // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
50  MBB.erase(I);
51}
52
53void Mips16RegisterInfo::eliminateFI(MachineBasicBlock::iterator II,
54                                     unsigned OpNo, int FrameIndex,
55                                     uint64_t StackSize,
56                                     int64_t SPOffset) const {
57      MachineInstr &MI = *II;
58      MachineFunction &MF = *MI.getParent()->getParent();
59      MachineFrameInfo *MFI = MF.getFrameInfo();
60      MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
61
62      const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
63      int MinCSFI = 0;
64      int MaxCSFI = -1;
65
66      if (CSI.size()) {
67        MinCSFI = CSI[0].getFrameIdx();
68        MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
69      }
70
71      // The following stack frame objects are always
72      // referenced relative to $sp:
73      //  1. Outgoing arguments.
74      //  2. Pointer to dynamically allocated stack space.
75      //  3. Locations for callee-saved registers.
76      // Everything else is referenced relative to whatever register
77      // getFrameRegister() returns.
78      unsigned FrameReg;
79
80      if (MipsFI->isOutArgFI(FrameIndex) ||
81         (FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI))
82        FrameReg = Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP;
83      else
84        FrameReg = getFrameRegister(MF);
85
86      // Calculate final offset.
87      // - There is no need to change the offset if the frame object
88      //   is one of the
89      //   following: an outgoing argument, pointer to a dynamically allocated
90      //   stack space or a $gp restore location,
91      // - If the frame object is any of the following,
92      //   its offset must be adjusted
93      //   by adding the size of the stack:
94      //   incoming argument, callee-saved register location or local variable.
95      int64_t Offset;
96
97      if (MipsFI->isOutArgFI(FrameIndex))
98        Offset = SPOffset;
99      else
100        Offset = SPOffset + (int64_t)StackSize;
101
102      Offset    += MI.getOperand(OpNo + 1).getImm();
103
104      DEBUG(errs() << "Offset     : " << Offset << "\n" << "<--------->\n");
105
106      MI.getOperand(OpNo).ChangeToRegister(FrameReg, false);
107      MI.getOperand(OpNo + 1).ChangeToImmediate(Offset);
108
109
110}
111