1 //===-- MipsSERegisterInfo.cpp - MIPS32/64 Register Information -== -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the MIPS32/64 implementation of the TargetRegisterInfo
13 //===----------------------------------------------------------------------===//
15 #include "MipsSERegisterInfo.h"
17 #include "MipsAnalyzeImmediate.h"
18 #include "MipsMachineFunction.h"
19 #include "MipsSEInstrInfo.h"
20 #include "MipsSubtarget.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/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/ValueTypes.h"
28 #include "llvm/Constants.h"
29 #include "llvm/DebugInfo.h"
30 #include "llvm/Function.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetFrameLowering.h"
36 #include "llvm/Target/TargetInstrInfo.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/Type.h"
43 MipsSERegisterInfo::MipsSERegisterInfo(const MipsSubtarget &ST,
44 const MipsSEInstrInfo &I)
45 : MipsRegisterInfo(ST), TII(I) {}
47 bool MipsSERegisterInfo::
48 requiresRegisterScavenging(const MachineFunction &MF) const {
52 bool MipsSERegisterInfo::
53 requiresFrameIndexScavenging(const MachineFunction &MF) const {
57 // This function eliminate ADJCALLSTACKDOWN,
58 // ADJCALLSTACKUP pseudo instructions
59 void MipsSERegisterInfo::
60 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
61 MachineBasicBlock::iterator I) const {
62 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
64 if (!TFI->hasReservedCallFrame(MF)) {
65 int64_t Amount = I->getOperand(0).getImm();
67 if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
70 const MipsSEInstrInfo *II = static_cast<const MipsSEInstrInfo*>(&TII);
71 unsigned SP = Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP;
73 II->adjustStackPtr(SP, Amount, MBB, I);
79 void MipsSERegisterInfo::eliminateFI(MachineBasicBlock::iterator II,
80 unsigned OpNo, int FrameIndex,
82 int64_t SPOffset) const {
83 MachineInstr &MI = *II;
84 MachineFunction &MF = *MI.getParent()->getParent();
85 MachineFrameInfo *MFI = MF.getFrameInfo();
87 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
92 MinCSFI = CSI[0].getFrameIdx();
93 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
96 // The following stack frame objects are always referenced relative to $sp:
97 // 1. Outgoing arguments.
98 // 2. Pointer to dynamically allocated stack space.
99 // 3. Locations for callee-saved registers.
100 // Everything else is referenced relative to whatever register
101 // getFrameRegister() returns.
104 if (FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI)
105 FrameReg = Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP;
107 FrameReg = getFrameRegister(MF);
109 // Calculate final offset.
110 // - There is no need to change the offset if the frame object is one of the
111 // following: an outgoing argument, pointer to a dynamically allocated
112 // stack space or a $gp restore location,
113 // - If the frame object is any of the following, its offset must be adjusted
114 // by adding the size of the stack:
115 // incoming argument, callee-saved register location or local variable.
119 Offset = SPOffset + (int64_t)StackSize;
120 Offset += MI.getOperand(OpNo + 1).getImm();
122 DEBUG(errs() << "Offset : " << Offset << "\n" << "<--------->\n");
124 // If MI is not a debug value, make sure Offset fits in the 16-bit immediate
126 if (!MI.isDebugValue() && !isInt<16>(Offset)) {
127 MachineBasicBlock &MBB = *MI.getParent();
128 DebugLoc DL = II->getDebugLoc();
129 unsigned ADDu = Subtarget.isABI_N64() ? Mips::DADDu : Mips::ADDu;
132 unsigned Reg = TII.loadImmediate(Offset, MBB, II, DL, &NewImm);
133 BuildMI(MBB, II, DL, TII.get(ADDu), Reg).addReg(FrameReg)
134 .addReg(Reg, RegState::Kill);
137 Offset = SignExtend64<16>(NewImm);
141 MI.getOperand(OpNo).ChangeToRegister(FrameReg, false, false, IsKill);
142 MI.getOperand(OpNo + 1).ChangeToImmediate(Offset);