1 //===- XCoreRegisterInfo.cpp - XCore Register Information -------*- C++ -*-===//
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 XCore implementation of the MRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "XCoreRegisterInfo.h"
15 #include "XCoreMachineFunctionInfo.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineLocation.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/RegisterScavenging.h"
24 #include "llvm/Target/TargetFrameLowering.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Type.h"
29 #include "llvm/Function.h"
30 #include "llvm/ADT/BitVector.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
38 XCoreRegisterInfo::XCoreRegisterInfo(const TargetInstrInfo
&tii
)
39 : XCoreGenRegisterInfo(XCore::ADJCALLSTACKDOWN
, XCore::ADJCALLSTACKUP
),
44 static inline bool isImmUs(unsigned val
) {
48 static inline bool isImmU6(unsigned val
) {
49 return val
< (1 << 6);
52 static inline bool isImmU16(unsigned val
) {
53 return val
< (1 << 16);
56 static const unsigned XCore_ArgRegs
[] = {
57 XCore::R0
, XCore::R1
, XCore::R2
, XCore::R3
60 const unsigned * XCoreRegisterInfo::getArgRegs(const MachineFunction
*MF
)
65 unsigned XCoreRegisterInfo::getNumArgRegs(const MachineFunction
*MF
)
67 return array_lengthof(XCore_ArgRegs
);
70 bool XCoreRegisterInfo::needsFrameMoves(const MachineFunction
&MF
) {
71 return MF
.getMMI().hasDebugInfo() || !MF
.getFunction()->doesNotThrow() ||
72 UnwindTablesMandatory
;
75 const unsigned* XCoreRegisterInfo::getCalleeSavedRegs(const MachineFunction
*MF
)
77 static const unsigned CalleeSavedRegs
[] = {
78 XCore::R4
, XCore::R5
, XCore::R6
, XCore::R7
,
79 XCore::R8
, XCore::R9
, XCore::R10
, XCore::LR
,
82 return CalleeSavedRegs
;
85 BitVector
XCoreRegisterInfo::getReservedRegs(const MachineFunction
&MF
) const {
86 BitVector
Reserved(getNumRegs());
87 const TargetFrameLowering
*TFI
= MF
.getTarget().getFrameLowering();
89 Reserved
.set(XCore::CP
);
90 Reserved
.set(XCore::DP
);
91 Reserved
.set(XCore::SP
);
92 Reserved
.set(XCore::LR
);
94 Reserved
.set(XCore::R10
);
100 XCoreRegisterInfo::requiresRegisterScavenging(const MachineFunction
&MF
) const {
101 const TargetFrameLowering
*TFI
= MF
.getTarget().getFrameLowering();
103 // TODO can we estimate stack size?
104 return TFI
->hasFP(MF
);
108 XCoreRegisterInfo::useFPForScavengingIndex(const MachineFunction
&MF
) const {
112 // This function eliminates ADJCALLSTACKDOWN,
113 // ADJCALLSTACKUP pseudo instructions
114 void XCoreRegisterInfo::
115 eliminateCallFramePseudoInstr(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
116 MachineBasicBlock::iterator I
) const {
117 const TargetFrameLowering
*TFI
= MF
.getTarget().getFrameLowering();
119 if (!TFI
->hasReservedCallFrame(MF
)) {
120 // Turn the adjcallstackdown instruction into 'extsp <amt>' and the
121 // adjcallstackup instruction into 'ldaw sp, sp[<amt>]'
122 MachineInstr
*Old
= I
;
123 uint64_t Amount
= Old
->getOperand(0).getImm();
125 // We need to keep the stack aligned properly. To do this, we round the
126 // amount of space needed for the outgoing arguments up to the next
127 // alignment boundary.
128 unsigned Align
= TFI
->getStackAlignment();
129 Amount
= (Amount
+Align
-1)/Align
*Align
;
131 assert(Amount
%4 == 0);
134 bool isU6
= isImmU6(Amount
);
135 if (!isU6
&& !isImmU16(Amount
)) {
136 // FIX could emit multiple instructions in this case.
138 errs() << "eliminateCallFramePseudoInstr size too big: "
145 if (Old
->getOpcode() == XCore::ADJCALLSTACKDOWN
) {
146 int Opcode
= isU6
? XCore::EXTSP_u6
: XCore::EXTSP_lu6
;
147 New
=BuildMI(MF
, Old
->getDebugLoc(), TII
.get(Opcode
))
150 assert(Old
->getOpcode() == XCore::ADJCALLSTACKUP
);
151 int Opcode
= isU6
? XCore::LDAWSP_ru6_RRegs
: XCore::LDAWSP_lru6_RRegs
;
152 New
=BuildMI(MF
, Old
->getDebugLoc(), TII
.get(Opcode
), XCore::SP
)
156 // Replace the pseudo instruction with a new instruction...
165 XCoreRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II
,
166 int SPAdj
, RegScavenger
*RS
) const {
167 assert(SPAdj
== 0 && "Unexpected");
168 MachineInstr
&MI
= *II
;
169 DebugLoc dl
= MI
.getDebugLoc();
172 while (!MI
.getOperand(i
).isFI()) {
174 assert(i
< MI
.getNumOperands() && "Instr doesn't have FrameIndex operand!");
177 MachineOperand
&FrameOp
= MI
.getOperand(i
);
178 int FrameIndex
= FrameOp
.getIndex();
180 MachineFunction
&MF
= *MI
.getParent()->getParent();
181 const TargetFrameLowering
*TFI
= MF
.getTarget().getFrameLowering();
182 int Offset
= MF
.getFrameInfo()->getObjectOffset(FrameIndex
);
183 int StackSize
= MF
.getFrameInfo()->getStackSize();
186 DEBUG(errs() << "\nFunction : "
187 << MF
.getFunction()->getName() << "\n");
188 DEBUG(errs() << "<--------->\n");
189 DEBUG(MI
.print(errs()));
190 DEBUG(errs() << "FrameIndex : " << FrameIndex
<< "\n");
191 DEBUG(errs() << "FrameOffset : " << Offset
<< "\n");
192 DEBUG(errs() << "StackSize : " << StackSize
<< "\n");
197 // fold constant into offset.
198 Offset
+= MI
.getOperand(i
+ 1).getImm();
199 MI
.getOperand(i
+ 1).ChangeToImmediate(0);
201 assert(Offset
%4 == 0 && "Misaligned stack offset");
203 DEBUG(errs() << "Offset : " << Offset
<< "\n" << "<--------->\n");
207 bool FP
= TFI
->hasFP(MF
);
209 unsigned Reg
= MI
.getOperand(0).getReg();
210 bool isKill
= MI
.getOpcode() == XCore::STWFI
&& MI
.getOperand(0).isKill();
212 assert(XCore::GRRegsRegisterClass
->contains(Reg
) &&
213 "Unexpected register operand");
215 MachineBasicBlock
&MBB
= *MI
.getParent();
218 bool isUs
= isImmUs(Offset
);
219 unsigned FramePtr
= XCore::R10
;
223 report_fatal_error("eliminateFrameIndex Frame size too big: " +
225 unsigned ScratchReg
= RS
->scavengeRegister(XCore::GRRegsRegisterClass
, II
,
227 loadConstant(MBB
, II
, ScratchReg
, Offset
, dl
);
228 switch (MI
.getOpcode()) {
230 BuildMI(MBB
, II
, dl
, TII
.get(XCore::LDW_3r
), Reg
)
232 .addReg(ScratchReg
, RegState::Kill
);
235 BuildMI(MBB
, II
, dl
, TII
.get(XCore::STW_3r
))
236 .addReg(Reg
, getKillRegState(isKill
))
238 .addReg(ScratchReg
, RegState::Kill
);
241 BuildMI(MBB
, II
, dl
, TII
.get(XCore::LDAWF_l3r
), Reg
)
243 .addReg(ScratchReg
, RegState::Kill
);
246 llvm_unreachable("Unexpected Opcode");
249 switch (MI
.getOpcode()) {
251 BuildMI(MBB
, II
, dl
, TII
.get(XCore::LDW_2rus
), Reg
)
256 BuildMI(MBB
, II
, dl
, TII
.get(XCore::STW_2rus
))
257 .addReg(Reg
, getKillRegState(isKill
))
262 BuildMI(MBB
, II
, dl
, TII
.get(XCore::LDAWF_l2rus
), Reg
)
267 llvm_unreachable("Unexpected Opcode");
271 bool isU6
= isImmU6(Offset
);
272 if (!isU6
&& !isImmU16(Offset
))
273 report_fatal_error("eliminateFrameIndex Frame size too big: " +
276 switch (MI
.getOpcode()) {
279 NewOpcode
= (isU6
) ? XCore::LDWSP_ru6
: XCore::LDWSP_lru6
;
280 BuildMI(MBB
, II
, dl
, TII
.get(NewOpcode
), Reg
)
284 NewOpcode
= (isU6
) ? XCore::STWSP_ru6
: XCore::STWSP_lru6
;
285 BuildMI(MBB
, II
, dl
, TII
.get(NewOpcode
))
286 .addReg(Reg
, getKillRegState(isKill
))
290 NewOpcode
= (isU6
) ? XCore::LDAWSP_ru6
: XCore::LDAWSP_lru6
;
291 BuildMI(MBB
, II
, dl
, TII
.get(NewOpcode
), Reg
)
295 llvm_unreachable("Unexpected Opcode");
298 // Erase old instruction.
302 void XCoreRegisterInfo::
303 loadConstant(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator I
,
304 unsigned DstReg
, int64_t Value
, DebugLoc dl
) const {
305 // TODO use mkmsk if possible.
306 if (!isImmU16(Value
)) {
307 // TODO use constant pool.
308 report_fatal_error("loadConstant value too big " + Twine(Value
));
310 int Opcode
= isImmU6(Value
) ? XCore::LDC_ru6
: XCore::LDC_lru6
;
311 BuildMI(MBB
, I
, dl
, TII
.get(Opcode
), DstReg
).addImm(Value
);
314 int XCoreRegisterInfo::getDwarfRegNum(unsigned RegNum
, bool isEH
) const {
315 return XCoreGenRegisterInfo::getDwarfRegNumFull(RegNum
, 0);
318 unsigned XCoreRegisterInfo::getFrameRegister(const MachineFunction
&MF
) const {
319 const TargetFrameLowering
*TFI
= MF
.getTarget().getFrameLowering();
321 return TFI
->hasFP(MF
) ? XCore::R10
: XCore::SP
;
324 unsigned XCoreRegisterInfo::getRARegister() const {
328 #include "XCoreGenRegisterInfo.inc"