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