1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072 |
- //===- Thumb1FrameLowering.cpp - Thumb1 Frame Information -----------------===//
- //
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
- // See https://llvm.org/LICENSE.txt for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- //===----------------------------------------------------------------------===//
- //
- // This file contains the Thumb1 implementation of TargetFrameLowering class.
- //
- //===----------------------------------------------------------------------===//
- #include "Thumb1FrameLowering.h"
- #include "ARMBaseInstrInfo.h"
- #include "ARMBaseRegisterInfo.h"
- #include "ARMMachineFunctionInfo.h"
- #include "ARMSubtarget.h"
- #include "Thumb1InstrInfo.h"
- #include "ThumbRegisterInfo.h"
- #include "Utils/ARMBaseInfo.h"
- #include "llvm/ADT/BitVector.h"
- #include "llvm/ADT/STLExtras.h"
- #include "llvm/ADT/SmallVector.h"
- #include "llvm/CodeGen/LivePhysRegs.h"
- #include "llvm/CodeGen/MachineBasicBlock.h"
- #include "llvm/CodeGen/MachineFrameInfo.h"
- #include "llvm/CodeGen/MachineFunction.h"
- #include "llvm/CodeGen/MachineInstr.h"
- #include "llvm/CodeGen/MachineInstrBuilder.h"
- #include "llvm/CodeGen/MachineModuleInfo.h"
- #include "llvm/CodeGen/MachineOperand.h"
- #include "llvm/CodeGen/MachineRegisterInfo.h"
- #include "llvm/CodeGen/TargetInstrInfo.h"
- #include "llvm/CodeGen/TargetOpcodes.h"
- #include "llvm/CodeGen/TargetSubtargetInfo.h"
- #include "llvm/IR/DebugLoc.h"
- #include "llvm/MC/MCContext.h"
- #include "llvm/MC/MCDwarf.h"
- #include "llvm/MC/MCRegisterInfo.h"
- #include "llvm/Support/Compiler.h"
- #include "llvm/Support/ErrorHandling.h"
- #include "llvm/Support/MathExtras.h"
- #include <bitset>
- #include <cassert>
- #include <iterator>
- #include <vector>
- using namespace llvm;
- Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti)
- : ARMFrameLowering(sti) {}
- bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{
- const MachineFrameInfo &MFI = MF.getFrameInfo();
- unsigned CFSize = MFI.getMaxCallFrameSize();
- // It's not always a good idea to include the call frame as part of the
- // stack frame. ARM (especially Thumb) has small immediate offset to
- // address the stack frame. So a large call frame can cause poor codegen
- // and may even makes it impossible to scavenge a register.
- if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
- return false;
- return !MFI.hasVarSizedObjects();
- }
- static void
- emitPrologueEpilogueSPUpdate(MachineBasicBlock &MBB,
- MachineBasicBlock::iterator &MBBI,
- const TargetInstrInfo &TII, const DebugLoc &dl,
- const ThumbRegisterInfo &MRI, int NumBytes,
- unsigned ScratchReg, unsigned MIFlags) {
- // If it would take more than three instructions to adjust the stack pointer
- // using tADDspi/tSUBspi, load an immediate instead.
- if (std::abs(NumBytes) > 508 * 3) {
- // We use a different codepath here from the normal
- // emitThumbRegPlusImmediate so we don't have to deal with register
- // scavenging. (Scavenging could try to use the emergency spill slot
- // before we've actually finished setting up the stack.)
- if (ScratchReg == ARM::NoRegister)
- report_fatal_error("Failed to emit Thumb1 stack adjustment");
- MachineFunction &MF = *MBB.getParent();
- const ARMSubtarget &ST = MF.getSubtarget<ARMSubtarget>();
- if (ST.genExecuteOnly()) {
- BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ScratchReg)
- .addImm(NumBytes).setMIFlags(MIFlags);
- } else {
- MRI.emitLoadConstPool(MBB, MBBI, dl, ScratchReg, 0, NumBytes, ARMCC::AL,
- 0, MIFlags);
- }
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDhirr), ARM::SP)
- .addReg(ARM::SP)
- .addReg(ScratchReg, RegState::Kill)
- .add(predOps(ARMCC::AL))
- .setMIFlags(MIFlags);
- return;
- }
- // FIXME: This is assuming the heuristics in emitThumbRegPlusImmediate
- // won't change.
- emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
- MRI, MIFlags);
- }
- static void emitCallSPUpdate(MachineBasicBlock &MBB,
- MachineBasicBlock::iterator &MBBI,
- const TargetInstrInfo &TII, const DebugLoc &dl,
- const ThumbRegisterInfo &MRI, int NumBytes,
- unsigned MIFlags = MachineInstr::NoFlags) {
- emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII,
- MRI, MIFlags);
- }
- MachineBasicBlock::iterator Thumb1FrameLowering::
- eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
- MachineBasicBlock::iterator I) const {
- const Thumb1InstrInfo &TII =
- *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
- const ThumbRegisterInfo *RegInfo =
- static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
- if (!hasReservedCallFrame(MF)) {
- // If we have alloca, convert as follows:
- // ADJCALLSTACKDOWN -> sub, sp, sp, amount
- // ADJCALLSTACKUP -> add, sp, sp, amount
- MachineInstr &Old = *I;
- DebugLoc dl = Old.getDebugLoc();
- unsigned Amount = TII.getFrameSize(Old);
- if (Amount != 0) {
- // We need to keep the stack aligned properly. To do this, we round the
- // amount of space needed for the outgoing arguments up to the next
- // alignment boundary.
- Amount = alignTo(Amount, getStackAlign());
- // Replace the pseudo instruction with a new instruction...
- unsigned Opc = Old.getOpcode();
- if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
- emitCallSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount);
- } else {
- assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
- emitCallSPUpdate(MBB, I, TII, dl, *RegInfo, Amount);
- }
- }
- }
- return MBB.erase(I);
- }
- void Thumb1FrameLowering::emitPrologue(MachineFunction &MF,
- MachineBasicBlock &MBB) const {
- MachineBasicBlock::iterator MBBI = MBB.begin();
- MachineFrameInfo &MFI = MF.getFrameInfo();
- ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
- MachineModuleInfo &MMI = MF.getMMI();
- const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
- const ThumbRegisterInfo *RegInfo =
- static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
- const Thumb1InstrInfo &TII =
- *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
- unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
- unsigned NumBytes = MFI.getStackSize();
- assert(NumBytes >= ArgRegsSaveSize &&
- "ArgRegsSaveSize is included in NumBytes");
- const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
- // Debug location must be unknown since the first debug location is used
- // to determine the end of the prologue.
- DebugLoc dl;
- Register FramePtr = RegInfo->getFrameRegister(MF);
- Register BasePtr = RegInfo->getBaseRegister();
- int CFAOffset = 0;
- // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
- NumBytes = (NumBytes + 3) & ~3;
- MFI.setStackSize(NumBytes);
- // Determine the sizes of each callee-save spill areas and record which frame
- // belongs to which callee-save spill areas.
- unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
- int FramePtrSpillFI = 0;
- if (ArgRegsSaveSize) {
- emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize,
- ARM::NoRegister, MachineInstr::FrameSetup);
- CFAOffset += ArgRegsSaveSize;
- unsigned CFIIndex =
- MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
- BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
- .addCFIIndex(CFIIndex)
- .setMIFlags(MachineInstr::FrameSetup);
- }
- if (!AFI->hasStackFrame()) {
- if (NumBytes - ArgRegsSaveSize != 0) {
- emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo,
- -(NumBytes - ArgRegsSaveSize),
- ARM::NoRegister, MachineInstr::FrameSetup);
- CFAOffset += NumBytes - ArgRegsSaveSize;
- unsigned CFIIndex = MF.addFrameInst(
- MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
- BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
- .addCFIIndex(CFIIndex)
- .setMIFlags(MachineInstr::FrameSetup);
- }
- return;
- }
- for (const CalleeSavedInfo &I : CSI) {
- Register Reg = I.getReg();
- int FI = I.getFrameIdx();
- switch (Reg) {
- case ARM::R8:
- case ARM::R9:
- case ARM::R10:
- case ARM::R11:
- if (STI.splitFramePushPop(MF)) {
- GPRCS2Size += 4;
- break;
- }
- LLVM_FALLTHROUGH;
- case ARM::R4:
- case ARM::R5:
- case ARM::R6:
- case ARM::R7:
- case ARM::LR:
- if (Reg == FramePtr)
- FramePtrSpillFI = FI;
- GPRCS1Size += 4;
- break;
- default:
- DPRCSSize += 8;
- }
- }
- if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
- ++MBBI;
- }
- // Determine starting offsets of spill areas.
- unsigned DPRCSOffset = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize);
- unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
- unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
- bool HasFP = hasFP(MF);
- if (HasFP)
- AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
- NumBytes);
- AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
- AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
- AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
- NumBytes = DPRCSOffset;
- int FramePtrOffsetInBlock = 0;
- unsigned adjustedGPRCS1Size = GPRCS1Size;
- if (GPRCS1Size > 0 && GPRCS2Size == 0 &&
- tryFoldSPUpdateIntoPushPop(STI, MF, &*std::prev(MBBI), NumBytes)) {
- FramePtrOffsetInBlock = NumBytes;
- adjustedGPRCS1Size += NumBytes;
- NumBytes = 0;
- }
- if (adjustedGPRCS1Size) {
- CFAOffset += adjustedGPRCS1Size;
- unsigned CFIIndex =
- MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
- BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
- .addCFIIndex(CFIIndex)
- .setMIFlags(MachineInstr::FrameSetup);
- }
- for (const CalleeSavedInfo &I : CSI) {
- Register Reg = I.getReg();
- int FI = I.getFrameIdx();
- switch (Reg) {
- case ARM::R8:
- case ARM::R9:
- case ARM::R10:
- case ARM::R11:
- case ARM::R12:
- if (STI.splitFramePushPop(MF))
- break;
- LLVM_FALLTHROUGH;
- case ARM::R0:
- case ARM::R1:
- case ARM::R2:
- case ARM::R3:
- case ARM::R4:
- case ARM::R5:
- case ARM::R6:
- case ARM::R7:
- case ARM::LR:
- unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
- nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
- BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
- .addCFIIndex(CFIIndex)
- .setMIFlags(MachineInstr::FrameSetup);
- break;
- }
- }
- // Adjust FP so it point to the stack slot that contains the previous FP.
- if (HasFP) {
- FramePtrOffsetInBlock +=
- MFI.getObjectOffset(FramePtrSpillFI) + GPRCS1Size + ArgRegsSaveSize;
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
- .addReg(ARM::SP)
- .addImm(FramePtrOffsetInBlock / 4)
- .setMIFlags(MachineInstr::FrameSetup)
- .add(predOps(ARMCC::AL));
- if(FramePtrOffsetInBlock) {
- CFAOffset -= FramePtrOffsetInBlock;
- unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
- nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
- BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
- .addCFIIndex(CFIIndex)
- .setMIFlags(MachineInstr::FrameSetup);
- } else {
- unsigned CFIIndex =
- MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
- nullptr, MRI->getDwarfRegNum(FramePtr, true)));
- BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
- .addCFIIndex(CFIIndex)
- .setMIFlags(MachineInstr::FrameSetup);
- }
- if (NumBytes > 508)
- // If offset is > 508 then sp cannot be adjusted in a single instruction,
- // try restoring from fp instead.
- AFI->setShouldRestoreSPFromFP(true);
- }
- // Skip past the spilling of r8-r11, which could consist of multiple tPUSH
- // and tMOVr instructions. We don't need to add any call frame information
- // in-between these instructions, because they do not modify the high
- // registers.
- while (true) {
- MachineBasicBlock::iterator OldMBBI = MBBI;
- // Skip a run of tMOVr instructions
- while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tMOVr)
- MBBI++;
- if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
- MBBI++;
- } else {
- // We have reached an instruction which is not a push, so the previous
- // run of tMOVr instructions (which may have been empty) was not part of
- // the prologue. Reset MBBI back to the last PUSH of the prologue.
- MBBI = OldMBBI;
- break;
- }
- }
- // Emit call frame information for the callee-saved high registers.
- for (auto &I : CSI) {
- Register Reg = I.getReg();
- int FI = I.getFrameIdx();
- switch (Reg) {
- case ARM::R8:
- case ARM::R9:
- case ARM::R10:
- case ARM::R11:
- case ARM::R12: {
- unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
- nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
- BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
- .addCFIIndex(CFIIndex)
- .setMIFlags(MachineInstr::FrameSetup);
- break;
- }
- default:
- break;
- }
- }
- if (NumBytes) {
- // Insert it after all the callee-save spills.
- //
- // For a large stack frame, we might need a scratch register to store
- // the size of the frame. We know all callee-save registers are free
- // at this point in the prologue, so pick one.
- unsigned ScratchRegister = ARM::NoRegister;
- for (auto &I : CSI) {
- Register Reg = I.getReg();
- if (isARMLowRegister(Reg) && !(HasFP && Reg == FramePtr)) {
- ScratchRegister = Reg;
- break;
- }
- }
- emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes,
- ScratchRegister, MachineInstr::FrameSetup);
- if (!HasFP) {
- CFAOffset += NumBytes;
- unsigned CFIIndex = MF.addFrameInst(
- MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
- BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
- .addCFIIndex(CFIIndex)
- .setMIFlags(MachineInstr::FrameSetup);
- }
- }
- if (STI.isTargetELF() && HasFP)
- MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
- AFI->getFramePtrSpillOffset());
- AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
- AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
- AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
- if (RegInfo->hasStackRealignment(MF)) {
- const unsigned NrBitsToZero = Log2(MFI.getMaxAlign());
- // Emit the following sequence, using R4 as a temporary, since we cannot use
- // SP as a source or destination register for the shifts:
- // mov r4, sp
- // lsrs r4, r4, #NrBitsToZero
- // lsls r4, r4, #NrBitsToZero
- // mov sp, r4
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
- .addReg(ARM::SP, RegState::Kill)
- .add(predOps(ARMCC::AL));
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tLSRri), ARM::R4)
- .addDef(ARM::CPSR)
- .addReg(ARM::R4, RegState::Kill)
- .addImm(NrBitsToZero)
- .add(predOps(ARMCC::AL));
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tLSLri), ARM::R4)
- .addDef(ARM::CPSR)
- .addReg(ARM::R4, RegState::Kill)
- .addImm(NrBitsToZero)
- .add(predOps(ARMCC::AL));
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
- .addReg(ARM::R4, RegState::Kill)
- .add(predOps(ARMCC::AL));
- AFI->setShouldRestoreSPFromFP(true);
- }
- // If we need a base pointer, set it up here. It's whatever the value
- // of the stack pointer is at this point. Any variable size objects
- // will be allocated after this, so we can still use the base pointer
- // to reference locals.
- if (RegInfo->hasBasePointer(MF))
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr)
- .addReg(ARM::SP)
- .add(predOps(ARMCC::AL));
- // If the frame has variable sized objects then the epilogue must restore
- // the sp from fp. We can assume there's an FP here since hasFP already
- // checks for hasVarSizedObjects.
- if (MFI.hasVarSizedObjects())
- AFI->setShouldRestoreSPFromFP(true);
- // In some cases, virtual registers have been introduced, e.g. by uses of
- // emitThumbRegPlusImmInReg.
- MF.getProperties().reset(MachineFunctionProperties::Property::NoVRegs);
- }
- static bool isCSRestore(MachineInstr &MI, const MCPhysReg *CSRegs) {
- if (MI.getOpcode() == ARM::tLDRspi && MI.getOperand(1).isFI() &&
- isCalleeSavedRegister(MI.getOperand(0).getReg(), CSRegs))
- return true;
- else if (MI.getOpcode() == ARM::tPOP) {
- return true;
- } else if (MI.getOpcode() == ARM::tMOVr) {
- Register Dst = MI.getOperand(0).getReg();
- Register Src = MI.getOperand(1).getReg();
- return ((ARM::tGPRRegClass.contains(Src) || Src == ARM::LR) &&
- ARM::hGPRRegClass.contains(Dst));
- }
- return false;
- }
- void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF,
- MachineBasicBlock &MBB) const {
- MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
- DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
- MachineFrameInfo &MFI = MF.getFrameInfo();
- ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
- const ThumbRegisterInfo *RegInfo =
- static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
- const Thumb1InstrInfo &TII =
- *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo());
- unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
- int NumBytes = (int)MFI.getStackSize();
- assert((unsigned)NumBytes >= ArgRegsSaveSize &&
- "ArgRegsSaveSize is included in NumBytes");
- const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
- Register FramePtr = RegInfo->getFrameRegister(MF);
- if (!AFI->hasStackFrame()) {
- if (NumBytes - ArgRegsSaveSize != 0)
- emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo,
- NumBytes - ArgRegsSaveSize, ARM::NoRegister,
- MachineInstr::NoFlags);
- } else {
- // Unwind MBBI to point to first LDR / VLDRD.
- if (MBBI != MBB.begin()) {
- do
- --MBBI;
- while (MBBI != MBB.begin() && isCSRestore(*MBBI, CSRegs));
- if (!isCSRestore(*MBBI, CSRegs))
- ++MBBI;
- }
- // Move SP to start of FP callee save spill area.
- NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
- AFI->getGPRCalleeSavedArea2Size() +
- AFI->getDPRCalleeSavedAreaSize() +
- ArgRegsSaveSize);
- if (AFI->shouldRestoreSPFromFP()) {
- NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
- // Reset SP based on frame pointer only if the stack frame extends beyond
- // frame pointer stack slot, the target is ELF and the function has FP, or
- // the target uses var sized objects.
- if (NumBytes) {
- assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
- "No scratch register to restore SP from FP!");
- emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
- TII, *RegInfo);
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
- .addReg(ARM::R4)
- .add(predOps(ARMCC::AL));
- } else
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
- .addReg(FramePtr)
- .add(predOps(ARMCC::AL));
- } else {
- // For a large stack frame, we might need a scratch register to store
- // the size of the frame. We know all callee-save registers are free
- // at this point in the epilogue, so pick one.
- unsigned ScratchRegister = ARM::NoRegister;
- bool HasFP = hasFP(MF);
- for (auto &I : MFI.getCalleeSavedInfo()) {
- Register Reg = I.getReg();
- if (isARMLowRegister(Reg) && !(HasFP && Reg == FramePtr)) {
- ScratchRegister = Reg;
- break;
- }
- }
- if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET &&
- &MBB.front() != &*MBBI && std::prev(MBBI)->getOpcode() == ARM::tPOP) {
- MachineBasicBlock::iterator PMBBI = std::prev(MBBI);
- if (!tryFoldSPUpdateIntoPushPop(STI, MF, &*PMBBI, NumBytes))
- emitPrologueEpilogueSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes,
- ScratchRegister, MachineInstr::NoFlags);
- } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
- emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes,
- ScratchRegister, MachineInstr::NoFlags);
- }
- }
- if (needPopSpecialFixUp(MF)) {
- bool Done = emitPopSpecialFixUp(MBB, /* DoIt */ true);
- (void)Done;
- assert(Done && "Emission of the special fixup failed!?");
- }
- }
- bool Thumb1FrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
- if (!needPopSpecialFixUp(*MBB.getParent()))
- return true;
- MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
- return emitPopSpecialFixUp(*TmpMBB, /* DoIt */ false);
- }
- bool Thumb1FrameLowering::needPopSpecialFixUp(const MachineFunction &MF) const {
- ARMFunctionInfo *AFI =
- const_cast<MachineFunction *>(&MF)->getInfo<ARMFunctionInfo>();
- if (AFI->getArgRegsSaveSize())
- return true;
- // LR cannot be encoded with Thumb1, i.e., it requires a special fix-up.
- for (const CalleeSavedInfo &CSI : MF.getFrameInfo().getCalleeSavedInfo())
- if (CSI.getReg() == ARM::LR)
- return true;
- return false;
- }
- static void findTemporariesForLR(const BitVector &GPRsNoLRSP,
- const BitVector &PopFriendly,
- const LivePhysRegs &UsedRegs, unsigned &PopReg,
- unsigned &TmpReg, MachineRegisterInfo &MRI) {
- PopReg = TmpReg = 0;
- for (auto Reg : GPRsNoLRSP.set_bits()) {
- if (UsedRegs.available(MRI, Reg)) {
- // Remember the first pop-friendly register and exit.
- if (PopFriendly.test(Reg)) {
- PopReg = Reg;
- TmpReg = 0;
- break;
- }
- // Otherwise, remember that the register will be available to
- // save a pop-friendly register.
- TmpReg = Reg;
- }
- }
- }
- bool Thumb1FrameLowering::emitPopSpecialFixUp(MachineBasicBlock &MBB,
- bool DoIt) const {
- MachineFunction &MF = *MBB.getParent();
- ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
- unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
- const TargetInstrInfo &TII = *STI.getInstrInfo();
- const ThumbRegisterInfo *RegInfo =
- static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo());
- // If MBBI is a return instruction, or is a tPOP followed by a return
- // instruction in the successor BB, we may be able to directly restore
- // LR in the PC.
- // This is only possible with v5T ops (v4T can't change the Thumb bit via
- // a POP PC instruction), and only if we do not need to emit any SP update.
- // Otherwise, we need a temporary register to pop the value
- // and copy that value into LR.
- auto MBBI = MBB.getFirstTerminator();
- bool CanRestoreDirectly = STI.hasV5TOps() && !ArgRegsSaveSize;
- if (CanRestoreDirectly) {
- if (MBBI != MBB.end() && MBBI->getOpcode() != ARM::tB)
- CanRestoreDirectly = (MBBI->getOpcode() == ARM::tBX_RET ||
- MBBI->getOpcode() == ARM::tPOP_RET);
- else {
- auto MBBI_prev = MBBI;
- MBBI_prev--;
- assert(MBBI_prev->getOpcode() == ARM::tPOP);
- assert(MBB.succ_size() == 1);
- if ((*MBB.succ_begin())->begin()->getOpcode() == ARM::tBX_RET)
- MBBI = MBBI_prev; // Replace the final tPOP with a tPOP_RET.
- else
- CanRestoreDirectly = false;
- }
- }
- if (CanRestoreDirectly) {
- if (!DoIt || MBBI->getOpcode() == ARM::tPOP_RET)
- return true;
- MachineInstrBuilder MIB =
- BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP_RET))
- .add(predOps(ARMCC::AL));
- // Copy implicit ops and popped registers, if any.
- for (auto MO: MBBI->operands())
- if (MO.isReg() && (MO.isImplicit() || MO.isDef()))
- MIB.add(MO);
- MIB.addReg(ARM::PC, RegState::Define);
- // Erase the old instruction (tBX_RET or tPOP).
- MBB.erase(MBBI);
- return true;
- }
- // Look for a temporary register to use.
- // First, compute the liveness information.
- const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
- LivePhysRegs UsedRegs(TRI);
- UsedRegs.addLiveOuts(MBB);
- // The semantic of pristines changed recently and now,
- // the callee-saved registers that are touched in the function
- // are not part of the pristines set anymore.
- // Add those callee-saved now.
- const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
- for (unsigned i = 0; CSRegs[i]; ++i)
- UsedRegs.addReg(CSRegs[i]);
- DebugLoc dl = DebugLoc();
- if (MBBI != MBB.end()) {
- dl = MBBI->getDebugLoc();
- auto InstUpToMBBI = MBB.end();
- while (InstUpToMBBI != MBBI)
- // The pre-decrement is on purpose here.
- // We want to have the liveness right before MBBI.
- UsedRegs.stepBackward(*--InstUpToMBBI);
- }
- // Look for a register that can be directly use in the POP.
- unsigned PopReg = 0;
- // And some temporary register, just in case.
- unsigned TemporaryReg = 0;
- BitVector PopFriendly =
- TRI.getAllocatableSet(MF, TRI.getRegClass(ARM::tGPRRegClassID));
- // R7 may be used as a frame pointer, hence marked as not generally
- // allocatable, however there's no reason to not use it as a temporary for
- // restoring LR.
- if (STI.getFramePointerReg() == ARM::R7)
- PopFriendly.set(ARM::R7);
- assert(PopFriendly.any() && "No allocatable pop-friendly register?!");
- // Rebuild the GPRs from the high registers because they are removed
- // form the GPR reg class for thumb1.
- BitVector GPRsNoLRSP =
- TRI.getAllocatableSet(MF, TRI.getRegClass(ARM::hGPRRegClassID));
- GPRsNoLRSP |= PopFriendly;
- GPRsNoLRSP.reset(ARM::LR);
- GPRsNoLRSP.reset(ARM::SP);
- GPRsNoLRSP.reset(ARM::PC);
- findTemporariesForLR(GPRsNoLRSP, PopFriendly, UsedRegs, PopReg, TemporaryReg,
- MF.getRegInfo());
- // If we couldn't find a pop-friendly register, try restoring LR before
- // popping the other callee-saved registers, so we could use one of them as a
- // temporary.
- bool UseLDRSP = false;
- if (!PopReg && MBBI != MBB.begin()) {
- auto PrevMBBI = MBBI;
- PrevMBBI--;
- if (PrevMBBI->getOpcode() == ARM::tPOP) {
- UsedRegs.stepBackward(*PrevMBBI);
- findTemporariesForLR(GPRsNoLRSP, PopFriendly, UsedRegs, PopReg,
- TemporaryReg, MF.getRegInfo());
- if (PopReg) {
- MBBI = PrevMBBI;
- UseLDRSP = true;
- }
- }
- }
- if (!DoIt && !PopReg && !TemporaryReg)
- return false;
- assert((PopReg || TemporaryReg) && "Cannot get LR");
- if (UseLDRSP) {
- assert(PopReg && "Do not know how to get LR");
- // Load the LR via LDR tmp, [SP, #off]
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tLDRspi))
- .addReg(PopReg, RegState::Define)
- .addReg(ARM::SP)
- .addImm(MBBI->getNumExplicitOperands() - 2)
- .add(predOps(ARMCC::AL));
- // Move from the temporary register to the LR.
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
- .addReg(ARM::LR, RegState::Define)
- .addReg(PopReg, RegState::Kill)
- .add(predOps(ARMCC::AL));
- // Advance past the pop instruction.
- MBBI++;
- // Increment the SP.
- emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo,
- ArgRegsSaveSize + 4, ARM::NoRegister,
- MachineInstr::NoFlags);
- return true;
- }
- if (TemporaryReg) {
- assert(!PopReg && "Unnecessary MOV is about to be inserted");
- PopReg = PopFriendly.find_first();
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
- .addReg(TemporaryReg, RegState::Define)
- .addReg(PopReg, RegState::Kill)
- .add(predOps(ARMCC::AL));
- }
- if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPOP_RET) {
- // We couldn't use the direct restoration above, so
- // perform the opposite conversion: tPOP_RET to tPOP.
- MachineInstrBuilder MIB =
- BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP))
- .add(predOps(ARMCC::AL));
- bool Popped = false;
- for (auto MO: MBBI->operands())
- if (MO.isReg() && (MO.isImplicit() || MO.isDef()) &&
- MO.getReg() != ARM::PC) {
- MIB.add(MO);
- if (!MO.isImplicit())
- Popped = true;
- }
- // Is there anything left to pop?
- if (!Popped)
- MBB.erase(MIB.getInstr());
- // Erase the old instruction.
- MBB.erase(MBBI);
- MBBI = BuildMI(MBB, MBB.end(), dl, TII.get(ARM::tBX_RET))
- .add(predOps(ARMCC::AL));
- }
- assert(PopReg && "Do not know how to get LR");
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP))
- .add(predOps(ARMCC::AL))
- .addReg(PopReg, RegState::Define);
- emitPrologueEpilogueSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize,
- ARM::NoRegister, MachineInstr::NoFlags);
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
- .addReg(ARM::LR, RegState::Define)
- .addReg(PopReg, RegState::Kill)
- .add(predOps(ARMCC::AL));
- if (TemporaryReg)
- BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr))
- .addReg(PopReg, RegState::Define)
- .addReg(TemporaryReg, RegState::Kill)
- .add(predOps(ARMCC::AL));
- return true;
- }
- using ARMRegSet = std::bitset<ARM::NUM_TARGET_REGS>;
- // Return the first iteraror after CurrentReg which is present in EnabledRegs,
- // or OrderEnd if no further registers are in that set. This does not advance
- // the iterator fiorst, so returns CurrentReg if it is in EnabledRegs.
- static const unsigned *findNextOrderedReg(const unsigned *CurrentReg,
- const ARMRegSet &EnabledRegs,
- const unsigned *OrderEnd) {
- while (CurrentReg != OrderEnd && !EnabledRegs[*CurrentReg])
- ++CurrentReg;
- return CurrentReg;
- }
- bool Thumb1FrameLowering::spillCalleeSavedRegisters(
- MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
- ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
- if (CSI.empty())
- return false;
- DebugLoc DL;
- const TargetInstrInfo &TII = *STI.getInstrInfo();
- MachineFunction &MF = *MBB.getParent();
- const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
- MF.getSubtarget().getRegisterInfo());
- ARMRegSet LoRegsToSave; // r0-r7, lr
- ARMRegSet HiRegsToSave; // r8-r11
- ARMRegSet CopyRegs; // Registers which can be used after pushing
- // LoRegs for saving HiRegs.
- for (const CalleeSavedInfo &I : llvm::reverse(CSI)) {
- Register Reg = I.getReg();
- if (ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) {
- LoRegsToSave[Reg] = true;
- } else if (ARM::hGPRRegClass.contains(Reg) && Reg != ARM::LR) {
- HiRegsToSave[Reg] = true;
- } else {
- llvm_unreachable("callee-saved register of unexpected class");
- }
- if ((ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) &&
- !MF.getRegInfo().isLiveIn(Reg) &&
- !(hasFP(MF) && Reg == RegInfo->getFrameRegister(MF)))
- CopyRegs[Reg] = true;
- }
- // Unused argument registers can be used for the high register saving.
- for (unsigned ArgReg : {ARM::R0, ARM::R1, ARM::R2, ARM::R3})
- if (!MF.getRegInfo().isLiveIn(ArgReg))
- CopyRegs[ArgReg] = true;
- // Push the low registers and lr
- const MachineRegisterInfo &MRI = MF.getRegInfo();
- if (!LoRegsToSave.none()) {
- MachineInstrBuilder MIB =
- BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH)).add(predOps(ARMCC::AL));
- for (unsigned Reg : {ARM::R4, ARM::R5, ARM::R6, ARM::R7, ARM::LR}) {
- if (LoRegsToSave[Reg]) {
- bool isKill = !MRI.isLiveIn(Reg);
- if (isKill && !MRI.isReserved(Reg))
- MBB.addLiveIn(Reg);
- MIB.addReg(Reg, getKillRegState(isKill));
- }
- }
- MIB.setMIFlags(MachineInstr::FrameSetup);
- }
- // Push the high registers. There are no store instructions that can access
- // these registers directly, so we have to move them to low registers, and
- // push them. This might take multiple pushes, as it is possible for there to
- // be fewer low registers available than high registers which need saving.
- // These are in reverse order so that in the case where we need to use
- // multiple PUSH instructions, the order of the registers on the stack still
- // matches the unwind info. They need to be swicthed back to ascending order
- // before adding to the PUSH instruction.
- static const unsigned AllCopyRegs[] = {ARM::LR, ARM::R7, ARM::R6,
- ARM::R5, ARM::R4, ARM::R3,
- ARM::R2, ARM::R1, ARM::R0};
- static const unsigned AllHighRegs[] = {ARM::R11, ARM::R10, ARM::R9, ARM::R8};
- const unsigned *AllCopyRegsEnd = std::end(AllCopyRegs);
- const unsigned *AllHighRegsEnd = std::end(AllHighRegs);
- // Find the first register to save.
- const unsigned *HiRegToSave = findNextOrderedReg(
- std::begin(AllHighRegs), HiRegsToSave, AllHighRegsEnd);
- while (HiRegToSave != AllHighRegsEnd) {
- // Find the first low register to use.
- const unsigned *CopyReg =
- findNextOrderedReg(std::begin(AllCopyRegs), CopyRegs, AllCopyRegsEnd);
- // Create the PUSH, but don't insert it yet (the MOVs need to come first).
- MachineInstrBuilder PushMIB = BuildMI(MF, DL, TII.get(ARM::tPUSH))
- .add(predOps(ARMCC::AL))
- .setMIFlags(MachineInstr::FrameSetup);
- SmallVector<unsigned, 4> RegsToPush;
- while (HiRegToSave != AllHighRegsEnd && CopyReg != AllCopyRegsEnd) {
- if (HiRegsToSave[*HiRegToSave]) {
- bool isKill = !MRI.isLiveIn(*HiRegToSave);
- if (isKill && !MRI.isReserved(*HiRegToSave))
- MBB.addLiveIn(*HiRegToSave);
- // Emit a MOV from the high reg to the low reg.
- BuildMI(MBB, MI, DL, TII.get(ARM::tMOVr))
- .addReg(*CopyReg, RegState::Define)
- .addReg(*HiRegToSave, getKillRegState(isKill))
- .add(predOps(ARMCC::AL))
- .setMIFlags(MachineInstr::FrameSetup);
- // Record the register that must be added to the PUSH.
- RegsToPush.push_back(*CopyReg);
- CopyReg = findNextOrderedReg(++CopyReg, CopyRegs, AllCopyRegsEnd);
- HiRegToSave =
- findNextOrderedReg(++HiRegToSave, HiRegsToSave, AllHighRegsEnd);
- }
- }
- // Add the low registers to the PUSH, in ascending order.
- for (unsigned Reg : llvm::reverse(RegsToPush))
- PushMIB.addReg(Reg, RegState::Kill);
- // Insert the PUSH instruction after the MOVs.
- MBB.insert(MI, PushMIB);
- }
- return true;
- }
- bool Thumb1FrameLowering::restoreCalleeSavedRegisters(
- MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
- MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
- if (CSI.empty())
- return false;
- MachineFunction &MF = *MBB.getParent();
- ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
- const TargetInstrInfo &TII = *STI.getInstrInfo();
- const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
- MF.getSubtarget().getRegisterInfo());
- bool isVarArg = AFI->getArgRegsSaveSize() > 0;
- DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
- ARMRegSet LoRegsToRestore;
- ARMRegSet HiRegsToRestore;
- // Low registers (r0-r7) which can be used to restore the high registers.
- ARMRegSet CopyRegs;
- for (CalleeSavedInfo I : CSI) {
- Register Reg = I.getReg();
- if (ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR) {
- LoRegsToRestore[Reg] = true;
- } else if (ARM::hGPRRegClass.contains(Reg) && Reg != ARM::LR) {
- HiRegsToRestore[Reg] = true;
- } else {
- llvm_unreachable("callee-saved register of unexpected class");
- }
- // If this is a low register not used as the frame pointer, we may want to
- // use it for restoring the high registers.
- if ((ARM::tGPRRegClass.contains(Reg)) &&
- !(hasFP(MF) && Reg == RegInfo->getFrameRegister(MF)))
- CopyRegs[Reg] = true;
- }
- // If this is a return block, we may be able to use some unused return value
- // registers for restoring the high regs.
- auto Terminator = MBB.getFirstTerminator();
- if (Terminator != MBB.end() && Terminator->getOpcode() == ARM::tBX_RET) {
- CopyRegs[ARM::R0] = true;
- CopyRegs[ARM::R1] = true;
- CopyRegs[ARM::R2] = true;
- CopyRegs[ARM::R3] = true;
- for (auto Op : Terminator->implicit_operands()) {
- if (Op.isReg())
- CopyRegs[Op.getReg()] = false;
- }
- }
- static const unsigned AllCopyRegs[] = {ARM::R0, ARM::R1, ARM::R2, ARM::R3,
- ARM::R4, ARM::R5, ARM::R6, ARM::R7};
- static const unsigned AllHighRegs[] = {ARM::R8, ARM::R9, ARM::R10, ARM::R11};
- const unsigned *AllCopyRegsEnd = std::end(AllCopyRegs);
- const unsigned *AllHighRegsEnd = std::end(AllHighRegs);
- // Find the first register to restore.
- auto HiRegToRestore = findNextOrderedReg(std::begin(AllHighRegs),
- HiRegsToRestore, AllHighRegsEnd);
- while (HiRegToRestore != AllHighRegsEnd) {
- assert(!CopyRegs.none());
- // Find the first low register to use.
- auto CopyReg =
- findNextOrderedReg(std::begin(AllCopyRegs), CopyRegs, AllCopyRegsEnd);
- // Create the POP instruction.
- MachineInstrBuilder PopMIB =
- BuildMI(MBB, MI, DL, TII.get(ARM::tPOP)).add(predOps(ARMCC::AL));
- while (HiRegToRestore != AllHighRegsEnd && CopyReg != AllCopyRegsEnd) {
- // Add the low register to the POP.
- PopMIB.addReg(*CopyReg, RegState::Define);
- // Create the MOV from low to high register.
- BuildMI(MBB, MI, DL, TII.get(ARM::tMOVr))
- .addReg(*HiRegToRestore, RegState::Define)
- .addReg(*CopyReg, RegState::Kill)
- .add(predOps(ARMCC::AL));
- CopyReg = findNextOrderedReg(++CopyReg, CopyRegs, AllCopyRegsEnd);
- HiRegToRestore =
- findNextOrderedReg(++HiRegToRestore, HiRegsToRestore, AllHighRegsEnd);
- }
- }
- MachineInstrBuilder MIB =
- BuildMI(MF, DL, TII.get(ARM::tPOP)).add(predOps(ARMCC::AL));
- bool NeedsPop = false;
- for (CalleeSavedInfo &Info : llvm::reverse(CSI)) {
- Register Reg = Info.getReg();
- // High registers (excluding lr) have already been dealt with
- if (!(ARM::tGPRRegClass.contains(Reg) || Reg == ARM::LR))
- continue;
- if (Reg == ARM::LR) {
- Info.setRestored(false);
- if (!MBB.succ_empty() ||
- MI->getOpcode() == ARM::TCRETURNdi ||
- MI->getOpcode() == ARM::TCRETURNri)
- // LR may only be popped into PC, as part of return sequence.
- // If this isn't the return sequence, we'll need emitPopSpecialFixUp
- // to restore LR the hard way.
- // FIXME: if we don't pass any stack arguments it would be actually
- // advantageous *and* correct to do the conversion to an ordinary call
- // instruction here.
- continue;
- // Special epilogue for vararg functions. See emitEpilogue
- if (isVarArg)
- continue;
- // ARMv4T requires BX, see emitEpilogue
- if (!STI.hasV5TOps())
- continue;
- // CMSE entry functions must return via BXNS, see emitEpilogue.
- if (AFI->isCmseNSEntryFunction())
- continue;
- // Pop LR into PC.
- Reg = ARM::PC;
- (*MIB).setDesc(TII.get(ARM::tPOP_RET));
- if (MI != MBB.end())
- MIB.copyImplicitOps(*MI);
- MI = MBB.erase(MI);
- }
- MIB.addReg(Reg, getDefRegState(true));
- NeedsPop = true;
- }
- // It's illegal to emit pop instruction without operands.
- if (NeedsPop)
- MBB.insert(MI, &*MIB);
- else
- MF.deleteMachineInstr(MIB);
- return true;
- }
|