1 //===- ARMRegisterInfo.cpp - ARM Register Information -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the "Instituto Nokia de Tecnologia" and
6 // is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file contains the ARM implementation of the MRegisterInfo class.
13 //===----------------------------------------------------------------------===//
16 #include "ARMRegisterInfo.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/Type.h"
22 #include "llvm/Target/TargetFrameInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Target/TargetInstrInfo.h"
26 #include "llvm/ADT/STLExtras.h"
30 // hasFP - Return true if the specified function should have a dedicated frame
31 // pointer register. This is true if the function has variable sized allocas or
32 // if frame pointer elimination is disabled.
34 static bool hasFP(const MachineFunction &MF) {
35 const MachineFrameInfo *MFI = MF.getFrameInfo();
36 return NoFramePointerElim || MFI->hasVarSizedObjects();
39 ARMRegisterInfo::ARMRegisterInfo(const TargetInstrInfo &tii)
40 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
44 void ARMRegisterInfo::
45 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
46 unsigned SrcReg, int FI,
47 const TargetRegisterClass *RC) const {
48 assert (RC == ARM::IntRegsRegisterClass);
49 BuildMI(MBB, I, TII.get(ARM::STR)).addReg(SrcReg).addFrameIndex(FI).addImm(0);
52 void ARMRegisterInfo::
53 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
54 unsigned DestReg, int FI,
55 const TargetRegisterClass *RC) const {
56 assert (RC == ARM::IntRegsRegisterClass);
57 BuildMI(MBB, I, TII.get(ARM::LDR), DestReg).addFrameIndex(FI).addImm(0);
60 void ARMRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
61 MachineBasicBlock::iterator I,
62 unsigned DestReg, unsigned SrcReg,
63 const TargetRegisterClass *RC) const {
64 assert(RC == ARM::IntRegsRegisterClass ||
65 RC == ARM::FPRegsRegisterClass ||
66 RC == ARM::DFPRegsRegisterClass);
68 if (RC == ARM::IntRegsRegisterClass)
69 BuildMI(MBB, I, TII.get(ARM::MOV), DestReg).addReg(SrcReg).addImm(0)
70 .addImm(ARMShift::LSL);
71 else if (RC == ARM::FPRegsRegisterClass)
72 BuildMI(MBB, I, TII.get(ARM::FCPYS), DestReg).addReg(SrcReg);
74 BuildMI(MBB, I, TII.get(ARM::FCPYD), DestReg).addReg(SrcReg);
77 MachineInstr *ARMRegisterInfo::foldMemoryOperand(MachineInstr* MI,
83 const unsigned* ARMRegisterInfo::getCalleeSaveRegs() const {
84 static const unsigned CalleeSaveRegs[] = {
85 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
86 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
89 return CalleeSaveRegs;
92 const TargetRegisterClass* const *
93 ARMRegisterInfo::getCalleeSaveRegClasses() const {
94 static const TargetRegisterClass * const CalleeSaveRegClasses[] = {
95 &ARM::IntRegsRegClass, &ARM::IntRegsRegClass, &ARM::IntRegsRegClass, &ARM::IntRegsRegClass,
96 &ARM::IntRegsRegClass, &ARM::IntRegsRegClass, &ARM::IntRegsRegClass, &ARM::IntRegsRegClass,
97 &ARM::IntRegsRegClass, 0
99 return CalleeSaveRegClasses;
102 void ARMRegisterInfo::
103 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
104 MachineBasicBlock::iterator I) const {
106 MachineInstr *Old = I;
107 unsigned Amount = Old->getOperand(0).getImmedValue();
109 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
110 Amount = (Amount+Align-1)/Align*Align;
112 if (Old->getOpcode() == ARM::ADJCALLSTACKDOWN) {
113 // sub sp, sp, amount
114 BuildMI(MBB, I, TII.get(ARM::SUB), ARM::R13).addReg(ARM::R13).addImm(Amount)
115 .addImm(0).addImm(ARMShift::LSL);
117 // add sp, sp, amount
118 assert(Old->getOpcode() == ARM::ADJCALLSTACKUP);
119 BuildMI(MBB, I, TII.get(ARM::ADD), ARM::R13).addReg(ARM::R13).addImm(Amount)
120 .addImm(0).addImm(ARMShift::LSL);
128 ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
129 MachineInstr &MI = *II;
130 MachineBasicBlock &MBB = *MI.getParent();
131 MachineFunction &MF = *MBB.getParent();
133 assert (MI.getOpcode() == ARM::LDR ||
134 MI.getOpcode() == ARM::STR ||
135 MI.getOpcode() == ARM::ADD);
137 unsigned FrameIdx = 1;
140 int FrameIndex = MI.getOperand(FrameIdx).getFrameIndex();
142 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
143 MI.getOperand(OffIdx).getImmedValue();
145 unsigned StackSize = MF.getFrameInfo()->getStackSize();
149 assert (Offset >= 0);
150 unsigned BaseRegister = hasFP(MF) ? ARM::R11 : ARM::R13;
152 // Replace the FrameIndex with r13
153 MI.getOperand(FrameIdx).ChangeToRegister(BaseRegister, false);
154 // Replace the ldr offset with Offset
155 MI.getOperand(OffIdx).ChangeToImmediate(Offset);
157 // Insert a set of r12 with the full address
158 // r12 = r13 + offset
159 MachineBasicBlock *MBB2 = MI.getParent();
160 BuildMI(*MBB2, II, TII.get(ARM::ADD), ARM::R12).addReg(BaseRegister)
161 .addImm(Offset).addImm(0).addImm(ARMShift::LSL);
163 // Replace the FrameIndex with r12
164 MI.getOperand(FrameIdx).ChangeToRegister(ARM::R12, false);
168 void ARMRegisterInfo::
169 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {}
171 void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
172 MachineBasicBlock &MBB = MF.front();
173 MachineBasicBlock::iterator MBBI = MBB.begin();
174 MachineFrameInfo *MFI = MF.getFrameInfo();
175 int NumBytes = (int) MFI->getStackSize();
177 bool HasFP = hasFP(MF);
179 if (MFI->hasCalls()) {
180 // We reserve argument space for call sites in the function immediately on
181 // entry to the current function. This eliminates the need for add/sub
182 // brackets around call sites.
183 NumBytes += MFI->getMaxCallFrameSize();
187 // Add space for storing the FP
191 NumBytes = ((NumBytes + 7) / 8) * 8;
193 MFI->setStackSize(NumBytes);
195 //sub sp, sp, #NumBytes
196 BuildMI(MBB, MBBI, TII.get(ARM::SUB), ARM::R13).addReg(ARM::R13).addImm(NumBytes)
197 .addImm(0).addImm(ARMShift::LSL);
200 BuildMI(MBB, MBBI, TII.get(ARM::STR))
201 .addReg(ARM::R11).addReg(ARM::R13).addImm(0);
202 BuildMI(MBB, MBBI, TII.get(ARM::MOV), ARM::R11).addReg(ARM::R13).addImm(0).
203 addImm(ARMShift::LSL);
207 void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
208 MachineBasicBlock &MBB) const {
209 MachineBasicBlock::iterator MBBI = prior(MBB.end());
210 assert(MBBI->getOpcode() == ARM::bx &&
211 "Can only insert epilog into returning blocks");
213 MachineFrameInfo *MFI = MF.getFrameInfo();
214 int NumBytes = (int) MFI->getStackSize();
217 BuildMI(MBB, MBBI, TII.get(ARM::MOV), ARM::R13).addReg(ARM::R11).addImm(0).
218 addImm(ARMShift::LSL);
219 BuildMI(MBB, MBBI, TII.get(ARM::LDR), ARM::R11).addReg(ARM::R13).addImm(0);
222 //add sp, sp, #NumBytes
223 BuildMI(MBB, MBBI, TII.get(ARM::ADD), ARM::R13).addReg(ARM::R13).addImm(NumBytes)
224 .addImm(0).addImm(ARMShift::LSL);
227 unsigned ARMRegisterInfo::getRARegister() const {
231 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction &MF) const {
232 return hasFP(MF) ? ARM::R11 : ARM::R13;
235 #include "ARMGenRegisterInfo.inc"