Mips16RegisterInfo.cpp revision f99998a2b0a6c186b3a1b6ad7bfa488009a0c5f5
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
61  const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
62  int MinCSFI = 0;
63  int MaxCSFI = -1;
64
65  if (CSI.size()) {
66    MinCSFI = CSI[0].getFrameIdx();
67    MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
68  }
69
70  // The following stack frame objects are always
71  // referenced relative to $sp:
72  //  1. Outgoing arguments.
73  //  2. Pointer to dynamically allocated stack space.
74  //  3. Locations for callee-saved registers.
75  // Everything else is referenced relative to whatever register
76  // getFrameRegister() returns.
77  unsigned FrameReg;
78
79  if (FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI)
80    FrameReg = Mips::SP;
81  else {
82    const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
83    if (TFI->hasFP(MF)) {
84      FrameReg = Mips::S0;
85    }
86    else {
87      if ((MI.getNumOperands()> OpNo+2) && MI.getOperand(OpNo+2).isReg())
88        FrameReg = MI.getOperand(OpNo+2).getReg();
89      else
90        FrameReg = Mips::SP;
91    }
92  }
93  // Calculate final offset.
94  // - There is no need to change the offset if the frame object
95  //   is one of the
96  //   following: an outgoing argument, pointer to a dynamically allocated
97  //   stack space or a $gp restore location,
98  // - If the frame object is any of the following,
99  //   its offset must be adjusted
100  //   by adding the size of the stack:
101  //   incoming argument, callee-saved register location or local variable.
102  int64_t Offset;
103  Offset = SPOffset + (int64_t)StackSize;
104  Offset += MI.getOperand(OpNo + 1).getImm();
105
106
107  DEBUG(errs() << "Offset     : " << Offset << "\n" << "<--------->\n");
108
109  MI.getOperand(OpNo).ChangeToRegister(FrameReg, false);
110  MI.getOperand(OpNo + 1).ChangeToImmediate(Offset);
111
112
113}
114