blob: 3cfe61b738d45b46fb4da7715cde068bf7a7dee9 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- Thumb1FrameLowering.cpp - Thumb1 Frame Information ----------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Anton Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the Thumb1 implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "Thumb1FrameLowering.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000015#include "ARMMachineFunctionInfo.h"
Quentin Colombet71a71482015-07-20 21:42:14 +000016#include "llvm/CodeGen/LivePhysRegs.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Chengeb56dca2010-11-22 18:12:04 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000022
23using namespace llvm;
24
Eric Christopher45fb7b62014-06-26 19:29:59 +000025Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti)
26 : ARMFrameLowering(sti) {}
27
Jim Grosbache7e2aca2011-09-13 20:30:37 +000028bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
Matthias Braun941a7052016-07-28 18:40:00 +000029 const MachineFrameInfo &MFI = MF.getFrameInfo();
30 unsigned CFSize = MFI.getMaxCallFrameSize();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000031 // It's not always a good idea to include the call frame as part of the
32 // stack frame. ARM (especially Thumb) has small immediate offset to
33 // address the stack frame. So a large call frame can cause poor codegen
34 // and may even makes it impossible to scavenge a register.
35 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
36 return false;
37
Matthias Braun941a7052016-07-28 18:40:00 +000038 return !MFI.hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000039}
40
Benjamin Kramerbdc49562016-06-12 15:39:02 +000041static void emitSPUpdate(MachineBasicBlock &MBB,
42 MachineBasicBlock::iterator &MBBI,
43 const TargetInstrInfo &TII, const DebugLoc &dl,
44 const ThumbRegisterInfo &MRI, int NumBytes,
45 unsigned MIFlags = MachineInstr::NoFlags) {
Anton Korobeynikove7410dd2011-03-05 18:43:32 +000046 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
Anton Korobeynikova8d177b2011-03-05 18:43:50 +000047 MRI, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000048}
49
Eli Bendersky8da87162013-02-21 20:05:00 +000050
Hans Wennborge1a2e902016-03-31 18:33:38 +000051MachineBasicBlock::iterator Thumb1FrameLowering::
Eli Bendersky8da87162013-02-21 20:05:00 +000052eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
53 MachineBasicBlock::iterator I) const {
54 const Thumb1InstrInfo &TII =
Eric Christopher1b21f002015-01-29 00:19:33 +000055 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
Eric Christopherae326492015-03-12 22:48:50 +000056 const ThumbRegisterInfo *RegInfo =
57 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +000058 if (!hasReservedCallFrame(MF)) {
59 // If we have alloca, convert as follows:
60 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
61 // ADJCALLSTACKUP -> add, sp, sp, amount
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +000062 MachineInstr &Old = *I;
63 DebugLoc dl = Old.getDebugLoc();
64 unsigned Amount = Old.getOperand(0).getImm();
Eli Bendersky8da87162013-02-21 20:05:00 +000065 if (Amount != 0) {
66 // We need to keep the stack aligned properly. To do this, we round the
67 // amount of space needed for the outgoing arguments up to the next
68 // alignment boundary.
69 unsigned Align = getStackAlignment();
70 Amount = (Amount+Align-1)/Align*Align;
71
72 // Replace the pseudo instruction with a new instruction...
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +000073 unsigned Opc = Old.getOpcode();
Eli Bendersky8da87162013-02-21 20:05:00 +000074 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
75 emitSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount);
76 } else {
77 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
78 emitSPUpdate(MBB, I, TII, dl, *RegInfo, Amount);
79 }
80 }
81 }
Hans Wennborge1a2e902016-03-31 18:33:38 +000082 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +000083}
84
Quentin Colombet61b305e2015-05-05 17:38:16 +000085void Thumb1FrameLowering::emitPrologue(MachineFunction &MF,
86 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000087 MachineBasicBlock::iterator MBBI = MBB.begin();
Matthias Braun941a7052016-07-28 18:40:00 +000088 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000089 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +000090 MachineModuleInfo &MMI = MF.getMMI();
91 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Eric Christopherae326492015-03-12 22:48:50 +000092 const ThumbRegisterInfo *RegInfo =
93 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000094 const Thumb1InstrInfo &TII =
Eric Christopher1b21f002015-01-29 00:19:33 +000095 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000096
Tim Northover8cda34f2015-03-11 18:54:22 +000097 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +000098 unsigned NumBytes = MFI.getStackSize();
Tim Northover775aaeb2015-11-05 21:54:58 +000099 assert(NumBytes >= ArgRegsSaveSize &&
100 "ArgRegsSaveSize is included in NumBytes");
Matthias Braun941a7052016-07-28 18:40:00 +0000101 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000102
103 // Debug location must be unknown since the first debug location is used
104 // to determine the end of the prologue.
105 DebugLoc dl;
106
107 unsigned FramePtr = RegInfo->getFrameRegister(MF);
108 unsigned BasePtr = RegInfo->getBaseRegister();
109 int CFAOffset = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000110
111 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
112 NumBytes = (NumBytes + 3) & ~3;
Matthias Braun941a7052016-07-28 18:40:00 +0000113 MFI.setStackSize(NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000114
115 // Determine the sizes of each callee-save spill areas and record which frame
116 // belongs to which callee-save spill areas.
117 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
118 int FramePtrSpillFI = 0;
119
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000120 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000121 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000122 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000123 CFAOffset -= ArgRegsSaveSize;
Matthias Braunf23ef432016-11-30 23:48:42 +0000124 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000125 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
126 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000127 .addCFIIndex(CFIIndex)
128 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000129 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000130
131 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000132 if (NumBytes - ArgRegsSaveSize != 0) {
133 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000134 MachineInstr::FrameSetup);
Oliver Stannardd55e1152014-03-05 15:25:27 +0000135 CFAOffset -= NumBytes - ArgRegsSaveSize;
Matthias Braunf23ef432016-11-30 23:48:42 +0000136 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000137 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
138 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000139 .addCFIIndex(CFIIndex)
140 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000141 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000142 return;
143 }
144
145 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
146 unsigned Reg = CSI[i].getReg();
147 int FI = CSI[i].getFrameIdx();
148 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000149 case ARM::R8:
150 case ARM::R9:
151 case ARM::R10:
152 case ARM::R11:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000153 if (STI.splitFramePushPop(MF)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000154 GPRCS2Size += 4;
155 break;
156 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000157 LLVM_FALLTHROUGH;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000158 case ARM::R4:
159 case ARM::R5:
160 case ARM::R6:
161 case ARM::R7:
162 case ARM::LR:
163 if (Reg == FramePtr)
164 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000165 GPRCS1Size += 4;
166 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000167 default:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000168 DPRCSSize += 8;
169 }
170 }
Tim Northover775aaeb2015-11-05 21:54:58 +0000171
172 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
173 ++MBBI;
174 }
175
176 // Determine starting offsets of spill areas.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000177 unsigned DPRCSOffset = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000178 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
179 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
Logan Chien53c18d82013-02-20 12:21:33 +0000180 bool HasFP = hasFP(MF);
181 if (HasFP)
Matthias Braun941a7052016-07-28 18:40:00 +0000182 AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
Logan Chien53c18d82013-02-20 12:21:33 +0000183 NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000184 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
185 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
186 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000187 NumBytes = DPRCSOffset;
Evan Chengeb56dca2010-11-22 18:12:04 +0000188
Tim Northover93bcc662013-11-08 17:18:07 +0000189 int FramePtrOffsetInBlock = 0;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000190 unsigned adjustedGPRCS1Size = GPRCS1Size;
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000191 if (GPRCS1Size > 0 && GPRCS2Size == 0 &&
192 tryFoldSPUpdateIntoPushPop(STI, MF, &*std::prev(MBBI), NumBytes)) {
Tim Northover93bcc662013-11-08 17:18:07 +0000193 FramePtrOffsetInBlock = NumBytes;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000194 adjustedGPRCS1Size += NumBytes;
Tim Northover93bcc662013-11-08 17:18:07 +0000195 NumBytes = 0;
196 }
197
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000198 if (adjustedGPRCS1Size) {
199 CFAOffset -= adjustedGPRCS1Size;
Matthias Braunf23ef432016-11-30 23:48:42 +0000200 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000201 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
202 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000203 .addCFIIndex(CFIIndex)
204 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000205 }
206 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
207 E = CSI.end(); I != E; ++I) {
208 unsigned Reg = I->getReg();
209 int FI = I->getFrameIdx();
210 switch (Reg) {
211 case ARM::R8:
212 case ARM::R9:
213 case ARM::R10:
214 case ARM::R11:
215 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000216 if (STI.splitFramePushPop(MF))
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000217 break;
218 // fallthough
219 case ARM::R0:
220 case ARM::R1:
221 case ARM::R2:
222 case ARM::R3:
223 case ARM::R4:
224 case ARM::R5:
225 case ARM::R6:
226 case ARM::R7:
227 case ARM::LR:
Matthias Braunf23ef432016-11-30 23:48:42 +0000228 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Matthias Braun941a7052016-07-28 18:40:00 +0000229 nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000230 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000231 .addCFIIndex(CFIIndex)
232 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000233 break;
234 }
235 }
236
Evan Chengeb56dca2010-11-22 18:12:04 +0000237 // Adjust FP so it point to the stack slot that contains the previous FP.
Logan Chien53c18d82013-02-20 12:21:33 +0000238 if (HasFP) {
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000239 FramePtrOffsetInBlock +=
Matthias Braun941a7052016-07-28 18:40:00 +0000240 MFI.getObjectOffset(FramePtrSpillFI) + GPRCS1Size + ArgRegsSaveSize;
Diana Picus4f8c3e12017-01-13 09:37:56 +0000241 BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
242 .addReg(ARM::SP)
243 .addImm(FramePtrOffsetInBlock / 4)
244 .setMIFlags(MachineInstr::FrameSetup)
245 .add(predOps(ARMCC::AL));
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000246 if(FramePtrOffsetInBlock) {
247 CFAOffset += FramePtrOffsetInBlock;
Matthias Braunf23ef432016-11-30 23:48:42 +0000248 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000249 nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
250 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000251 .addCFIIndex(CFIIndex)
252 .setMIFlags(MachineInstr::FrameSetup);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000253 } else {
254 unsigned CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +0000255 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000256 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
257 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000258 .addCFIIndex(CFIIndex)
259 .setMIFlags(MachineInstr::FrameSetup);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000260 }
Jim Grosbachdca85312011-06-13 21:18:25 +0000261 if (NumBytes > 508)
262 // If offset is > 508 then sp cannot be adjusted in a single instruction,
Evan Chengeb56dca2010-11-22 18:12:04 +0000263 // try restoring from fp instead.
264 AFI->setShouldRestoreSPFromFP(true);
265 }
266
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000267 // Skip past the spilling of r8-r11, which could consist of multiple tPUSH
268 // and tMOVr instructions. We don't need to add any call frame information
269 // in-between these instructions, because they do not modify the high
270 // registers.
271 while (true) {
272 MachineBasicBlock::iterator OldMBBI = MBBI;
273 // Skip a run of tMOVr instructions
274 while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tMOVr)
275 MBBI++;
276 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
277 MBBI++;
278 } else {
279 // We have reached an instruction which is not a push, so the previous
280 // run of tMOVr instructions (which may have been empty) was not part of
281 // the prologue. Reset MBBI back to the last PUSH of the prologue.
282 MBBI = OldMBBI;
283 break;
284 }
285 }
286
287 // Emit call frame information for the callee-saved high registers.
288 for (auto &I : CSI) {
289 unsigned Reg = I.getReg();
290 int FI = I.getFrameIdx();
291 switch (Reg) {
292 case ARM::R8:
293 case ARM::R9:
294 case ARM::R10:
295 case ARM::R11:
296 case ARM::R12: {
Matthias Braunf23ef432016-11-30 23:48:42 +0000297 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000298 nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
299 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
300 .addCFIIndex(CFIIndex)
301 .setMIFlags(MachineInstr::FrameSetup);
302 break;
303 }
304 default:
305 break;
306 }
307 }
308
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000309 if (NumBytes) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000310 // Insert it after all the callee-save spills.
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000311 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
312 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000313 if (!HasFP) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000314 CFAOffset -= NumBytes;
Matthias Braunf23ef432016-11-30 23:48:42 +0000315 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000316 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
317 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantld9e64b62014-12-22 23:09:14 +0000318 .addCFIIndex(CFIIndex)
319 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000320 }
321 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000322
Logan Chien53c18d82013-02-20 12:21:33 +0000323 if (STI.isTargetELF() && HasFP)
Matthias Braun941a7052016-07-28 18:40:00 +0000324 MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
325 AFI->getFramePtrSpillOffset());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000326
327 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
328 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
329 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
330
Chad Rosieradd38c12011-10-20 00:07:12 +0000331 // Thumb1 does not currently support dynamic stack realignment. Report a
332 // fatal error rather then silently generate bad code.
333 if (RegInfo->needsStackRealignment(MF))
334 report_fatal_error("Dynamic stack realignment not supported for thumb1.");
Chad Rosier1809d6c2011-10-15 00:28:24 +0000335
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000336 // If we need a base pointer, set it up here. It's whatever the value
337 // of the stack pointer is at this point. Any variable size objects
338 // will be allocated after this, so we can still use the base pointer
339 // to reference locals.
340 if (RegInfo->hasBasePointer(MF))
Diana Picus4f8c3e12017-01-13 09:37:56 +0000341 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
342 .addReg(ARM::SP)
343 .add(predOps(ARMCC::AL));
Anton Korobeynikova8d177b2011-03-05 18:43:50 +0000344
Eric Christopher39043432011-01-11 00:16:04 +0000345 // If the frame has variable sized objects then the epilogue must restore
346 // the sp from fp. We can assume there's an FP here since hasFP already
347 // checks for hasVarSizedObjects.
Matthias Braun941a7052016-07-28 18:40:00 +0000348 if (MFI.hasVarSizedObjects())
Eric Christopher39043432011-01-11 00:16:04 +0000349 AFI->setShouldRestoreSPFromFP(true);
Florian Hahn8485cec2017-01-18 15:01:22 +0000350
351 // In some cases, virtual registers have been introduced, e.g. by uses of
352 // emitThumbRegPlusImmInReg.
353 MF.getProperties().reset(MachineFunctionProperties::Property::NoVRegs);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000354}
355
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000356static bool isCSRestore(MachineInstr &MI, const MCPhysReg *CSRegs) {
357 if (MI.getOpcode() == ARM::tLDRspi && MI.getOperand(1).isFI() &&
358 isCalleeSavedRegister(MI.getOperand(0).getReg(), CSRegs))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000359 return true;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000360 else if (MI.getOpcode() == ARM::tPOP) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000361 return true;
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000362 } else if (MI.getOpcode() == ARM::tMOVr) {
363 unsigned Dst = MI.getOperand(0).getReg();
364 unsigned Src = MI.getOperand(1).getReg();
365 return ((ARM::tGPRRegClass.contains(Src) || Src == ARM::LR) &&
366 ARM::hGPRRegClass.contains(Dst));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000367 }
368 return false;
369}
370
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000371void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000372 MachineBasicBlock &MBB) const {
Quentin Colombet71a71482015-07-20 21:42:14 +0000373 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
374 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Matthias Braun941a7052016-07-28 18:40:00 +0000375 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000376 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopherae326492015-03-12 22:48:50 +0000377 const ThumbRegisterInfo *RegInfo =
378 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000379 const Thumb1InstrInfo &TII =
Eric Christopher1b21f002015-01-29 00:19:33 +0000380 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000381
Tim Northover8cda34f2015-03-11 18:54:22 +0000382 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +0000383 int NumBytes = (int)MFI.getStackSize();
David Blaikie7f4a52e2014-03-05 18:53:36 +0000384 assert((unsigned)NumBytes >= ArgRegsSaveSize &&
Oliver Stannardd55e1152014-03-05 15:25:27 +0000385 "ArgRegsSaveSize is included in NumBytes");
Eric Christopher7af952872015-03-11 21:41:28 +0000386 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000387 unsigned FramePtr = RegInfo->getFrameRegister(MF);
388
389 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000390 if (NumBytes - ArgRegsSaveSize != 0)
391 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000392 } else {
393 // Unwind MBBI to point to first LDR / VLDRD.
394 if (MBBI != MBB.begin()) {
395 do
396 --MBBI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000397 while (MBBI != MBB.begin() && isCSRestore(*MBBI, CSRegs));
398 if (!isCSRestore(*MBBI, CSRegs))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000399 ++MBBI;
400 }
401
402 // Move SP to start of FP callee save spill area.
403 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
404 AFI->getGPRCalleeSavedArea2Size() +
Oliver Stannardd55e1152014-03-05 15:25:27 +0000405 AFI->getDPRCalleeSavedAreaSize() +
406 ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000407
408 if (AFI->shouldRestoreSPFromFP()) {
409 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
410 // Reset SP based on frame pointer only if the stack frame extends beyond
Eric Christopher39043432011-01-11 00:16:04 +0000411 // frame pointer stack slot, the target is ELF and the function has FP, or
412 // the target uses var sized objects.
Evan Chengeb56dca2010-11-22 18:12:04 +0000413 if (NumBytes) {
Matthias Braun941a7052016-07-28 18:40:00 +0000414 assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000415 "No scratch register to restore SP from FP!");
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000416 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
417 TII, *RegInfo);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000418 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
419 .addReg(ARM::R4)
420 .add(predOps(ARMCC::AL));
Evan Chengeb56dca2010-11-22 18:12:04 +0000421 } else
Diana Picus4f8c3e12017-01-13 09:37:56 +0000422 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
423 .addReg(FramePtr)
424 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000425 } else {
Quentin Colombet71a71482015-07-20 21:42:14 +0000426 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET &&
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000427 &MBB.front() != &*MBBI && std::prev(MBBI)->getOpcode() == ARM::tPOP) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000428 MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000429 if (!tryFoldSPUpdateIntoPushPop(STI, MF, &*PMBBI, NumBytes))
Tim Northover93bcc662013-11-08 17:18:07 +0000430 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000431 } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000432 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
433 }
434 }
435
Quentin Colombet48b77202015-07-22 16:34:37 +0000436 if (needPopSpecialFixUp(MF)) {
437 bool Done = emitPopSpecialFixUp(MBB, /* DoIt */ true);
438 (void)Done;
439 assert(Done && "Emission of the special fixup failed!?");
440 }
441}
442
443bool Thumb1FrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
444 if (!needPopSpecialFixUp(*MBB.getParent()))
445 return true;
446
447 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
448 return emitPopSpecialFixUp(*TmpMBB, /* DoIt */ false);
449}
450
451bool Thumb1FrameLowering::needPopSpecialFixUp(const MachineFunction &MF) const {
452 ARMFunctionInfo *AFI =
453 const_cast<MachineFunction *>(&MF)->getInfo<ARMFunctionInfo>();
454 if (AFI->getArgRegsSaveSize())
455 return true;
456
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000457 // LR cannot be encoded with Thumb1, i.e., it requires a special fix-up.
Matthias Braun941a7052016-07-28 18:40:00 +0000458 for (const CalleeSavedInfo &CSI : MF.getFrameInfo().getCalleeSavedInfo())
Jonathan Roelofsef84bda2014-08-05 21:32:21 +0000459 if (CSI.getReg() == ARM::LR)
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000460 return true;
461
462 return false;
Quentin Colombet48b77202015-07-22 16:34:37 +0000463}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000464
Quentin Colombet48b77202015-07-22 16:34:37 +0000465bool Thumb1FrameLowering::emitPopSpecialFixUp(MachineBasicBlock &MBB,
466 bool DoIt) const {
467 MachineFunction &MF = *MBB.getParent();
468 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
469 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
470 const TargetInstrInfo &TII = *STI.getInstrInfo();
471 const ThumbRegisterInfo *RegInfo =
472 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
Quentin Colombet71a71482015-07-20 21:42:14 +0000473
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000474 // If MBBI is a return instruction, or is a tPOP followed by a return
475 // instruction in the successor BB, we may be able to directly restore
476 // LR in the PC.
477 // This is only possible with v5T ops (v4T can't change the Thumb bit via
478 // a POP PC instruction), and only if we do not need to emit any SP update.
479 // Otherwise, we need a temporary register to pop the value
480 // and copy that value into LR.
Quentin Colombet48b77202015-07-22 16:34:37 +0000481 auto MBBI = MBB.getFirstTerminator();
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000482 bool CanRestoreDirectly = STI.hasV5TOps() && !ArgRegsSaveSize;
483 if (CanRestoreDirectly) {
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000484 if (MBBI != MBB.end() && MBBI->getOpcode() != ARM::tB)
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000485 CanRestoreDirectly = (MBBI->getOpcode() == ARM::tBX_RET ||
486 MBBI->getOpcode() == ARM::tPOP_RET);
487 else {
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000488 auto MBBI_prev = MBBI;
489 MBBI_prev--;
490 assert(MBBI_prev->getOpcode() == ARM::tPOP);
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000491 assert(MBB.succ_size() == 1);
492 if ((*MBB.succ_begin())->begin()->getOpcode() == ARM::tBX_RET)
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000493 MBBI = MBBI_prev; // Replace the final tPOP with a tPOP_RET.
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000494 else
495 CanRestoreDirectly = false;
496 }
497 }
498
499 if (CanRestoreDirectly) {
500 if (!DoIt || MBBI->getOpcode() == ARM::tPOP_RET)
501 return true;
502 MachineInstrBuilder MIB =
Diana Picus4f8c3e12017-01-13 09:37:56 +0000503 BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP_RET))
504 .add(predOps(ARMCC::AL));
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000505 // Copy implicit ops and popped registers, if any.
506 for (auto MO: MBBI->operands())
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000507 if (MO.isReg() && (MO.isImplicit() || MO.isDef()))
Diana Picus116bbab2017-01-13 09:58:52 +0000508 MIB.add(MO);
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000509 MIB.addReg(ARM::PC, RegState::Define);
510 // Erase the old instruction (tBX_RET or tPOP).
511 MBB.erase(MBBI);
512 return true;
513 }
Quentin Colombet71a71482015-07-20 21:42:14 +0000514
Quentin Colombet48b77202015-07-22 16:34:37 +0000515 // Look for a temporary register to use.
516 // First, compute the liveness information.
517 LivePhysRegs UsedRegs(STI.getRegisterInfo());
Matthias Braund1aabb22016-05-03 00:24:32 +0000518 UsedRegs.addLiveOuts(MBB);
Quentin Colombet48b77202015-07-22 16:34:37 +0000519 // The semantic of pristines changed recently and now,
520 // the callee-saved registers that are touched in the function
521 // are not part of the pristines set anymore.
522 // Add those callee-saved now.
523 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
524 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
525 for (unsigned i = 0; CSRegs[i]; ++i)
526 UsedRegs.addReg(CSRegs[i]);
Quentin Colombet71a71482015-07-20 21:42:14 +0000527
Quentin Colombet48b77202015-07-22 16:34:37 +0000528 DebugLoc dl = DebugLoc();
529 if (MBBI != MBB.end()) {
530 dl = MBBI->getDebugLoc();
531 auto InstUpToMBBI = MBB.end();
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000532 while (InstUpToMBBI != MBBI)
533 // The pre-decrement is on purpose here.
534 // We want to have the liveness right before MBBI.
535 UsedRegs.stepBackward(*--InstUpToMBBI);
Quentin Colombet48b77202015-07-22 16:34:37 +0000536 }
537
538 // Look for a register that can be directly use in the POP.
539 unsigned PopReg = 0;
540 // And some temporary register, just in case.
541 unsigned TemporaryReg = 0;
542 BitVector PopFriendly =
543 TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::tGPRRegClassID));
544 assert(PopFriendly.any() && "No allocatable pop-friendly register?!");
545 // Rebuild the GPRs from the high registers because they are removed
546 // form the GPR reg class for thumb1.
547 BitVector GPRsNoLRSP =
548 TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::hGPRRegClassID));
549 GPRsNoLRSP |= PopFriendly;
550 GPRsNoLRSP.reset(ARM::LR);
551 GPRsNoLRSP.reset(ARM::SP);
552 GPRsNoLRSP.reset(ARM::PC);
553 for (int Register = GPRsNoLRSP.find_first(); Register != -1;
554 Register = GPRsNoLRSP.find_next(Register)) {
555 if (!UsedRegs.contains(Register)) {
556 // Remember the first pop-friendly register and exit.
557 if (PopFriendly.test(Register)) {
558 PopReg = Register;
559 TemporaryReg = 0;
560 break;
Quentin Colombet71a71482015-07-20 21:42:14 +0000561 }
Quentin Colombet48b77202015-07-22 16:34:37 +0000562 // Otherwise, remember that the register will be available to
563 // save a pop-friendly register.
564 TemporaryReg = Register;
Jonathan Roelofsef84bda2014-08-05 21:32:21 +0000565 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000566 }
Quentin Colombet48b77202015-07-22 16:34:37 +0000567
568 if (!DoIt && !PopReg && !TemporaryReg)
569 return false;
570
571 assert((PopReg || TemporaryReg) && "Cannot get LR");
572
573 if (TemporaryReg) {
574 assert(!PopReg && "Unnecessary MOV is about to be inserted");
575 PopReg = PopFriendly.find_first();
Diana Picus4f8c3e12017-01-13 09:37:56 +0000576 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
577 .addReg(TemporaryReg, RegState::Define)
578 .addReg(PopReg, RegState::Kill)
579 .add(predOps(ARMCC::AL));
Quentin Colombet48b77202015-07-22 16:34:37 +0000580 }
581
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000582 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPOP_RET) {
Artyom Skrobov0a37b802015-12-08 19:59:01 +0000583 // We couldn't use the direct restoration above, so
584 // perform the opposite conversion: tPOP_RET to tPOP.
585 MachineInstrBuilder MIB =
Diana Picus4f8c3e12017-01-13 09:37:56 +0000586 BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP))
587 .add(predOps(ARMCC::AL));
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000588 bool Popped = false;
Artyom Skrobov0a37b802015-12-08 19:59:01 +0000589 for (auto MO: MBBI->operands())
590 if (MO.isReg() && (MO.isImplicit() || MO.isDef()) &&
591 MO.getReg() != ARM::PC) {
Diana Picus116bbab2017-01-13 09:58:52 +0000592 MIB.add(MO);
Artyom Skrobov0a37b802015-12-08 19:59:01 +0000593 if (!MO.isImplicit())
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000594 Popped = true;
Artyom Skrobov0a37b802015-12-08 19:59:01 +0000595 }
596 // Is there anything left to pop?
597 if (!Popped)
598 MBB.erase(MIB.getInstr());
599 // Erase the old instruction.
600 MBB.erase(MBBI);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000601 MBBI = BuildMI(MBB, MBB.end(), dl, TII.get(ARM::tBX_RET))
602 .add(predOps(ARMCC::AL));
Artyom Skrobov5d1f2522015-12-01 19:25:11 +0000603 }
604
Quentin Colombet48b77202015-07-22 16:34:37 +0000605 assert(PopReg && "Do not know how to get LR");
Diana Picus4f8c3e12017-01-13 09:37:56 +0000606 BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP))
607 .add(predOps(ARMCC::AL))
Quentin Colombet48b77202015-07-22 16:34:37 +0000608 .addReg(PopReg, RegState::Define);
609
610 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
611
Diana Picus4f8c3e12017-01-13 09:37:56 +0000612 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
613 .addReg(ARM::LR, RegState::Define)
614 .addReg(PopReg, RegState::Kill)
615 .add(predOps(ARMCC::AL));
Quentin Colombet48b77202015-07-22 16:34:37 +0000616
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000617 if (TemporaryReg)
Diana Picus4f8c3e12017-01-13 09:37:56 +0000618 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
619 .addReg(PopReg, RegState::Define)
620 .addReg(TemporaryReg, RegState::Kill)
621 .add(predOps(ARMCC::AL));
Quentin Colombet48b77202015-07-22 16:34:37 +0000622
623 return true;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000624}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000625
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000626// Return the first iteraror after CurrentReg which is present in EnabledRegs,
627// or OrderEnd if no further registers are in that set. This does not advance
628// the iterator fiorst, so returns CurrentReg if it is in EnabledRegs.
629template <unsigned SetSize>
630static const unsigned *
631findNextOrderedReg(const unsigned *CurrentReg,
632 SmallSet<unsigned, SetSize> &EnabledRegs,
633 const unsigned *OrderEnd) {
634 while (CurrentReg != OrderEnd && !EnabledRegs.count(*CurrentReg))
635 ++CurrentReg;
636 return CurrentReg;
637}
638
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000639bool Thumb1FrameLowering::
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000640spillCalleeSavedRegisters(MachineBasicBlock &MBB,
641 MachineBasicBlock::iterator MI,
642 const std::vector<CalleeSavedInfo> &CSI,
643 const TargetRegisterInfo *TRI) const {
644 if (CSI.empty())
645 return false;
646
Tim Northover775aaeb2015-11-05 21:54:58 +0000647 DebugLoc DL;
648 const TargetInstrInfo &TII = *STI.getInstrInfo();
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000649 MachineFunction &MF = *MBB.getParent();
650 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
651 MF.getSubtarget().getRegisterInfo());
Tim Northover775aaeb2015-11-05 21:54:58 +0000652
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000653 SmallSet<unsigned, 9> LoRegsToSave; // r0-r7, lr
654 SmallSet<unsigned, 4> HiRegsToSave; // r8-r11
655 SmallSet<unsigned, 9> CopyRegs; // Registers which can be used after pushing
656 // LoRegs for saving HiRegs.
657
Tim Northover775aaeb2015-11-05 21:54:58 +0000658 for (unsigned i = CSI.size(); i != 0; --i) {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000659 unsigned Reg = CSI[i-1].getReg();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000660
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000661 if (ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) {
662 LoRegsToSave.insert(Reg);
663 } else if (ARM::hGPRRegClass.contains(Reg) && Reg != ARM::LR) {
664 HiRegsToSave.insert(Reg);
665 } else {
666 llvm_unreachable("callee-saved register of unexpected class");
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000667 }
668
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000669 if ((ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) &&
670 !MF.getRegInfo().isLiveIn(Reg) &&
671 !(hasFP(MF) && Reg == RegInfo->getFrameRegister(MF)))
672 CopyRegs.insert(Reg);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000673 }
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000674
675 // Unused argument registers can be used for the high register saving.
676 for (unsigned ArgReg : {ARM::R0, ARM::R1, ARM::R2, ARM::R3})
677 if (!MF.getRegInfo().isLiveIn(ArgReg))
678 CopyRegs.insert(ArgReg);
679
680 // Push the low registers and lr
681 if (!LoRegsToSave.empty()) {
Diana Picus4f8c3e12017-01-13 09:37:56 +0000682 MachineInstrBuilder MIB =
683 BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH)).add(predOps(ARMCC::AL));
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000684 for (unsigned Reg : {ARM::R4, ARM::R5, ARM::R6, ARM::R7, ARM::LR}) {
685 if (LoRegsToSave.count(Reg)) {
686 bool isKill = !MF.getRegInfo().isLiveIn(Reg);
687 if (isKill)
688 MBB.addLiveIn(Reg);
689
690 MIB.addReg(Reg, getKillRegState(isKill));
691 }
692 }
693 MIB.setMIFlags(MachineInstr::FrameSetup);
694 }
695
696 // Push the high registers. There are no store instructions that can access
697 // these registers directly, so we have to move them to low registers, and
698 // push them. This might take multiple pushes, as it is possible for there to
699 // be fewer low registers available than high registers which need saving.
700
701 // These are in reverse order so that in the case where we need to use
702 // multiple PUSH instructions, the order of the registers on the stack still
703 // matches the unwind info. They need to be swicthed back to ascending order
704 // before adding to the PUSH instruction.
705 static const unsigned AllCopyRegs[] = {ARM::LR, ARM::R7, ARM::R6,
706 ARM::R5, ARM::R4, ARM::R3,
707 ARM::R2, ARM::R1, ARM::R0};
708 static const unsigned AllHighRegs[] = {ARM::R11, ARM::R10, ARM::R9, ARM::R8};
709
710 const unsigned *AllCopyRegsEnd = std::end(AllCopyRegs);
711 const unsigned *AllHighRegsEnd = std::end(AllHighRegs);
712
713 // Find the first register to save.
714 const unsigned *HiRegToSave = findNextOrderedReg(
715 std::begin(AllHighRegs), HiRegsToSave, AllHighRegsEnd);
716
717 while (HiRegToSave != AllHighRegsEnd) {
718 // Find the first low register to use.
719 const unsigned *CopyReg =
720 findNextOrderedReg(std::begin(AllCopyRegs), CopyRegs, AllCopyRegsEnd);
721
722 // Create the PUSH, but don't insert it yet (the MOVs need to come first).
Diana Picus4f8c3e12017-01-13 09:37:56 +0000723 MachineInstrBuilder PushMIB =
724 BuildMI(MF, DL, TII.get(ARM::tPUSH)).add(predOps(ARMCC::AL));
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000725
726 SmallVector<unsigned, 4> RegsToPush;
727 while (HiRegToSave != AllHighRegsEnd && CopyReg != AllCopyRegsEnd) {
728 if (HiRegsToSave.count(*HiRegToSave)) {
729 bool isKill = !MF.getRegInfo().isLiveIn(*HiRegToSave);
730 if (isKill)
731 MBB.addLiveIn(*HiRegToSave);
732
733 // Emit a MOV from the high reg to the low reg.
Diana Picus4f8c3e12017-01-13 09:37:56 +0000734 BuildMI(MBB, MI, DL, TII.get(ARM::tMOVr))
735 .addReg(*CopyReg, RegState::Define)
736 .addReg(*HiRegToSave, getKillRegState(isKill))
737 .add(predOps(ARMCC::AL));
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000738
739 // Record the register that must be added to the PUSH.
740 RegsToPush.push_back(*CopyReg);
741
742 CopyReg = findNextOrderedReg(++CopyReg, CopyRegs, AllCopyRegsEnd);
743 HiRegToSave =
744 findNextOrderedReg(++HiRegToSave, HiRegsToSave, AllHighRegsEnd);
745 }
746 }
747
748 // Add the low registers to the PUSH, in ascending order.
749 for (unsigned Reg : reverse(RegsToPush))
750 PushMIB.addReg(Reg, RegState::Kill);
751
752 // Insert the PUSH instruction after the MOVs.
753 MBB.insert(MI, PushMIB);
754 }
755
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000756 return true;
757}
758
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000759bool Thumb1FrameLowering::
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000760restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
761 MachineBasicBlock::iterator MI,
762 const std::vector<CalleeSavedInfo> &CSI,
763 const TargetRegisterInfo *TRI) const {
764 if (CSI.empty())
765 return false;
766
767 MachineFunction &MF = *MBB.getParent();
768 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopher1b21f002015-01-29 00:19:33 +0000769 const TargetInstrInfo &TII = *STI.getInstrInfo();
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000770 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
771 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000772
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000773 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Quentin Colombet48b77202015-07-22 16:34:37 +0000774 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000775
776 SmallSet<unsigned, 9> LoRegsToRestore;
777 SmallSet<unsigned, 4> HiRegsToRestore;
778 // Low registers (r0-r7) which can be used to restore the high registers.
779 SmallSet<unsigned, 9> CopyRegs;
780
781 for (CalleeSavedInfo I : CSI) {
782 unsigned Reg = I.getReg();
783
784 if (ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) {
785 LoRegsToRestore.insert(Reg);
786 } else if (ARM::hGPRRegClass.contains(Reg) && Reg != ARM::LR) {
787 HiRegsToRestore.insert(Reg);
788 } else {
789 llvm_unreachable("callee-saved register of unexpected class");
790 }
791
792 // If this is a low register not used as the frame pointer, we may want to
793 // use it for restoring the high registers.
794 if ((ARM::tGPRRegClass.contains(Reg)) &&
795 !(hasFP(MF) && Reg == RegInfo->getFrameRegister(MF)))
796 CopyRegs.insert(Reg);
797 }
798
799 // If this is a return block, we may be able to use some unused return value
800 // registers for restoring the high regs.
801 auto Terminator = MBB.getFirstTerminator();
802 if (Terminator != MBB.end() && Terminator->getOpcode() == ARM::tBX_RET) {
803 CopyRegs.insert(ARM::R0);
804 CopyRegs.insert(ARM::R1);
805 CopyRegs.insert(ARM::R2);
806 CopyRegs.insert(ARM::R3);
807 for (auto Op : Terminator->implicit_operands()) {
808 if (Op.isReg())
809 CopyRegs.erase(Op.getReg());
810 }
811 }
812
813 static const unsigned AllCopyRegs[] = {ARM::R0, ARM::R1, ARM::R2, ARM::R3,
814 ARM::R4, ARM::R5, ARM::R6, ARM::R7};
815 static const unsigned AllHighRegs[] = {ARM::R8, ARM::R9, ARM::R10, ARM::R11};
816
817 const unsigned *AllCopyRegsEnd = std::end(AllCopyRegs);
818 const unsigned *AllHighRegsEnd = std::end(AllHighRegs);
819
820 // Find the first register to restore.
821 auto HiRegToRestore = findNextOrderedReg(std::begin(AllHighRegs),
822 HiRegsToRestore, AllHighRegsEnd);
823
824 while (HiRegToRestore != AllHighRegsEnd) {
825 assert(!CopyRegs.empty());
826 // Find the first low register to use.
827 auto CopyReg =
828 findNextOrderedReg(std::begin(AllCopyRegs), CopyRegs, AllCopyRegsEnd);
829
830 // Create the POP instruction.
Diana Picus4f8c3e12017-01-13 09:37:56 +0000831 MachineInstrBuilder PopMIB =
832 BuildMI(MBB, MI, DL, TII.get(ARM::tPOP)).add(predOps(ARMCC::AL));
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000833
834 while (HiRegToRestore != AllHighRegsEnd && CopyReg != AllCopyRegsEnd) {
835 // Add the low register to the POP.
836 PopMIB.addReg(*CopyReg, RegState::Define);
837
838 // Create the MOV from low to high register.
Diana Picus4f8c3e12017-01-13 09:37:56 +0000839 BuildMI(MBB, MI, DL, TII.get(ARM::tMOVr))
840 .addReg(*HiRegToRestore, RegState::Define)
841 .addReg(*CopyReg, RegState::Kill)
842 .add(predOps(ARMCC::AL));
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000843
844 CopyReg = findNextOrderedReg(++CopyReg, CopyRegs, AllCopyRegsEnd);
845 HiRegToRestore =
846 findNextOrderedReg(++HiRegToRestore, HiRegsToRestore, AllHighRegsEnd);
847 }
848 }
849
Diana Picus4f8c3e12017-01-13 09:37:56 +0000850 MachineInstrBuilder MIB =
851 BuildMI(MF, DL, TII.get(ARM::tPOP)).add(predOps(ARMCC::AL));
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000852
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000853 bool NeedsPop = false;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000854 for (unsigned i = CSI.size(); i != 0; --i) {
855 unsigned Reg = CSI[i-1].getReg();
Reid Klecknerbdfc05f2016-10-11 21:14:03 +0000856
857 // High registers (excluding lr) have already been dealt with
858 if (!(ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR))
859 continue;
860
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000861 if (Reg == ARM::LR) {
862 if (MBB.succ_empty()) {
863 // Special epilogue for vararg functions. See emitEpilogue
864 if (isVarArg)
865 continue;
866 // ARMv4T requires BX, see emitEpilogue
867 if (!STI.hasV5TOps())
868 continue;
869 Reg = ARM::PC;
870 (*MIB).setDesc(TII.get(ARM::tPOP_RET));
871 if (MI != MBB.end())
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000872 MIB.copyImplicitOps(*MI);
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000873 MI = MBB.erase(MI);
874 } else
875 // LR may only be popped into PC, as part of return sequence.
876 // If this isn't the return sequence, we'll need emitPopSpecialFixUp
877 // to restore LR the hard way.
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000878 continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000879 }
880 MIB.addReg(Reg, getDefRegState(true));
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000881 NeedsPop = true;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000882 }
883
884 // It's illegal to emit pop instruction without operands.
Artyom Skrobov2aca0c62015-12-28 21:40:45 +0000885 if (NeedsPop)
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000886 MBB.insert(MI, &*MIB);
887 else
888 MF.DeleteMachineInstr(MIB);
889
890 return true;
891}