1 //===- Thumb1FrameLowering.cpp - Thumb1 Frame Information -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the Thumb1 implementation of TargetFrameLowering class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "Thumb1FrameLowering.h"
14 #include "ARMBaseInstrInfo.h"
15 #include "ARMBaseRegisterInfo.h"
16 #include "ARMMachineFunctionInfo.h"
17 #include "ARMSubtarget.h"
18 #include "Thumb1InstrInfo.h"
19 #include "ThumbRegisterInfo.h"
20 #include "Utils/ARMBaseInfo.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/CodeGen/LivePhysRegs.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineOperand.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/TargetInstrInfo.h"
34 #include "llvm/CodeGen/TargetOpcodes.h"
35 #include "llvm/CodeGen/TargetSubtargetInfo.h"
36 #include "llvm/IR/DebugLoc.h"
37 #include "llvm/MC/MCContext.h"
38 #include "llvm/MC/MCDwarf.h"
39 #include "llvm/MC/MCRegisterInfo.h"
40 #include "llvm/Support/Compiler.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/MathExtras.h"
43 #include <bitset>
44 #include <cassert>
45 #include <iterator>
46 #include <vector>
47 
48 using namespace llvm;
49 
50 Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti)
51     : ARMFrameLowering(sti) {}
52 
53 bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
54   const MachineFrameInfo &MFI = MF.getFrameInfo();
55   unsigned CFSize = MFI.getMaxCallFrameSize();
56   // It's not always a good idea to include the call frame as part of the
57   // stack frame. ARM (especially Thumb) has small immediate offset to
58   // address the stack frame. So a large call frame can cause poor codegen
59   // and may even makes it impossible to scavenge a register.
60   if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
61     return false;
62 
63   return !MFI.hasVarSizedObjects();
64 }
65 
66 static void
67 emitPrologueEpilogueSPUpdate(MachineBasicBlock &MBB,
68                              MachineBasicBlock::iterator &MBBI,
69                              const TargetInstrInfo &TII, const DebugLoc &dl,
70                              const ThumbRegisterInfo &MRI, int NumBytes,
71                              unsigned ScratchReg, unsigned MIFlags) {
72   // If it would take more than three instructions to adjust the stack pointer
73   // using tADDspi/tSUBspi, load an immediate instead.
74   if (std::abs(NumBytes) > 508 * 3) {
75     // We use a different codepath here from the normal
76     // emitThumbRegPlusImmediate so we don't have to deal with register
77     // scavenging. (Scavenging could try to use the emergency spill slot
78     // before we've actually finished setting up the stack.)
79     if (ScratchReg == ARM::NoRegister)
80       report_fatal_error("Failed to emit Thumb1 stack adjustment");
81     MachineFunction &MF = *MBB.getParent();
82     const ARMSubtarget &ST = MF.getSubtarget<ARMSubtarget>();
83     if (ST.genExecuteOnly()) {
84       BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ScratchReg)
85         .addImm(NumBytes).setMIFlags(MIFlags);
86     } else {
87       MRI.emitLoadConstPool(MBB, MBBI, dl, ScratchReg, 0, NumBytes, ARMCC::AL,
88                             0, MIFlags);
89     }
90     BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDhirr), ARM::SP)
91         .addReg(ARM::SP)
92         .addReg(ScratchReg, RegState::Kill)
93         .add(predOps(ARMCC::AL))
94         .setMIFlags(MIFlags);
95     return;
96   }
97   // FIXME: This is assuming the heuristics in emitThumbRegPlusImmediate
98   // won't change.
99   emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
100                             MRI, MIFlags);
101 
102 }
103 
104 static void emitCallSPUpdate(MachineBasicBlock &MBB,
105                              MachineBasicBlock::iterator &MBBI,
106                              const TargetInstrInfo &TII, const DebugLoc &dl,
107                              const ThumbRegisterInfo &MRI, int NumBytes,
108                              unsigned MIFlags = MachineInstr::NoFlags) {
109   emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
110                             MRI, MIFlags);
111 }
112 
113 
114 MachineBasicBlock::iterator Thumb1FrameLowering::
115 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
116                               MachineBasicBlock::iterator I) const {
117   const Thumb1InstrInfo &TII =
118       *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
119   const ThumbRegisterInfo *RegInfo =
120       static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
121   if (!hasReservedCallFrame(MF)) {
122     // If we have alloca, convert as follows:
123     // ADJCALLSTACKDOWN -> sub, sp, sp, amount
124     // ADJCALLSTACKUP   -> add, sp, sp, amount
125     MachineInstr &Old = *I;
126     DebugLoc dl = Old.getDebugLoc();
127     unsigned Amount = TII.getFrameSize(Old);
128     if (Amount != 0) {
129       // We need to keep the stack aligned properly.  To do this, we round the
130       // amount of space needed for the outgoing arguments up to the next
131       // alignment boundary.
132       Amount = alignTo(Amount, getStackAlign());
133 
134       // Replace the pseudo instruction with a new instruction...
135       unsigned Opc = Old.getOpcode();
136       if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
137         emitCallSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount);
138       } else {
139         assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
140         emitCallSPUpdate(MBB, I, TII, dl, *RegInfo, Amount);
141       }
142     }
143   }
144   return MBB.erase(I);
145 }
146 
147 void Thumb1FrameLowering::emitPrologue(MachineFunction &MF,
148                                        MachineBasicBlock &MBB) const {
149   MachineBasicBlock::iterator MBBI = MBB.begin();
150   MachineFrameInfo &MFI = MF.getFrameInfo();
151   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
152   MachineModuleInfo &MMI = MF.getMMI();
153   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
154   const ThumbRegisterInfo *RegInfo =
155       static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
156   const Thumb1InstrInfo &TII =
157       *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
158 
159   unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
160   unsigned NumBytes = MFI.getStackSize();
161   assert(NumBytes >= ArgRegsSaveSize &&
162          "ArgRegsSaveSize is included in NumBytes");
163   const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
164 
165   // Debug location must be unknown since the first debug location is used
166   // to determine the end of the prologue.
167   DebugLoc dl;
168 
169   Register FramePtr = RegInfo->getFrameRegister(MF);
170   Register BasePtr = RegInfo->getBaseRegister();
171   int CFAOffset = 0;
172 
173   // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
174   NumBytes = (NumBytes + 3) & ~3;
175   MFI.setStackSize(NumBytes);
176 
177   // Determine the sizes of each callee-save spill areas and record which frame
178   // belongs to which callee-save spill areas.
179   unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
180   int FramePtrSpillFI = 0;
181 
182   if (ArgRegsSaveSize) {
183     emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
184                                  ARM::NoRegister, MachineInstr::FrameSetup);
185     CFAOffset += ArgRegsSaveSize;
186     unsigned CFIIndex =
187         MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
188     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
189         .addCFIIndex(CFIIndex)
190         .setMIFlags(MachineInstr::FrameSetup);
191   }
192 
193   if (!AFI->hasStackFrame()) {
194     if (NumBytes - ArgRegsSaveSize != 0) {
195       emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo,
196                                    -(NumBytes - ArgRegsSaveSize),
197                                    ARM::NoRegister, MachineInstr::FrameSetup);
198       CFAOffset += NumBytes - ArgRegsSaveSize;
199       unsigned CFIIndex = MF.addFrameInst(
200           MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
201       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
202           .addCFIIndex(CFIIndex)
203           .setMIFlags(MachineInstr::FrameSetup);
204     }
205     return;
206   }
207 
208   for (const CalleeSavedInfo &I : CSI) {
209     Register Reg = I.getReg();
210     int FI = I.getFrameIdx();
211     switch (Reg) {
212     case ARM::R8:
213     case ARM::R9:
214     case ARM::R10:
215     case ARM::R11:
216       if (STI.splitFramePushPop(MF)) {
217         GPRCS2Size += 4;
218         break;
219       }
220       LLVM_FALLTHROUGH;
221     case ARM::R4:
222     case ARM::R5:
223     case ARM::R6:
224     case ARM::R7:
225     case ARM::LR:
226       if (Reg == FramePtr)
227         FramePtrSpillFI = FI;
228       GPRCS1Size += 4;
229       break;
230     default:
231       DPRCSSize += 8;
232     }
233   }
234 
235   if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
236     ++MBBI;
237   }
238 
239   // Determine starting offsets of spill areas.
240   unsigned DPRCSOffset  = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
241   unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
242   unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
243   bool HasFP = hasFP(MF);
244   if (HasFP)
245     AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
246                                 NumBytes);
247   AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
248   AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
249   AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
250   NumBytes = DPRCSOffset;
251 
252   int FramePtrOffsetInBlock = 0;
253   unsigned adjustedGPRCS1Size = GPRCS1Size;
254   if (GPRCS1Size > 0 && GPRCS2Size == 0 &&
255       tryFoldSPUpdateIntoPushPop(STI, MF, &*std::prev(MBBI), NumBytes)) {
256     FramePtrOffsetInBlock = NumBytes;
257     adjustedGPRCS1Size += NumBytes;
258     NumBytes = 0;
259   }
260 
261   if (adjustedGPRCS1Size) {
262     CFAOffset += adjustedGPRCS1Size;
263     unsigned CFIIndex =
264         MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
265     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
266         .addCFIIndex(CFIIndex)
267         .setMIFlags(MachineInstr::FrameSetup);
268   }
269   for (const CalleeSavedInfo &I : CSI) {
270     Register Reg = I.getReg();
271     int FI = I.getFrameIdx();
272     switch (Reg) {
273     case ARM::R8:
274     case ARM::R9:
275     case ARM::R10:
276     case ARM::R11:
277     case ARM::R12:
278       if (STI.splitFramePushPop(MF))
279         break;
280       LLVM_FALLTHROUGH;
281     case ARM::R0:
282     case ARM::R1:
283     case ARM::R2:
284     case ARM::R3:
285     case ARM::R4:
286     case ARM::R5:
287     case ARM::R6:
288     case ARM::R7:
289     case ARM::LR:
290       unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
291           nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
292       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
293           .addCFIIndex(CFIIndex)
294           .setMIFlags(MachineInstr::FrameSetup);
295       break;
296     }
297   }
298 
299   // Adjust FP so it point to the stack slot that contains the previous FP.
300   if (HasFP) {
301     FramePtrOffsetInBlock +=
302         MFI.getObjectOffset(FramePtrSpillFI) + GPRCS1Size + ArgRegsSaveSize;
303     BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
304         .addReg(ARM::SP)
305         .addImm(FramePtrOffsetInBlock / 4)
306         .setMIFlags(MachineInstr::FrameSetup)
307         .add(predOps(ARMCC::AL));
308     if(FramePtrOffsetInBlock) {
309       CFAOffset -= FramePtrOffsetInBlock;
310       unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
311           nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
312       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
313           .addCFIIndex(CFIIndex)
314           .setMIFlags(MachineInstr::FrameSetup);
315     } else {
316       unsigned CFIIndex =
317           MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
318               nullptr, MRI->getDwarfRegNum(FramePtr, true)));
319       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
320           .addCFIIndex(CFIIndex)
321           .setMIFlags(MachineInstr::FrameSetup);
322     }
323     if (NumBytes > 508)
324       // If offset is > 508 then sp cannot be adjusted in a single instruction,
325       // try restoring from fp instead.
326       AFI->setShouldRestoreSPFromFP(true);
327   }
328 
329   // Skip past the spilling of r8-r11, which could consist of multiple tPUSH
330   // and tMOVr instructions. We don't need to add any call frame information
331   // in-between these instructions, because they do not modify the high
332   // registers.
333   while (true) {
334     MachineBasicBlock::iterator OldMBBI = MBBI;
335     // Skip a run of tMOVr instructions
336     while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tMOVr)
337       MBBI++;
338     if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
339       MBBI++;
340     } else {
341       // We have reached an instruction which is not a push, so the previous
342       // run of tMOVr instructions (which may have been empty) was not part of
343       // the prologue. Reset MBBI back to the last PUSH of the prologue.
344       MBBI = OldMBBI;
345       break;
346     }
347   }
348 
349   // Emit call frame information for the callee-saved high registers.
350   for (auto &I : CSI) {
351     Register Reg = I.getReg();
352     int FI = I.getFrameIdx();
353     switch (Reg) {
354     case ARM::R8:
355     case ARM::R9:
356     case ARM::R10:
357     case ARM::R11:
358     case ARM::R12: {
359       unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
360           nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
361       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
362           .addCFIIndex(CFIIndex)
363           .setMIFlags(MachineInstr::FrameSetup);
364       break;
365     }
366     default:
367       break;
368     }
369   }
370 
371   if (NumBytes) {
372     // Insert it after all the callee-save spills.
373     //
374     // For a large stack frame, we might need a scratch register to store
375     // the size of the frame.  We know all callee-save registers are free
376     // at this point in the prologue, so pick one.
377     unsigned ScratchRegister = ARM::NoRegister;
378     for (auto &I : CSI) {
379       Register Reg = I.getReg();
380       if (isARMLowRegister(Reg) && !(HasFP && Reg == FramePtr)) {
381         ScratchRegister = Reg;
382         break;
383       }
384     }
385     emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
386                                  ScratchRegister, MachineInstr::FrameSetup);
387     if (!HasFP) {
388       CFAOffset += NumBytes;
389       unsigned CFIIndex = MF.addFrameInst(
390           MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
391       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
392           .addCFIIndex(CFIIndex)
393           .setMIFlags(MachineInstr::FrameSetup);
394     }
395   }
396 
397   if (STI.isTargetELF() && HasFP)
398     MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
399                             AFI->getFramePtrSpillOffset());
400 
401   AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
402   AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
403   AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
404 
405   if (RegInfo->hasStackRealignment(MF)) {
406     const unsigned NrBitsToZero = Log2(MFI.getMaxAlign());
407     // Emit the following sequence, using R4 as a temporary, since we cannot use
408     // SP as a source or destination register for the shifts:
409     // mov  r4, sp
410     // lsrs r4, r4, #NrBitsToZero
411     // lsls r4, r4, #NrBitsToZero
412     // mov  sp, r4
413     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
414       .addReg(ARM::SP, RegState::Kill)
415       .add(predOps(ARMCC::AL));
416 
417     BuildMI(MBB, MBBI, dl, TII.get(ARM::tLSRri), ARM::R4)
418       .addDef(ARM::CPSR)
419       .addReg(ARM::R4, RegState::Kill)
420       .addImm(NrBitsToZero)
421       .add(predOps(ARMCC::AL));
422 
423     BuildMI(MBB, MBBI, dl, TII.get(ARM::tLSLri), ARM::R4)
424       .addDef(ARM::CPSR)
425       .addReg(ARM::R4, RegState::Kill)
426       .addImm(NrBitsToZero)
427       .add(predOps(ARMCC::AL));
428 
429     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
430       .addReg(ARM::R4, RegState::Kill)
431       .add(predOps(ARMCC::AL));
432 
433     AFI->setShouldRestoreSPFromFP(true);
434   }
435 
436   // If we need a base pointer, set it up here. It's whatever the value
437   // of the stack pointer is at this point. Any variable size objects
438   // will be allocated after this, so we can still use the base pointer
439   // to reference locals.
440   if (RegInfo->hasBasePointer(MF))
441     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
442         .addReg(ARM::SP)
443         .add(predOps(ARMCC::AL));
444 
445   // If the frame has variable sized objects then the epilogue must restore
446   // the sp from fp. We can assume there's an FP here since hasFP already
447   // checks for hasVarSizedObjects.
448   if (MFI.hasVarSizedObjects())
449     AFI->setShouldRestoreSPFromFP(true);
450 
451   // In some cases, virtual registers have been introduced, e.g. by uses of
452   // emitThumbRegPlusImmInReg.
453   MF.getProperties().reset(MachineFunctionProperties::Property::NoVRegs);
454 }
455 
456 void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
457                                    MachineBasicBlock &MBB) const {
458   MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
459   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
460   MachineFrameInfo &MFI = MF.getFrameInfo();
461   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
462   const ThumbRegisterInfo *RegInfo =
463       static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
464   const Thumb1InstrInfo &TII =
465       *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
466 
467   unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
468   int NumBytes = (int)MFI.getStackSize();
469   assert((unsigned)NumBytes >= ArgRegsSaveSize &&
470          "ArgRegsSaveSize is included in NumBytes");
471   Register FramePtr = RegInfo->getFrameRegister(MF);
472 
473   if (!AFI->hasStackFrame()) {
474     if (NumBytes - ArgRegsSaveSize != 0)
475       emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo,
476                                    NumBytes - ArgRegsSaveSize, ARM::NoRegister,
477                                    MachineInstr::FrameDestroy);
478   } else {
479     // Unwind MBBI to point to first LDR / VLDRD.
480     if (MBBI != MBB.begin()) {
481       do
482         --MBBI;
483       while (MBBI != MBB.begin() && MBBI->getFlag(MachineInstr::FrameDestroy));
484       if (!MBBI->getFlag(MachineInstr::FrameDestroy))
485         ++MBBI;
486     }
487 
488     // Move SP to start of FP callee save spill area.
489     NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
490                  AFI->getGPRCalleeSavedArea2Size() +
491                  AFI->getDPRCalleeSavedAreaSize() +
492                  ArgRegsSaveSize);
493 
494     if (AFI->shouldRestoreSPFromFP()) {
495       NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
496       // Reset SP based on frame pointer only if the stack frame extends beyond
497       // frame pointer stack slot, the target is ELF and the function has FP, or
498       // the target uses var sized objects.
499       if (NumBytes) {
500         assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
501                "No scratch register to restore SP from FP!");
502         emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
503                                   TII, *RegInfo, MachineInstr::FrameDestroy);
504         BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
505             .addReg(ARM::R4)
506             .add(predOps(ARMCC::AL))
507             .setMIFlag(MachineInstr::FrameDestroy);
508       } else
509         BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
510             .addReg(FramePtr)
511             .add(predOps(ARMCC::AL))
512             .setMIFlag(MachineInstr::FrameDestroy);
513     } else {
514       // For a large stack frame, we might need a scratch register to store
515       // the size of the frame.  We know all callee-save registers are free
516       // at this point in the epilogue, so pick one.
517       unsigned ScratchRegister = ARM::NoRegister;
518       bool HasFP = hasFP(MF);
519       for (auto &I : MFI.getCalleeSavedInfo()) {
520         Register Reg = I.getReg();
521         if (isARMLowRegister(Reg) && !(HasFP && Reg == FramePtr)) {
522           ScratchRegister = Reg;
523           break;
524         }
525       }
526       if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET &&
527           &MBB.front() != &*MBBI && std::prev(MBBI)->getOpcode() == ARM::tPOP) {
528         MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
529         if (!tryFoldSPUpdateIntoPushPop(STI, MF, &*PMBBI, NumBytes))
530           emitPrologueEpilogueSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes,
531                                        ScratchRegister, MachineInstr::FrameDestroy);
532       } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
533         emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes,
534                                      ScratchRegister, MachineInstr::FrameDestroy);
535     }
536   }
537 
538   if (needPopSpecialFixUp(MF)) {
539     bool Done = emitPopSpecialFixUp(MBB, /* DoIt */ true);
540     (void)Done;
541     assert(Done && "Emission of the special fixup failed!?");
542   }
543 }
544 
545 bool Thumb1FrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
546   if (!needPopSpecialFixUp(*MBB.getParent()))
547     return true;
548 
549   MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
550   return emitPopSpecialFixUp(*TmpMBB, /* DoIt */ false);
551 }
552 
553 bool Thumb1FrameLowering::needPopSpecialFixUp(const MachineFunction &MF) const {
554   ARMFunctionInfo *AFI =
555       const_cast<MachineFunction *>(&MF)->getInfo<ARMFunctionInfo>();
556   if (AFI->getArgRegsSaveSize())
557     return true;
558 
559   // LR cannot be encoded with Thumb1, i.e., it requires a special fix-up.
560   for (const CalleeSavedInfo &CSI : MF.getFrameInfo().getCalleeSavedInfo())
561     if (CSI.getReg() == ARM::LR)
562       return true;
563 
564   return false;
565 }
566 
567 static void findTemporariesForLR(const BitVector &GPRsNoLRSP,
568                                  const BitVector &PopFriendly,
569                                  const LivePhysRegs &UsedRegs, unsigned &PopReg,
570                                  unsigned &TmpReg, MachineRegisterInfo &MRI) {
571   PopReg = TmpReg = 0;
572   for (auto Reg : GPRsNoLRSP.set_bits()) {
573     if (UsedRegs.available(MRI, Reg)) {
574       // Remember the first pop-friendly register and exit.
575       if (PopFriendly.test(Reg)) {
576         PopReg = Reg;
577         TmpReg = 0;
578         break;
579       }
580       // Otherwise, remember that the register will be available to
581       // save a pop-friendly register.
582       TmpReg = Reg;
583     }
584   }
585 }
586 
587 bool Thumb1FrameLowering::emitPopSpecialFixUp(MachineBasicBlock &MBB,
588                                               bool DoIt) const {
589   MachineFunction &MF = *MBB.getParent();
590   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
591   unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
592   const TargetInstrInfo &TII = *STI.getInstrInfo();
593   const ThumbRegisterInfo *RegInfo =
594       static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
595 
596   // If MBBI is a return instruction, or is a tPOP followed by a return
597   // instruction in the successor BB, we may be able to directly restore
598   // LR in the PC.
599   // This is only possible with v5T ops (v4T can't change the Thumb bit via
600   // a POP PC instruction), and only if we do not need to emit any SP update.
601   // Otherwise, we need a temporary register to pop the value
602   // and copy that value into LR.
603   auto MBBI = MBB.getFirstTerminator();
604   bool CanRestoreDirectly = STI.hasV5TOps() && !ArgRegsSaveSize;
605   if (CanRestoreDirectly) {
606     if (MBBI != MBB.end() && MBBI->getOpcode() != ARM::tB)
607       CanRestoreDirectly = (MBBI->getOpcode() == ARM::tBX_RET ||
608                             MBBI->getOpcode() == ARM::tPOP_RET);
609     else {
610       auto MBBI_prev = MBBI;
611       MBBI_prev--;
612       assert(MBBI_prev->getOpcode() == ARM::tPOP);
613       assert(MBB.succ_size() == 1);
614       if ((*MBB.succ_begin())->begin()->getOpcode() == ARM::tBX_RET)
615         MBBI = MBBI_prev; // Replace the final tPOP with a tPOP_RET.
616       else
617         CanRestoreDirectly = false;
618     }
619   }
620 
621   if (CanRestoreDirectly) {
622     if (!DoIt || MBBI->getOpcode() == ARM::tPOP_RET)
623       return true;
624     MachineInstrBuilder MIB =
625         BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP_RET))
626             .add(predOps(ARMCC::AL))
627             .setMIFlag(MachineInstr::FrameDestroy);
628     // Copy implicit ops and popped registers, if any.
629     for (auto MO: MBBI->operands())
630       if (MO.isReg() && (MO.isImplicit() || MO.isDef()))
631         MIB.add(MO);
632     MIB.addReg(ARM::PC, RegState::Define);
633     // Erase the old instruction (tBX_RET or tPOP).
634     MBB.erase(MBBI);
635     return true;
636   }
637 
638   // Look for a temporary register to use.
639   // First, compute the liveness information.
640   const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
641   LivePhysRegs UsedRegs(TRI);
642   UsedRegs.addLiveOuts(MBB);
643   // The semantic of pristines changed recently and now,
644   // the callee-saved registers that are touched in the function
645   // are not part of the pristines set anymore.
646   // Add those callee-saved now.
647   const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
648   for (unsigned i = 0; CSRegs[i]; ++i)
649     UsedRegs.addReg(CSRegs[i]);
650 
651   DebugLoc dl = DebugLoc();
652   if (MBBI != MBB.end()) {
653     dl = MBBI->getDebugLoc();
654     auto InstUpToMBBI = MBB.end();
655     while (InstUpToMBBI != MBBI)
656       // The pre-decrement is on purpose here.
657       // We want to have the liveness right before MBBI.
658       UsedRegs.stepBackward(*--InstUpToMBBI);
659   }
660 
661   // Look for a register that can be directly use in the POP.
662   unsigned PopReg = 0;
663   // And some temporary register, just in case.
664   unsigned TemporaryReg = 0;
665   BitVector PopFriendly =
666       TRI.getAllocatableSet(MF, TRI.getRegClass(ARM::tGPRRegClassID));
667   // R7 may be used as a frame pointer, hence marked as not generally
668   // allocatable, however there's no reason to not use it as a temporary for
669   // restoring LR.
670   if (STI.getFramePointerReg() == ARM::R7)
671     PopFriendly.set(ARM::R7);
672 
673   assert(PopFriendly.any() && "No allocatable pop-friendly register?!");
674   // Rebuild the GPRs from the high registers because they are removed
675   // form the GPR reg class for thumb1.
676   BitVector GPRsNoLRSP =
677       TRI.getAllocatableSet(MF, TRI.getRegClass(ARM::hGPRRegClassID));
678   GPRsNoLRSP |= PopFriendly;
679   GPRsNoLRSP.reset(ARM::LR);
680   GPRsNoLRSP.reset(ARM::SP);
681   GPRsNoLRSP.reset(ARM::PC);
682   findTemporariesForLR(GPRsNoLRSP, PopFriendly, UsedRegs, PopReg, TemporaryReg,
683                        MF.getRegInfo());
684 
685   // If we couldn't find a pop-friendly register, try restoring LR before
686   // popping the other callee-saved registers, so we could use one of them as a
687   // temporary.
688   bool UseLDRSP = false;
689   if (!PopReg && MBBI != MBB.begin()) {
690     auto PrevMBBI = MBBI;
691     PrevMBBI--;
692     if (PrevMBBI->getOpcode() == ARM::tPOP) {
693       UsedRegs.stepBackward(*PrevMBBI);
694       findTemporariesForLR(GPRsNoLRSP, PopFriendly, UsedRegs, PopReg,
695                            TemporaryReg, MF.getRegInfo());
696       if (PopReg) {
697         MBBI = PrevMBBI;
698         UseLDRSP = true;
699       }
700     }
701   }
702 
703   if (!DoIt && !PopReg && !TemporaryReg)
704     return false;
705 
706   assert((PopReg || TemporaryReg) && "Cannot get LR");
707 
708   if (UseLDRSP) {
709     assert(PopReg && "Do not know how to get LR");
710     // Load the LR via LDR tmp, [SP, #off]
711     BuildMI(MBB, MBBI, dl, TII.get(ARM::tLDRspi))
712       .addReg(PopReg, RegState::Define)
713       .addReg(ARM::SP)
714       .addImm(MBBI->getNumExplicitOperands() - 2)
715       .add(predOps(ARMCC::AL))
716       .setMIFlag(MachineInstr::FrameDestroy);
717     // Move from the temporary register to the LR.
718     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
719       .addReg(ARM::LR, RegState::Define)
720       .addReg(PopReg, RegState::Kill)
721       .add(predOps(ARMCC::AL))
722       .setMIFlag(MachineInstr::FrameDestroy);
723     // Advance past the pop instruction.
724     MBBI++;
725     // Increment the SP.
726     emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo,
727                                  ArgRegsSaveSize + 4, ARM::NoRegister,
728                                  MachineInstr::FrameDestroy);
729     return true;
730   }
731 
732   if (TemporaryReg) {
733     assert(!PopReg && "Unnecessary MOV is about to be inserted");
734     PopReg = PopFriendly.find_first();
735     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
736         .addReg(TemporaryReg, RegState::Define)
737         .addReg(PopReg, RegState::Kill)
738         .add(predOps(ARMCC::AL))
739         .setMIFlag(MachineInstr::FrameDestroy);
740   }
741 
742   if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPOP_RET) {
743     // We couldn't use the direct restoration above, so
744     // perform the opposite conversion: tPOP_RET to tPOP.
745     MachineInstrBuilder MIB =
746         BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP))
747             .add(predOps(ARMCC::AL))
748             .setMIFlag(MachineInstr::FrameDestroy);
749     bool Popped = false;
750     for (auto MO: MBBI->operands())
751       if (MO.isReg() && (MO.isImplicit() || MO.isDef()) &&
752           MO.getReg() != ARM::PC) {
753         MIB.add(MO);
754         if (!MO.isImplicit())
755           Popped = true;
756       }
757     // Is there anything left to pop?
758     if (!Popped)
759       MBB.erase(MIB.getInstr());
760     // Erase the old instruction.
761     MBB.erase(MBBI);
762     MBBI = BuildMI(MBB, MBB.end(), dl, TII.get(ARM::tBX_RET))
763                .add(predOps(ARMCC::AL))
764                .setMIFlag(MachineInstr::FrameDestroy);
765   }
766 
767   assert(PopReg && "Do not know how to get LR");
768   BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP))
769       .add(predOps(ARMCC::AL))
770       .addReg(PopReg, RegState::Define)
771       .setMIFlag(MachineInstr::FrameDestroy);
772 
773   emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize,
774                                ARM::NoRegister, MachineInstr::FrameDestroy);
775 
776   BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
777       .addReg(ARM::LR, RegState::Define)
778       .addReg(PopReg, RegState::Kill)
779       .add(predOps(ARMCC::AL))
780       .setMIFlag(MachineInstr::FrameDestroy);
781 
782   if (TemporaryReg)
783     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
784         .addReg(PopReg, RegState::Define)
785         .addReg(TemporaryReg, RegState::Kill)
786         .add(predOps(ARMCC::AL))
787         .setMIFlag(MachineInstr::FrameDestroy);
788 
789   return true;
790 }
791 
792 using ARMRegSet = std::bitset<ARM::NUM_TARGET_REGS>;
793 
794 // Return the first iteraror after CurrentReg which is present in EnabledRegs,
795 // or OrderEnd if no further registers are in that set. This does not advance
796 // the iterator fiorst, so returns CurrentReg if it is in EnabledRegs.
797 static const unsigned *findNextOrderedReg(const unsigned *CurrentReg,
798                                           const ARMRegSet &EnabledRegs,
799                                           const unsigned *OrderEnd) {
800   while (CurrentReg != OrderEnd && !EnabledRegs[*CurrentReg])
801     ++CurrentReg;
802   return CurrentReg;
803 }
804 
805 bool Thumb1FrameLowering::spillCalleeSavedRegisters(
806     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
807     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
808   if (CSI.empty())
809     return false;
810 
811   DebugLoc DL;
812   const TargetInstrInfo &TII = *STI.getInstrInfo();
813   MachineFunction &MF = *MBB.getParent();
814   const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
815       MF.getSubtarget().getRegisterInfo());
816 
817   ARMRegSet LoRegsToSave; // r0-r7, lr
818   ARMRegSet HiRegsToSave; // r8-r11
819   ARMRegSet CopyRegs;     // Registers which can be used after pushing
820                           // LoRegs for saving HiRegs.
821 
822   for (const CalleeSavedInfo &I : llvm::reverse(CSI)) {
823     Register Reg = I.getReg();
824 
825     if (ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) {
826       LoRegsToSave[Reg] = true;
827     } else if (ARM::hGPRRegClass.contains(Reg) && Reg != ARM::LR) {
828       HiRegsToSave[Reg] = true;
829     } else {
830       llvm_unreachable("callee-saved register of unexpected class");
831     }
832 
833     if ((ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) &&
834         !MF.getRegInfo().isLiveIn(Reg) &&
835         !(hasFP(MF) && Reg == RegInfo->getFrameRegister(MF)))
836       CopyRegs[Reg] = true;
837   }
838 
839   // Unused argument registers can be used for the high register saving.
840   for (unsigned ArgReg : {ARM::R0, ARM::R1, ARM::R2, ARM::R3})
841     if (!MF.getRegInfo().isLiveIn(ArgReg))
842       CopyRegs[ArgReg] = true;
843 
844   // Push the low registers and lr
845   const MachineRegisterInfo &MRI = MF.getRegInfo();
846   if (!LoRegsToSave.none()) {
847     MachineInstrBuilder MIB =
848         BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH)).add(predOps(ARMCC::AL));
849     for (unsigned Reg : {ARM::R4, ARM::R5, ARM::R6, ARM::R7, ARM::LR}) {
850       if (LoRegsToSave[Reg]) {
851         bool isKill = !MRI.isLiveIn(Reg);
852         if (isKill && !MRI.isReserved(Reg))
853           MBB.addLiveIn(Reg);
854 
855         MIB.addReg(Reg, getKillRegState(isKill));
856       }
857     }
858     MIB.setMIFlags(MachineInstr::FrameSetup);
859   }
860 
861   // Push the high registers. There are no store instructions that can access
862   // these registers directly, so we have to move them to low registers, and
863   // push them. This might take multiple pushes, as it is possible for there to
864   // be fewer low registers available than high registers which need saving.
865 
866   // These are in reverse order so that in the case where we need to use
867   // multiple PUSH instructions, the order of the registers on the stack still
868   // matches the unwind info. They need to be swicthed back to ascending order
869   // before adding to the PUSH instruction.
870   static const unsigned AllCopyRegs[] = {ARM::LR, ARM::R7, ARM::R6,
871                                          ARM::R5, ARM::R4, ARM::R3,
872                                          ARM::R2, ARM::R1, ARM::R0};
873   static const unsigned AllHighRegs[] = {ARM::R11, ARM::R10, ARM::R9, ARM::R8};
874 
875   const unsigned *AllCopyRegsEnd = std::end(AllCopyRegs);
876   const unsigned *AllHighRegsEnd = std::end(AllHighRegs);
877 
878   // Find the first register to save.
879   const unsigned *HiRegToSave = findNextOrderedReg(
880       std::begin(AllHighRegs), HiRegsToSave, AllHighRegsEnd);
881 
882   while (HiRegToSave != AllHighRegsEnd) {
883     // Find the first low register to use.
884     const unsigned *CopyReg =
885         findNextOrderedReg(std::begin(AllCopyRegs), CopyRegs, AllCopyRegsEnd);
886 
887     // Create the PUSH, but don't insert it yet (the MOVs need to come first).
888     MachineInstrBuilder PushMIB = BuildMI(MF, DL, TII.get(ARM::tPUSH))
889                                       .add(predOps(ARMCC::AL))
890                                       .setMIFlags(MachineInstr::FrameSetup);
891 
892     SmallVector<unsigned, 4> RegsToPush;
893     while (HiRegToSave != AllHighRegsEnd && CopyReg != AllCopyRegsEnd) {
894       if (HiRegsToSave[*HiRegToSave]) {
895         bool isKill = !MRI.isLiveIn(*HiRegToSave);
896         if (isKill && !MRI.isReserved(*HiRegToSave))
897           MBB.addLiveIn(*HiRegToSave);
898 
899         // Emit a MOV from the high reg to the low reg.
900         BuildMI(MBB, MI, DL, TII.get(ARM::tMOVr))
901             .addReg(*CopyReg, RegState::Define)
902             .addReg(*HiRegToSave, getKillRegState(isKill))
903             .add(predOps(ARMCC::AL))
904             .setMIFlags(MachineInstr::FrameSetup);
905 
906         // Record the register that must be added to the PUSH.
907         RegsToPush.push_back(*CopyReg);
908 
909         CopyReg = findNextOrderedReg(++CopyReg, CopyRegs, AllCopyRegsEnd);
910         HiRegToSave =
911             findNextOrderedReg(++HiRegToSave, HiRegsToSave, AllHighRegsEnd);
912       }
913     }
914 
915     // Add the low registers to the PUSH, in ascending order.
916     for (unsigned Reg : llvm::reverse(RegsToPush))
917       PushMIB.addReg(Reg, RegState::Kill);
918 
919     // Insert the PUSH instruction after the MOVs.
920     MBB.insert(MI, PushMIB);
921   }
922 
923   return true;
924 }
925 
926 bool Thumb1FrameLowering::restoreCalleeSavedRegisters(
927     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
928     MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
929   if (CSI.empty())
930     return false;
931 
932   MachineFunction &MF = *MBB.getParent();
933   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
934   const TargetInstrInfo &TII = *STI.getInstrInfo();
935   const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
936       MF.getSubtarget().getRegisterInfo());
937 
938   bool isVarArg = AFI->getArgRegsSaveSize() > 0;
939   DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
940 
941   ARMRegSet LoRegsToRestore;
942   ARMRegSet HiRegsToRestore;
943   // Low registers (r0-r7) which can be used to restore the high registers.
944   ARMRegSet CopyRegs;
945 
946   for (CalleeSavedInfo I : CSI) {
947     Register Reg = I.getReg();
948 
949     if (ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) {
950       LoRegsToRestore[Reg] = true;
951     } else if (ARM::hGPRRegClass.contains(Reg) && Reg != ARM::LR) {
952       HiRegsToRestore[Reg] = true;
953     } else {
954       llvm_unreachable("callee-saved register of unexpected class");
955     }
956 
957     // If this is a low register not used as the frame pointer, we may want to
958     // use it for restoring the high registers.
959     if ((ARM::tGPRRegClass.contains(Reg)) &&
960         !(hasFP(MF) && Reg == RegInfo->getFrameRegister(MF)))
961       CopyRegs[Reg] = true;
962   }
963 
964   // If this is a return block, we may be able to use some unused return value
965   // registers for restoring the high regs.
966   auto Terminator = MBB.getFirstTerminator();
967   if (Terminator != MBB.end() && Terminator->getOpcode() == ARM::tBX_RET) {
968     CopyRegs[ARM::R0] = true;
969     CopyRegs[ARM::R1] = true;
970     CopyRegs[ARM::R2] = true;
971     CopyRegs[ARM::R3] = true;
972     for (auto Op : Terminator->implicit_operands()) {
973       if (Op.isReg())
974         CopyRegs[Op.getReg()] = false;
975     }
976   }
977 
978   static const unsigned AllCopyRegs[] = {ARM::R0, ARM::R1, ARM::R2, ARM::R3,
979                                          ARM::R4, ARM::R5, ARM::R6, ARM::R7};
980   static const unsigned AllHighRegs[] = {ARM::R8, ARM::R9, ARM::R10, ARM::R11};
981 
982   const unsigned *AllCopyRegsEnd = std::end(AllCopyRegs);
983   const unsigned *AllHighRegsEnd = std::end(AllHighRegs);
984 
985   // Find the first register to restore.
986   auto HiRegToRestore = findNextOrderedReg(std::begin(AllHighRegs),
987                                            HiRegsToRestore, AllHighRegsEnd);
988 
989   while (HiRegToRestore != AllHighRegsEnd) {
990     assert(!CopyRegs.none());
991     // Find the first low register to use.
992     auto CopyReg =
993         findNextOrderedReg(std::begin(AllCopyRegs), CopyRegs, AllCopyRegsEnd);
994 
995     // Create the POP instruction.
996     MachineInstrBuilder PopMIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPOP))
997                                      .add(predOps(ARMCC::AL))
998                                      .setMIFlag(MachineInstr::FrameDestroy);
999 
1000     while (HiRegToRestore != AllHighRegsEnd && CopyReg != AllCopyRegsEnd) {
1001       // Add the low register to the POP.
1002       PopMIB.addReg(*CopyReg, RegState::Define);
1003 
1004       // Create the MOV from low to high register.
1005       BuildMI(MBB, MI, DL, TII.get(ARM::tMOVr))
1006           .addReg(*HiRegToRestore, RegState::Define)
1007           .addReg(*CopyReg, RegState::Kill)
1008           .add(predOps(ARMCC::AL))
1009           .setMIFlag(MachineInstr::FrameDestroy);
1010 
1011       CopyReg = findNextOrderedReg(++CopyReg, CopyRegs, AllCopyRegsEnd);
1012       HiRegToRestore =
1013           findNextOrderedReg(++HiRegToRestore, HiRegsToRestore, AllHighRegsEnd);
1014     }
1015   }
1016 
1017   MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP))
1018                                 .add(predOps(ARMCC::AL))
1019                                 .setMIFlag(MachineInstr::FrameDestroy);
1020 
1021   bool NeedsPop = false;
1022   for (CalleeSavedInfo &Info : llvm::reverse(CSI)) {
1023     Register Reg = Info.getReg();
1024 
1025     // High registers (excluding lr) have already been dealt with
1026     if (!(ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR))
1027       continue;
1028 
1029     if (Reg == ARM::LR) {
1030       Info.setRestored(false);
1031       if (!MBB.succ_empty() ||
1032           MI->getOpcode() == ARM::TCRETURNdi ||
1033           MI->getOpcode() == ARM::TCRETURNri)
1034         // LR may only be popped into PC, as part of return sequence.
1035         // If this isn't the return sequence, we'll need emitPopSpecialFixUp
1036         // to restore LR the hard way.
1037         // FIXME: if we don't pass any stack arguments it would be actually
1038         // advantageous *and* correct to do the conversion to an ordinary call
1039         // instruction here.
1040         continue;
1041       // Special epilogue for vararg functions. See emitEpilogue
1042       if (isVarArg)
1043         continue;
1044       // ARMv4T requires BX, see emitEpilogue
1045       if (!STI.hasV5TOps())
1046         continue;
1047 
1048       // CMSE entry functions must return via BXNS, see emitEpilogue.
1049       if (AFI->isCmseNSEntryFunction())
1050         continue;
1051 
1052       // Pop LR into PC.
1053       Reg = ARM::PC;
1054       (*MIB).setDesc(TII.get(ARM::tPOP_RET));
1055       if (MI != MBB.end())
1056         MIB.copyImplicitOps(*MI);
1057       MI = MBB.erase(MI);
1058     }
1059     MIB.addReg(Reg, getDefRegState(true));
1060     NeedsPop = true;
1061   }
1062 
1063   // It's illegal to emit pop instruction without operands.
1064   if (NeedsPop)
1065     MBB.insert(MI, &*MIB);
1066   else
1067     MF.deleteMachineInstr(MIB);
1068 
1069   return true;
1070 }
1071