1 //===-- Mips16RegisterInfo.cpp - MIPS16 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 MIPS16 implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "Mips16RegisterInfo.h"
16 #include "Mips16InstrInfo.h"
17 #include "MipsInstrInfo.h"
18 #include "MipsMachineFunction.h"
19 #include "MipsSubtarget.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/TargetFrameLowering.h"
26 #include "llvm/CodeGen/TargetInstrInfo.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Target/TargetOptions.h"
39 #define DEBUG_TYPE "mips16-registerinfo"
41 Mips16RegisterInfo::Mips16RegisterInfo() : MipsRegisterInfo() {}
43 bool Mips16RegisterInfo::requiresRegisterScavenging
44 (const MachineFunction
&MF
) const {
47 bool Mips16RegisterInfo::requiresFrameIndexScavenging
48 (const MachineFunction
&MF
) const {
52 bool Mips16RegisterInfo::useFPForScavengingIndex
53 (const MachineFunction
&MF
) const {
57 bool Mips16RegisterInfo::saveScavengerRegister
58 (MachineBasicBlock
&MBB
,
59 MachineBasicBlock::iterator I
,
60 MachineBasicBlock::iterator
&UseMI
,
61 const TargetRegisterClass
*RC
,
64 const TargetInstrInfo
&TII
= *MBB
.getParent()->getSubtarget().getInstrInfo();
65 TII
.copyPhysReg(MBB
, I
, DL
, Mips::T0
, Reg
, true);
66 TII
.copyPhysReg(MBB
, UseMI
, DL
, Reg
, Mips::T0
, true);
70 const TargetRegisterClass
*
71 Mips16RegisterInfo::intRegClass(unsigned Size
) const {
73 return &Mips::CPU16RegsRegClass
;
76 void Mips16RegisterInfo::eliminateFI(MachineBasicBlock::iterator II
,
77 unsigned OpNo
, int FrameIndex
,
79 int64_t SPOffset
) const {
80 MachineInstr
&MI
= *II
;
81 MachineFunction
&MF
= *MI
.getParent()->getParent();
82 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
84 const std::vector
<CalleeSavedInfo
> &CSI
= MFI
.getCalleeSavedInfo();
89 MinCSFI
= CSI
[0].getFrameIdx();
90 MaxCSFI
= CSI
[CSI
.size() - 1].getFrameIdx();
93 // The following stack frame objects are always
94 // referenced relative to $sp:
95 // 1. Outgoing arguments.
96 // 2. Pointer to dynamically allocated stack space.
97 // 3. Locations for callee-saved registers.
98 // Everything else is referenced relative to whatever register
99 // getFrameRegister() returns.
102 if (FrameIndex
>= MinCSFI
&& FrameIndex
<= MaxCSFI
)
105 const TargetFrameLowering
*TFI
= MF
.getSubtarget().getFrameLowering();
106 if (TFI
->hasFP(MF
)) {
110 if ((MI
.getNumOperands()> OpNo
+2) && MI
.getOperand(OpNo
+2).isReg())
111 FrameReg
= MI
.getOperand(OpNo
+2).getReg();
116 // Calculate final offset.
117 // - There is no need to change the offset if the frame object
119 // following: an outgoing argument, pointer to a dynamically allocated
120 // stack space or a $gp restore location,
121 // - If the frame object is any of the following,
122 // its offset must be adjusted
123 // by adding the size of the stack:
124 // incoming argument, callee-saved register location or local variable.
127 Offset
= SPOffset
+ (int64_t)StackSize
;
128 Offset
+= MI
.getOperand(OpNo
+ 1).getImm();
130 LLVM_DEBUG(errs() << "Offset : " << Offset
<< "\n"
133 if (!MI
.isDebugValue() &&
134 !Mips16InstrInfo::validImmediate(MI
.getOpcode(), FrameReg
, Offset
)) {
135 MachineBasicBlock
&MBB
= *MI
.getParent();
136 DebugLoc DL
= II
->getDebugLoc();
138 const Mips16InstrInfo
&TII
=
139 *static_cast<const Mips16InstrInfo
*>(MF
.getSubtarget().getInstrInfo());
140 FrameReg
= TII
.loadImmediate(FrameReg
, Offset
, MBB
, II
, DL
, NewImm
);
141 Offset
= SignExtend64
<16>(NewImm
);
144 MI
.getOperand(OpNo
).ChangeToRegister(FrameReg
, false, false, IsKill
);
145 MI
.getOperand(OpNo
+ 1).ChangeToImmediate(Offset
);