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/TargetOptions.h"
23 #include "llvm/ADT/STLExtras.h"
27 // hasFP - Return true if the specified function should have a dedicated frame
28 // pointer register. This is true if the function has variable sized allocas or
29 // if frame pointer elimination is disabled.
31 static bool hasFP(const MachineFunction &MF) {
32 const MachineFrameInfo *MFI = MF.getFrameInfo();
33 return NoFramePointerElim || MFI->hasVarSizedObjects();
36 ARMRegisterInfo::ARMRegisterInfo()
37 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP) {
40 void ARMRegisterInfo::
41 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
42 unsigned SrcReg, int FI,
43 const TargetRegisterClass *RC) const {
44 assert (RC == ARM::IntRegsRegisterClass);
45 BuildMI(MBB, I, ARM::str, 3).addReg(SrcReg).addImm(0).addFrameIndex(FI);
48 void ARMRegisterInfo::
49 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
50 unsigned DestReg, int FI,
51 const TargetRegisterClass *RC) const {
52 assert (RC == ARM::IntRegsRegisterClass);
53 BuildMI(MBB, I, ARM::ldr, 2, DestReg).addImm(0).addFrameIndex(FI);
56 void ARMRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
57 MachineBasicBlock::iterator I,
58 unsigned DestReg, unsigned SrcReg,
59 const TargetRegisterClass *RC) const {
60 assert(RC == ARM::IntRegsRegisterClass ||
61 RC == ARM::FPRegsRegisterClass ||
62 RC == ARM::DFPRegsRegisterClass);
64 if (RC == ARM::IntRegsRegisterClass)
65 BuildMI(MBB, I, ARM::MOV, 3, DestReg).addReg(SrcReg).addImm(0)
66 .addImm(ARMShift::LSL);
67 else if (RC == ARM::FPRegsRegisterClass)
68 BuildMI(MBB, I, ARM::FCPYS, 1, DestReg).addReg(SrcReg);
70 BuildMI(MBB, I, ARM::FCPYD, 1, DestReg).addReg(SrcReg);
73 MachineInstr *ARMRegisterInfo::foldMemoryOperand(MachineInstr* MI,
79 const unsigned* ARMRegisterInfo::getCalleeSaveRegs() const {
80 static const unsigned CalleeSaveRegs[] = {
81 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
82 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
85 return CalleeSaveRegs;
88 const TargetRegisterClass* const *
89 ARMRegisterInfo::getCalleeSaveRegClasses() const {
90 static const TargetRegisterClass * const CalleeSaveRegClasses[] = {
91 &ARM::IntRegsRegClass, &ARM::IntRegsRegClass, &ARM::IntRegsRegClass, &ARM::IntRegsRegClass,
92 &ARM::IntRegsRegClass, &ARM::IntRegsRegClass, &ARM::IntRegsRegClass, &ARM::IntRegsRegClass,
93 &ARM::IntRegsRegClass, 0
95 return CalleeSaveRegClasses;
98 void ARMRegisterInfo::
99 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
100 MachineBasicBlock::iterator I) const {
108 ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
109 MachineInstr &MI = *II;
110 MachineBasicBlock &MBB = *MI.getParent();
111 MachineFunction &MF = *MBB.getParent();
113 assert (MI.getOpcode() == ARM::ldr ||
114 MI.getOpcode() == ARM::str ||
115 MI.getOpcode() == ARM::lea_addri);
117 unsigned FrameIdx = 2;
120 int FrameIndex = MI.getOperand(FrameIdx).getFrameIndex();
122 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
123 MI.getOperand(OffIdx).getImmedValue();
125 unsigned StackSize = MF.getFrameInfo()->getStackSize();
129 assert (Offset >= 0);
130 unsigned BaseRegister = hasFP(MF) ? ARM::R11 : ARM::R13;
132 // Replace the FrameIndex with r13
133 MI.getOperand(FrameIdx).ChangeToRegister(BaseRegister, false);
134 // Replace the ldr offset with Offset
135 MI.getOperand(OffIdx).ChangeToImmediate(Offset);
137 // Insert a set of r12 with the full address
138 // r12 = r13 + offset
139 MachineBasicBlock *MBB2 = MI.getParent();
140 BuildMI(*MBB2, II, ARM::ADD, 4, ARM::R12).addReg(BaseRegister)
141 .addImm(Offset).addImm(0).addImm(ARMShift::LSL);
143 // Replace the FrameIndex with r12
144 MI.getOperand(FrameIdx).ChangeToRegister(ARM::R12, false);
148 void ARMRegisterInfo::
149 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {}
151 void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
152 MachineBasicBlock &MBB = MF.front();
153 MachineBasicBlock::iterator MBBI = MBB.begin();
154 MachineFrameInfo *MFI = MF.getFrameInfo();
155 int NumBytes = (int) MFI->getStackSize();
157 bool HasFP = hasFP(MF);
159 if (MFI->hasCalls()) {
160 // We reserve argument space for call sites in the function immediately on
161 // entry to the current function. This eliminates the need for add/sub
162 // brackets around call sites.
163 NumBytes += MFI->getMaxCallFrameSize();
167 // Add space for storing the FP
171 NumBytes = ((NumBytes + 7) / 8) * 8;
173 MFI->setStackSize(NumBytes);
175 //sub sp, sp, #NumBytes
176 BuildMI(MBB, MBBI, ARM::SUB, 4, ARM::R13).addReg(ARM::R13).addImm(NumBytes)
177 .addImm(0).addImm(ARMShift::LSL);
180 BuildMI(MBB, MBBI, ARM::str, 3)
181 .addReg(ARM::R11).addImm(0).addReg(ARM::R13);
182 BuildMI(MBB, MBBI, ARM::MOV, 3, ARM::R11).addReg(ARM::R13).addImm(0).
183 addImm(ARMShift::LSL);
187 void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
188 MachineBasicBlock &MBB) const {
189 MachineBasicBlock::iterator MBBI = prior(MBB.end());
190 assert(MBBI->getOpcode() == ARM::bx &&
191 "Can only insert epilog into returning blocks");
193 MachineFrameInfo *MFI = MF.getFrameInfo();
194 int NumBytes = (int) MFI->getStackSize();
197 BuildMI(MBB, MBBI, ARM::MOV, 3, ARM::R13).addReg(ARM::R11).addImm(0).
198 addImm(ARMShift::LSL);
199 BuildMI(MBB, MBBI, ARM::ldr, 2, ARM::R11).addImm(0).addReg(ARM::R13);
202 //add sp, sp, #NumBytes
203 BuildMI(MBB, MBBI, ARM::ADD, 4, ARM::R13).addReg(ARM::R13).addImm(NumBytes)
204 .addImm(0).addImm(ARMShift::LSL);
207 unsigned ARMRegisterInfo::getRARegister() const {
211 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction &MF) const {
212 return hasFP(MF) ? ARM::R11 : ARM::R13;
215 #include "ARMGenRegisterInfo.inc"