1 //===-- Thumb1FrameLowering.cpp - Thumb1 Frame Information ----------------===//
2 //
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 //
10 // This file contains the Thumb1 implementation of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "Thumb1FrameLowering.h"
15 #include "ARMMachineFunctionInfo.h"
16 #include "llvm/CodeGen/LivePhysRegs.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 
23 using namespace llvm;
24 
25 Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti)
26     : ARMFrameLowering(sti) {}
27 
28 bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
29   const MachineFrameInfo &MFI = MF.getFrameInfo();
30   unsigned CFSize = MFI.getMaxCallFrameSize();
31   // 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 
38   return !MFI.hasVarSizedObjects();
39 }
40 
41 static 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) {
46   emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
47                             MRI, MIFlags);
48 }
49 
50 
51 MachineBasicBlock::iterator Thumb1FrameLowering::
52 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
53                               MachineBasicBlock::iterator I) const {
54   const Thumb1InstrInfo &TII =
55       *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
56   const ThumbRegisterInfo *RegInfo =
57       static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
58   if (!hasReservedCallFrame(MF)) {
59     // If we have alloca, convert as follows:
60     // ADJCALLSTACKDOWN -> sub, sp, sp, amount
61     // ADJCALLSTACKUP   -> add, sp, sp, amount
62     MachineInstr &Old = *I;
63     DebugLoc dl = Old.getDebugLoc();
64     unsigned Amount = Old.getOperand(0).getImm();
65     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...
73       unsigned Opc = Old.getOpcode();
74       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   }
82   return MBB.erase(I);
83 }
84 
85 void Thumb1FrameLowering::emitPrologue(MachineFunction &MF,
86                                        MachineBasicBlock &MBB) const {
87   MachineBasicBlock::iterator MBBI = MBB.begin();
88   MachineFrameInfo &MFI = MF.getFrameInfo();
89   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
90   MachineModuleInfo &MMI = MF.getMMI();
91   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
92   const ThumbRegisterInfo *RegInfo =
93       static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
94   const Thumb1InstrInfo &TII =
95       *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
96 
97   unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
98   unsigned NumBytes = MFI.getStackSize();
99   assert(NumBytes >= ArgRegsSaveSize &&
100          "ArgRegsSaveSize is included in NumBytes");
101   const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
102 
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;
110 
111   // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
112   NumBytes = (NumBytes + 3) & ~3;
113   MFI.setStackSize(NumBytes);
114 
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 
120   if (ArgRegsSaveSize) {
121     emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
122                  MachineInstr::FrameSetup);
123     CFAOffset -= ArgRegsSaveSize;
124     unsigned CFIIndex = MF.addFrameInst(
125         MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
126     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
127         .addCFIIndex(CFIIndex)
128         .setMIFlags(MachineInstr::FrameSetup);
129   }
130 
131   if (!AFI->hasStackFrame()) {
132     if (NumBytes - ArgRegsSaveSize != 0) {
133       emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize),
134                    MachineInstr::FrameSetup);
135       CFAOffset -= NumBytes - ArgRegsSaveSize;
136       unsigned CFIIndex = MF.addFrameInst(
137           MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
138       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
139           .addCFIIndex(CFIIndex)
140           .setMIFlags(MachineInstr::FrameSetup);
141     }
142     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) {
149     case ARM::R8:
150     case ARM::R9:
151     case ARM::R10:
152     case ARM::R11:
153       if (STI.splitFramePushPop(MF)) {
154         GPRCS2Size += 4;
155         break;
156       }
157       LLVM_FALLTHROUGH;
158     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;
165       GPRCS1Size += 4;
166       break;
167     default:
168       DPRCSSize += 8;
169     }
170   }
171 
172   if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
173     ++MBBI;
174   }
175 
176   // Determine starting offsets of spill areas.
177   unsigned DPRCSOffset  = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
178   unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
179   unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
180   bool HasFP = hasFP(MF);
181   if (HasFP)
182     AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
183                                 NumBytes);
184   AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
185   AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
186   AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
187   NumBytes = DPRCSOffset;
188 
189   int FramePtrOffsetInBlock = 0;
190   unsigned adjustedGPRCS1Size = GPRCS1Size;
191   if (GPRCS1Size > 0 && GPRCS2Size == 0 &&
192       tryFoldSPUpdateIntoPushPop(STI, MF, &*std::prev(MBBI), NumBytes)) {
193     FramePtrOffsetInBlock = NumBytes;
194     adjustedGPRCS1Size += NumBytes;
195     NumBytes = 0;
196   }
197 
198   if (adjustedGPRCS1Size) {
199     CFAOffset -= adjustedGPRCS1Size;
200     unsigned CFIIndex = MF.addFrameInst(
201         MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
202     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
203         .addCFIIndex(CFIIndex)
204         .setMIFlags(MachineInstr::FrameSetup);
205   }
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:
216       if (STI.splitFramePushPop(MF))
217         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:
228       unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
229           nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
230       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
231           .addCFIIndex(CFIIndex)
232           .setMIFlags(MachineInstr::FrameSetup);
233       break;
234     }
235   }
236 
237   // Adjust FP so it point to the stack slot that contains the previous FP.
238   if (HasFP) {
239     FramePtrOffsetInBlock +=
240         MFI.getObjectOffset(FramePtrSpillFI) + GPRCS1Size + ArgRegsSaveSize;
241     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));
246     if(FramePtrOffsetInBlock) {
247       CFAOffset += FramePtrOffsetInBlock;
248       unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
249           nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
250       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
251           .addCFIIndex(CFIIndex)
252           .setMIFlags(MachineInstr::FrameSetup);
253     } else {
254       unsigned CFIIndex =
255           MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
256               nullptr, MRI->getDwarfRegNum(FramePtr, true)));
257       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
258           .addCFIIndex(CFIIndex)
259           .setMIFlags(MachineInstr::FrameSetup);
260     }
261     if (NumBytes > 508)
262       // If offset is > 508 then sp cannot be adjusted in a single instruction,
263       // try restoring from fp instead.
264       AFI->setShouldRestoreSPFromFP(true);
265   }
266 
267   // 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: {
297       unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
298           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 
309   if (NumBytes) {
310     // Insert it after all the callee-save spills.
311     emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
312                  MachineInstr::FrameSetup);
313     if (!HasFP) {
314       CFAOffset -= NumBytes;
315       unsigned CFIIndex = MF.addFrameInst(
316           MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
317       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
318           .addCFIIndex(CFIIndex)
319           .setMIFlags(MachineInstr::FrameSetup);
320     }
321   }
322 
323   if (STI.isTargetELF() && HasFP)
324     MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
325                             AFI->getFramePtrSpillOffset());
326 
327   AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
328   AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
329   AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
330 
331   // 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.");
335 
336   // 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))
341     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
342         .addReg(ARM::SP)
343         .add(predOps(ARMCC::AL));
344 
345   // 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.
348   if (MFI.hasVarSizedObjects())
349     AFI->setShouldRestoreSPFromFP(true);
350 
351   // In some cases, virtual registers have been introduced, e.g. by uses of
352   // emitThumbRegPlusImmInReg.
353   MF.getProperties().reset(MachineFunctionProperties::Property::NoVRegs);
354 }
355 
356 static bool isCSRestore(MachineInstr &MI, const MCPhysReg *CSRegs) {
357   if (MI.getOpcode() == ARM::tLDRspi && MI.getOperand(1).isFI() &&
358       isCalleeSavedRegister(MI.getOperand(0).getReg(), CSRegs))
359     return true;
360   else if (MI.getOpcode() == ARM::tPOP) {
361     return true;
362   } 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));
367   }
368   return false;
369 }
370 
371 void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
372                                    MachineBasicBlock &MBB) const {
373   MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
374   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
375   MachineFrameInfo &MFI = MF.getFrameInfo();
376   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
377   const ThumbRegisterInfo *RegInfo =
378       static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
379   const Thumb1InstrInfo &TII =
380       *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
381 
382   unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
383   int NumBytes = (int)MFI.getStackSize();
384   assert((unsigned)NumBytes >= ArgRegsSaveSize &&
385          "ArgRegsSaveSize is included in NumBytes");
386   const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
387   unsigned FramePtr = RegInfo->getFrameRegister(MF);
388 
389   if (!AFI->hasStackFrame()) {
390     if (NumBytes - ArgRegsSaveSize != 0)
391       emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize);
392   } else {
393     // Unwind MBBI to point to first LDR / VLDRD.
394     if (MBBI != MBB.begin()) {
395       do
396         --MBBI;
397       while (MBBI != MBB.begin() && isCSRestore(*MBBI, CSRegs));
398       if (!isCSRestore(*MBBI, CSRegs))
399         ++MBBI;
400     }
401 
402     // Move SP to start of FP callee save spill area.
403     NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
404                  AFI->getGPRCalleeSavedArea2Size() +
405                  AFI->getDPRCalleeSavedAreaSize() +
406                  ArgRegsSaveSize);
407 
408     if (AFI->shouldRestoreSPFromFP()) {
409       NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
410       // Reset SP based on frame pointer only if the stack frame extends beyond
411       // frame pointer stack slot, the target is ELF and the function has FP, or
412       // the target uses var sized objects.
413       if (NumBytes) {
414         assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
415                "No scratch register to restore SP from FP!");
416         emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
417                                   TII, *RegInfo);
418         BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
419             .addReg(ARM::R4)
420             .add(predOps(ARMCC::AL));
421       } else
422         BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
423             .addReg(FramePtr)
424             .add(predOps(ARMCC::AL));
425     } else {
426       if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET &&
427           &MBB.front() != &*MBBI && std::prev(MBBI)->getOpcode() == ARM::tPOP) {
428         MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
429         if (!tryFoldSPUpdateIntoPushPop(STI, MF, &*PMBBI, NumBytes))
430           emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes);
431       } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
432         emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes);
433     }
434   }
435 
436   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 
443 bool 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 
451 bool Thumb1FrameLowering::needPopSpecialFixUp(const MachineFunction &MF) const {
452   ARMFunctionInfo *AFI =
453       const_cast<MachineFunction *>(&MF)->getInfo<ARMFunctionInfo>();
454   if (AFI->getArgRegsSaveSize())
455     return true;
456 
457   // LR cannot be encoded with Thumb1, i.e., it requires a special fix-up.
458   for (const CalleeSavedInfo &CSI : MF.getFrameInfo().getCalleeSavedInfo())
459     if (CSI.getReg() == ARM::LR)
460       return true;
461 
462   return false;
463 }
464 
465 bool 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());
473 
474   // 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.
481   auto MBBI = MBB.getFirstTerminator();
482   bool CanRestoreDirectly = STI.hasV5TOps() && !ArgRegsSaveSize;
483   if (CanRestoreDirectly) {
484     if (MBBI != MBB.end() && MBBI->getOpcode() != ARM::tB)
485       CanRestoreDirectly = (MBBI->getOpcode() == ARM::tBX_RET ||
486                             MBBI->getOpcode() == ARM::tPOP_RET);
487     else {
488       auto MBBI_prev = MBBI;
489       MBBI_prev--;
490       assert(MBBI_prev->getOpcode() == ARM::tPOP);
491       assert(MBB.succ_size() == 1);
492       if ((*MBB.succ_begin())->begin()->getOpcode() == ARM::tBX_RET)
493         MBBI = MBBI_prev; // Replace the final tPOP with a tPOP_RET.
494       else
495         CanRestoreDirectly = false;
496     }
497   }
498 
499   if (CanRestoreDirectly) {
500     if (!DoIt || MBBI->getOpcode() == ARM::tPOP_RET)
501       return true;
502     MachineInstrBuilder MIB =
503         BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP_RET))
504             .add(predOps(ARMCC::AL));
505     // Copy implicit ops and popped registers, if any.
506     for (auto MO: MBBI->operands())
507       if (MO.isReg() && (MO.isImplicit() || MO.isDef()))
508         MIB.add(MO);
509     MIB.addReg(ARM::PC, RegState::Define);
510     // Erase the old instruction (tBX_RET or tPOP).
511     MBB.erase(MBBI);
512     return true;
513   }
514 
515   // Look for a temporary register to use.
516   // First, compute the liveness information.
517   LivePhysRegs UsedRegs(STI.getRegisterInfo());
518   UsedRegs.addLiveOuts(MBB);
519   // 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]);
527 
528   DebugLoc dl = DebugLoc();
529   if (MBBI != MBB.end()) {
530     dl = MBBI->getDebugLoc();
531     auto InstUpToMBBI = MBB.end();
532     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);
536   }
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;
561       }
562       // Otherwise, remember that the register will be available to
563       // save a pop-friendly register.
564       TemporaryReg = Register;
565     }
566   }
567 
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();
576     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
577         .addReg(TemporaryReg, RegState::Define)
578         .addReg(PopReg, RegState::Kill)
579         .add(predOps(ARMCC::AL));
580   }
581 
582   if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPOP_RET) {
583     // We couldn't use the direct restoration above, so
584     // perform the opposite conversion: tPOP_RET to tPOP.
585     MachineInstrBuilder MIB =
586         BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP))
587             .add(predOps(ARMCC::AL));
588     bool Popped = false;
589     for (auto MO: MBBI->operands())
590       if (MO.isReg() && (MO.isImplicit() || MO.isDef()) &&
591           MO.getReg() != ARM::PC) {
592         MIB.add(MO);
593         if (!MO.isImplicit())
594           Popped = true;
595       }
596     // Is there anything left to pop?
597     if (!Popped)
598       MBB.erase(MIB.getInstr());
599     // Erase the old instruction.
600     MBB.erase(MBBI);
601     MBBI = BuildMI(MBB, MBB.end(), dl, TII.get(ARM::tBX_RET))
602                .add(predOps(ARMCC::AL));
603   }
604 
605   assert(PopReg && "Do not know how to get LR");
606   BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP))
607       .add(predOps(ARMCC::AL))
608       .addReg(PopReg, RegState::Define);
609 
610   emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize);
611 
612   BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
613       .addReg(ARM::LR, RegState::Define)
614       .addReg(PopReg, RegState::Kill)
615       .add(predOps(ARMCC::AL));
616 
617   if (TemporaryReg)
618     BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
619         .addReg(PopReg, RegState::Define)
620         .addReg(TemporaryReg, RegState::Kill)
621         .add(predOps(ARMCC::AL));
622 
623   return true;
624 }
625 
626 // 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.
629 template <unsigned SetSize>
630 static const unsigned *
631 findNextOrderedReg(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 
639 bool Thumb1FrameLowering::
640 spillCalleeSavedRegisters(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 
647   DebugLoc DL;
648   const TargetInstrInfo &TII = *STI.getInstrInfo();
649   MachineFunction &MF = *MBB.getParent();
650   const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
651       MF.getSubtarget().getRegisterInfo());
652 
653   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 
658   for (unsigned i = CSI.size(); i != 0; --i) {
659     unsigned Reg = CSI[i-1].getReg();
660 
661     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");
667     }
668 
669     if ((ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) &&
670         !MF.getRegInfo().isLiveIn(Reg) &&
671         !(hasFP(MF) && Reg == RegInfo->getFrameRegister(MF)))
672       CopyRegs.insert(Reg);
673   }
674 
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()) {
682     MachineInstrBuilder MIB =
683         BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH)).add(predOps(ARMCC::AL));
684     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).
723     MachineInstrBuilder PushMIB =
724         BuildMI(MF, DL, TII.get(ARM::tPUSH)).add(predOps(ARMCC::AL));
725 
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.
734         BuildMI(MBB, MI, DL, TII.get(ARM::tMOVr))
735             .addReg(*CopyReg, RegState::Define)
736             .addReg(*HiRegToSave, getKillRegState(isKill))
737             .add(predOps(ARMCC::AL));
738 
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 
756   return true;
757 }
758 
759 bool Thumb1FrameLowering::
760 restoreCalleeSavedRegisters(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>();
769   const TargetInstrInfo &TII = *STI.getInstrInfo();
770   const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
771       MF.getSubtarget().getRegisterInfo());
772 
773   bool isVarArg = AFI->getArgRegsSaveSize() > 0;
774   DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
775 
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.
831     MachineInstrBuilder PopMIB =
832         BuildMI(MBB, MI, DL, TII.get(ARM::tPOP)).add(predOps(ARMCC::AL));
833 
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.
839       BuildMI(MBB, MI, DL, TII.get(ARM::tMOVr))
840           .addReg(*HiRegToRestore, RegState::Define)
841           .addReg(*CopyReg, RegState::Kill)
842           .add(predOps(ARMCC::AL));
843 
844       CopyReg = findNextOrderedReg(++CopyReg, CopyRegs, AllCopyRegsEnd);
845       HiRegToRestore =
846           findNextOrderedReg(++HiRegToRestore, HiRegsToRestore, AllHighRegsEnd);
847     }
848   }
849 
850   MachineInstrBuilder MIB =
851       BuildMI(MF, DL, TII.get(ARM::tPOP)).add(predOps(ARMCC::AL));
852 
853   bool NeedsPop = false;
854   for (unsigned i = CSI.size(); i != 0; --i) {
855     unsigned Reg = CSI[i-1].getReg();
856 
857     // High registers (excluding lr) have already been dealt with
858     if (!(ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR))
859       continue;
860 
861     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())
872           MIB.copyImplicitOps(*MI);
873         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.
878         continue;
879     }
880     MIB.addReg(Reg, getDefRegState(true));
881     NeedsPop = true;
882   }
883 
884   // It's illegal to emit pop instruction without operands.
885   if (NeedsPop)
886     MBB.insert(MI, &*MIB);
887   else
888     MF.DeleteMachineInstr(MIB);
889 
890   return true;
891 }
892