xref: /llvm-project/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp (revision 1a935d7a17519e9b75d12c3caf9a54a3405a0af3)
1cf78715cSZi Xuan Wu //===-- CSKYFrameLowering.cpp - CSKY Frame Information ------------------===//
2cf78715cSZi Xuan Wu //
3cf78715cSZi Xuan Wu // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4cf78715cSZi Xuan Wu // See https://llvm.org/LICENSE.txt for license information.
5cf78715cSZi Xuan Wu // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cf78715cSZi Xuan Wu //
7cf78715cSZi Xuan Wu //===----------------------------------------------------------------------===//
8cf78715cSZi Xuan Wu //
9cf78715cSZi Xuan Wu // This file contains the CSKY implementation of TargetFrameLowering class.
10cf78715cSZi Xuan Wu //
11cf78715cSZi Xuan Wu //===----------------------------------------------------------------------===//
12cf78715cSZi Xuan Wu 
13cf78715cSZi Xuan Wu #include "CSKYFrameLowering.h"
149566cf16SZi Xuan Wu #include "CSKYMachineFunctionInfo.h"
15cf78715cSZi Xuan Wu #include "CSKYSubtarget.h"
1680fd9f3eSZi Xuan Wu (Zeson) #include "llvm/CodeGen/MachineConstantPool.h"
17cf78715cSZi Xuan Wu #include "llvm/CodeGen/MachineFrameInfo.h"
18cf78715cSZi Xuan Wu #include "llvm/CodeGen/MachineFunction.h"
19cf78715cSZi Xuan Wu #include "llvm/CodeGen/MachineInstrBuilder.h"
20cf78715cSZi Xuan Wu #include "llvm/CodeGen/MachineRegisterInfo.h"
21cf78715cSZi Xuan Wu #include "llvm/CodeGen/RegisterScavenging.h"
22cf78715cSZi Xuan Wu #include "llvm/IR/DiagnosticInfo.h"
23cf78715cSZi Xuan Wu #include "llvm/MC/MCDwarf.h"
24cf78715cSZi Xuan Wu 
25cf78715cSZi Xuan Wu using namespace llvm;
26cf78715cSZi Xuan Wu 
27cf78715cSZi Xuan Wu #define DEBUG_TYPE "csky-frame-lowering"
28cf78715cSZi Xuan Wu 
29cf78715cSZi Xuan Wu // Returns the register used to hold the frame pointer.
30cf78715cSZi Xuan Wu static Register getFPReg(const CSKYSubtarget &STI) { return CSKY::R8; }
31cf78715cSZi Xuan Wu 
32cf78715cSZi Xuan Wu // To avoid the BP value clobbered by a function call, we need to choose a
33cf78715cSZi Xuan Wu // callee saved register to save the value.
34cf78715cSZi Xuan Wu static Register getBPReg(const CSKYSubtarget &STI) { return CSKY::R7; }
35cf78715cSZi Xuan Wu 
36ad4a582fSAlex Rønne Petersen bool CSKYFrameLowering::hasFPImpl(const MachineFunction &MF) const {
37cf78715cSZi Xuan Wu   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
38cf78715cSZi Xuan Wu 
39cf78715cSZi Xuan Wu   const MachineFrameInfo &MFI = MF.getFrameInfo();
40cf78715cSZi Xuan Wu   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
41cf78715cSZi Xuan Wu          RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
42cf78715cSZi Xuan Wu          MFI.isFrameAddressTaken();
43cf78715cSZi Xuan Wu }
44cf78715cSZi Xuan Wu 
45cf78715cSZi Xuan Wu bool CSKYFrameLowering::hasBP(const MachineFunction &MF) const {
46cf78715cSZi Xuan Wu   const MachineFrameInfo &MFI = MF.getFrameInfo();
47cf78715cSZi Xuan Wu 
48cf78715cSZi Xuan Wu   return MFI.hasVarSizedObjects();
49cf78715cSZi Xuan Wu }
50cf78715cSZi Xuan Wu 
519566cf16SZi Xuan Wu // Determines the size of the frame and maximum call frame size.
529566cf16SZi Xuan Wu void CSKYFrameLowering::determineFrameLayout(MachineFunction &MF) const {
539566cf16SZi Xuan Wu   MachineFrameInfo &MFI = MF.getFrameInfo();
549566cf16SZi Xuan Wu   const CSKYRegisterInfo *RI = STI.getRegisterInfo();
559566cf16SZi Xuan Wu 
569566cf16SZi Xuan Wu   // Get the number of bytes to allocate from the FrameInfo.
579566cf16SZi Xuan Wu   uint64_t FrameSize = MFI.getStackSize();
589566cf16SZi Xuan Wu 
599566cf16SZi Xuan Wu   // Get the alignment.
609566cf16SZi Xuan Wu   Align StackAlign = getStackAlign();
619566cf16SZi Xuan Wu   if (RI->hasStackRealignment(MF)) {
629566cf16SZi Xuan Wu     Align MaxStackAlign = std::max(StackAlign, MFI.getMaxAlign());
639566cf16SZi Xuan Wu     FrameSize += (MaxStackAlign.value() - StackAlign.value());
649566cf16SZi Xuan Wu     StackAlign = MaxStackAlign;
659566cf16SZi Xuan Wu   }
669566cf16SZi Xuan Wu 
679566cf16SZi Xuan Wu   // Set Max Call Frame Size
689566cf16SZi Xuan Wu   uint64_t MaxCallSize = alignTo(MFI.getMaxCallFrameSize(), StackAlign);
699566cf16SZi Xuan Wu   MFI.setMaxCallFrameSize(MaxCallSize);
709566cf16SZi Xuan Wu 
719566cf16SZi Xuan Wu   // Make sure the frame is aligned.
729566cf16SZi Xuan Wu   FrameSize = alignTo(FrameSize, StackAlign);
739566cf16SZi Xuan Wu 
749566cf16SZi Xuan Wu   // Update frame info.
759566cf16SZi Xuan Wu   MFI.setStackSize(FrameSize);
769566cf16SZi Xuan Wu }
779566cf16SZi Xuan Wu 
78cf78715cSZi Xuan Wu void CSKYFrameLowering::emitPrologue(MachineFunction &MF,
79cf78715cSZi Xuan Wu                                      MachineBasicBlock &MBB) const {
809566cf16SZi Xuan Wu   CSKYMachineFunctionInfo *CFI = MF.getInfo<CSKYMachineFunctionInfo>();
819566cf16SZi Xuan Wu   MachineFrameInfo &MFI = MF.getFrameInfo();
829566cf16SZi Xuan Wu   const CSKYRegisterInfo *RI = STI.getRegisterInfo();
839566cf16SZi Xuan Wu   const CSKYInstrInfo *TII = STI.getInstrInfo();
849566cf16SZi Xuan Wu   MachineBasicBlock::iterator MBBI = MBB.begin();
859566cf16SZi Xuan Wu   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
869566cf16SZi Xuan Wu   const MachineRegisterInfo &MRI = MF.getRegInfo();
879566cf16SZi Xuan Wu 
889566cf16SZi Xuan Wu   Register FPReg = getFPReg(STI);
899566cf16SZi Xuan Wu   Register SPReg = CSKY::R14;
909566cf16SZi Xuan Wu   Register BPReg = getBPReg(STI);
919566cf16SZi Xuan Wu 
929566cf16SZi Xuan Wu   // Debug location must be unknown since the first debug location is used
939566cf16SZi Xuan Wu   // to determine the end of the prologue.
949566cf16SZi Xuan Wu   DebugLoc DL;
959566cf16SZi Xuan Wu 
969566cf16SZi Xuan Wu   if (MF.getFunction().hasFnAttribute("interrupt"))
979566cf16SZi Xuan Wu     BuildMI(MBB, MBBI, DL, TII->get(CSKY::NIE));
989566cf16SZi Xuan Wu 
999566cf16SZi Xuan Wu   // Determine the correct frame layout
1009566cf16SZi Xuan Wu   determineFrameLayout(MF);
1019566cf16SZi Xuan Wu 
1029566cf16SZi Xuan Wu   // FIXME (note copied from Lanai): This appears to be overallocating.  Needs
1039566cf16SZi Xuan Wu   // investigation. Get the number of bytes to allocate from the FrameInfo.
1049566cf16SZi Xuan Wu   uint64_t StackSize = MFI.getStackSize();
1059566cf16SZi Xuan Wu 
1069566cf16SZi Xuan Wu   // Early exit if there is no need to allocate on the stack
1079566cf16SZi Xuan Wu   if (StackSize == 0 && !MFI.adjustsStack())
1089566cf16SZi Xuan Wu     return;
1099566cf16SZi Xuan Wu 
1109566cf16SZi Xuan Wu   const auto &CSI = MFI.getCalleeSavedInfo();
1119566cf16SZi Xuan Wu 
1129566cf16SZi Xuan Wu   unsigned spillAreaSize = CFI->getCalleeSaveAreaSize();
1139566cf16SZi Xuan Wu 
1149566cf16SZi Xuan Wu   uint64_t ActualSize = spillAreaSize + CFI->getVarArgsSaveSize();
1159566cf16SZi Xuan Wu 
1169566cf16SZi Xuan Wu   // First part stack allocation.
1179566cf16SZi Xuan Wu   adjustReg(MBB, MBBI, DL, SPReg, SPReg, -(static_cast<int64_t>(ActualSize)),
1189566cf16SZi Xuan Wu             MachineInstr::NoFlags);
1199566cf16SZi Xuan Wu 
1209566cf16SZi Xuan Wu   // Emit ".cfi_def_cfa_offset FirstSPAdjustAmount"
1219566cf16SZi Xuan Wu   unsigned CFIIndex =
1229566cf16SZi Xuan Wu       MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, ActualSize));
1239566cf16SZi Xuan Wu   BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
1249566cf16SZi Xuan Wu       .addCFIIndex(CFIIndex);
1259566cf16SZi Xuan Wu 
1269566cf16SZi Xuan Wu   // The frame pointer is callee-saved, and code has been generated for us to
1279566cf16SZi Xuan Wu   // save it to the stack. We need to skip over the storing of callee-saved
1289566cf16SZi Xuan Wu   // registers as the frame pointer must be modified after it has been saved
1299566cf16SZi Xuan Wu   // to the stack, not before.
1309566cf16SZi Xuan Wu   // FIXME: assumes exactly one instruction is used to save each callee-saved
1319566cf16SZi Xuan Wu   // register.
1329566cf16SZi Xuan Wu   std::advance(MBBI, CSI.size());
1339566cf16SZi Xuan Wu 
1349566cf16SZi Xuan Wu   // Iterate over list of callee-saved registers and emit .cfi_offset
1359566cf16SZi Xuan Wu   // directives.
1369566cf16SZi Xuan Wu   for (const auto &Entry : CSI) {
1379566cf16SZi Xuan Wu     int64_t Offset = MFI.getObjectOffset(Entry.getFrameIdx());
1389566cf16SZi Xuan Wu     Register Reg = Entry.getReg();
1399566cf16SZi Xuan Wu 
1409566cf16SZi Xuan Wu     unsigned Num = TRI->getRegSizeInBits(Reg, MRI) / 32;
1419566cf16SZi Xuan Wu     for (unsigned i = 0; i < Num; i++) {
1429566cf16SZi Xuan Wu       unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
1439566cf16SZi Xuan Wu           nullptr, RI->getDwarfRegNum(Reg, true) + i, Offset + i * 4));
1449566cf16SZi Xuan Wu       BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
1459566cf16SZi Xuan Wu           .addCFIIndex(CFIIndex);
1469566cf16SZi Xuan Wu     }
1479566cf16SZi Xuan Wu   }
1489566cf16SZi Xuan Wu 
1499566cf16SZi Xuan Wu   // Generate new FP.
1509566cf16SZi Xuan Wu   if (hasFP(MF)) {
1519566cf16SZi Xuan Wu     BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::COPY), FPReg)
1529566cf16SZi Xuan Wu         .addReg(SPReg)
1539566cf16SZi Xuan Wu         .setMIFlag(MachineInstr::FrameSetup);
1549566cf16SZi Xuan Wu 
1559566cf16SZi Xuan Wu     // Emit ".cfi_def_cfa_register $fp"
1569566cf16SZi Xuan Wu     unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
1579566cf16SZi Xuan Wu         nullptr, RI->getDwarfRegNum(FPReg, true)));
1589566cf16SZi Xuan Wu     BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
1599566cf16SZi Xuan Wu         .addCFIIndex(CFIIndex);
1609566cf16SZi Xuan Wu 
1619566cf16SZi Xuan Wu     // Second part stack allocation.
1629566cf16SZi Xuan Wu     adjustReg(MBB, MBBI, DL, SPReg, SPReg,
1639566cf16SZi Xuan Wu               -(static_cast<int64_t>(StackSize - ActualSize)),
1649566cf16SZi Xuan Wu               MachineInstr::NoFlags);
1659566cf16SZi Xuan Wu 
1669566cf16SZi Xuan Wu     // Realign Stack
1679566cf16SZi Xuan Wu     const CSKYRegisterInfo *RI = STI.getRegisterInfo();
1689566cf16SZi Xuan Wu     if (RI->hasStackRealignment(MF)) {
1699566cf16SZi Xuan Wu       Align MaxAlignment = MFI.getMaxAlign();
1709566cf16SZi Xuan Wu 
1719566cf16SZi Xuan Wu       const CSKYInstrInfo *TII = STI.getInstrInfo();
1729566cf16SZi Xuan Wu       if (STI.hasE2() && isUInt<12>(~(-(int)MaxAlignment.value()))) {
1739566cf16SZi Xuan Wu         BuildMI(MBB, MBBI, DL, TII->get(CSKY::ANDNI32), SPReg)
1749566cf16SZi Xuan Wu             .addReg(SPReg)
1759566cf16SZi Xuan Wu             .addImm(~(-(int)MaxAlignment.value()));
1769566cf16SZi Xuan Wu       } else {
1779566cf16SZi Xuan Wu         unsigned ShiftAmount = Log2(MaxAlignment);
1789566cf16SZi Xuan Wu 
1799566cf16SZi Xuan Wu         if (STI.hasE2()) {
1809566cf16SZi Xuan Wu           Register VR =
1819566cf16SZi Xuan Wu               MF.getRegInfo().createVirtualRegister(&CSKY::GPRRegClass);
1829566cf16SZi Xuan Wu           BuildMI(MBB, MBBI, DL, TII->get(CSKY::LSRI32), VR)
1839566cf16SZi Xuan Wu               .addReg(SPReg)
1849566cf16SZi Xuan Wu               .addImm(ShiftAmount);
1859566cf16SZi Xuan Wu           BuildMI(MBB, MBBI, DL, TII->get(CSKY::LSLI32), SPReg)
1869566cf16SZi Xuan Wu               .addReg(VR)
1879566cf16SZi Xuan Wu               .addImm(ShiftAmount);
1889566cf16SZi Xuan Wu         } else {
1899566cf16SZi Xuan Wu           Register VR =
1909566cf16SZi Xuan Wu               MF.getRegInfo().createVirtualRegister(&CSKY::mGPRRegClass);
1919566cf16SZi Xuan Wu           BuildMI(MBB, MBBI, DL, TII->get(CSKY::MOV16), VR).addReg(SPReg);
1929566cf16SZi Xuan Wu           BuildMI(MBB, MBBI, DL, TII->get(CSKY::LSRI16), VR)
1939566cf16SZi Xuan Wu               .addReg(VR)
1949566cf16SZi Xuan Wu               .addImm(ShiftAmount);
1959566cf16SZi Xuan Wu           BuildMI(MBB, MBBI, DL, TII->get(CSKY::LSLI16), VR)
1969566cf16SZi Xuan Wu               .addReg(VR)
1979566cf16SZi Xuan Wu               .addImm(ShiftAmount);
1989566cf16SZi Xuan Wu           BuildMI(MBB, MBBI, DL, TII->get(CSKY::MOV16), SPReg).addReg(VR);
1999566cf16SZi Xuan Wu         }
2009566cf16SZi Xuan Wu       }
2019566cf16SZi Xuan Wu     }
2029566cf16SZi Xuan Wu 
2039566cf16SZi Xuan Wu     // FP will be used to restore the frame in the epilogue, so we need
2049566cf16SZi Xuan Wu     // another base register BP to record SP after re-alignment. SP will
2059566cf16SZi Xuan Wu     // track the current stack after allocating variable sized objects.
2069566cf16SZi Xuan Wu     if (hasBP(MF)) {
2079566cf16SZi Xuan Wu       // move BP, SP
2089566cf16SZi Xuan Wu       BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::COPY), BPReg).addReg(SPReg);
2099566cf16SZi Xuan Wu     }
2109566cf16SZi Xuan Wu 
2119566cf16SZi Xuan Wu   } else {
2129566cf16SZi Xuan Wu     adjustReg(MBB, MBBI, DL, SPReg, SPReg,
2139566cf16SZi Xuan Wu               -(static_cast<int64_t>(StackSize - ActualSize)),
2149566cf16SZi Xuan Wu               MachineInstr::NoFlags);
2159566cf16SZi Xuan Wu     // Emit ".cfi_def_cfa_offset StackSize"
2169566cf16SZi Xuan Wu     unsigned CFIIndex = MF.addFrameInst(
2179566cf16SZi Xuan Wu         MCCFIInstruction::cfiDefCfaOffset(nullptr, MFI.getStackSize()));
2189566cf16SZi Xuan Wu     BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
2199566cf16SZi Xuan Wu         .addCFIIndex(CFIIndex);
2209566cf16SZi Xuan Wu   }
221cf78715cSZi Xuan Wu }
222cf78715cSZi Xuan Wu 
223cf78715cSZi Xuan Wu void CSKYFrameLowering::emitEpilogue(MachineFunction &MF,
224cf78715cSZi Xuan Wu                                      MachineBasicBlock &MBB) const {
2259566cf16SZi Xuan Wu   CSKYMachineFunctionInfo *CFI = MF.getInfo<CSKYMachineFunctionInfo>();
2269566cf16SZi Xuan Wu 
2279566cf16SZi Xuan Wu   MachineFrameInfo &MFI = MF.getFrameInfo();
2289566cf16SZi Xuan Wu   Register FPReg = getFPReg(STI);
2299566cf16SZi Xuan Wu   Register SPReg = CSKY::R14;
2309566cf16SZi Xuan Wu 
2319566cf16SZi Xuan Wu   // Get the insert location for the epilogue. If there were no terminators in
2329566cf16SZi Xuan Wu   // the block, get the last instruction.
2339566cf16SZi Xuan Wu   MachineBasicBlock::iterator MBBI = MBB.end();
2349566cf16SZi Xuan Wu   DebugLoc DL;
2359566cf16SZi Xuan Wu   if (!MBB.empty()) {
2369566cf16SZi Xuan Wu     MBBI = MBB.getFirstTerminator();
2379566cf16SZi Xuan Wu     if (MBBI == MBB.end())
2389566cf16SZi Xuan Wu       MBBI = MBB.getLastNonDebugInstr();
2399566cf16SZi Xuan Wu     DL = MBBI->getDebugLoc();
2409566cf16SZi Xuan Wu 
2419566cf16SZi Xuan Wu     // If this is not a terminator, the actual insert location should be after
2429566cf16SZi Xuan Wu     // the last instruction.
2439566cf16SZi Xuan Wu     if (!MBBI->isTerminator())
2449566cf16SZi Xuan Wu       MBBI = std::next(MBBI);
2459566cf16SZi Xuan Wu   }
2469566cf16SZi Xuan Wu 
2479566cf16SZi Xuan Wu   const auto &CSI = MFI.getCalleeSavedInfo();
2489566cf16SZi Xuan Wu   uint64_t StackSize = MFI.getStackSize();
2499566cf16SZi Xuan Wu 
2509566cf16SZi Xuan Wu   uint64_t ActualSize =
2519566cf16SZi Xuan Wu       CFI->getCalleeSaveAreaSize() + CFI->getVarArgsSaveSize();
2529566cf16SZi Xuan Wu 
2539566cf16SZi Xuan Wu   // Skip to before the restores of callee-saved registers
2549566cf16SZi Xuan Wu   // FIXME: assumes exactly one instruction is used to restore each
2559566cf16SZi Xuan Wu   // callee-saved register.
2569566cf16SZi Xuan Wu   auto LastFrameDestroy = MBBI;
2579566cf16SZi Xuan Wu   if (!CSI.empty())
2589566cf16SZi Xuan Wu     LastFrameDestroy = std::prev(MBBI, CSI.size());
2599566cf16SZi Xuan Wu 
2609566cf16SZi Xuan Wu   if (hasFP(MF)) {
2619566cf16SZi Xuan Wu     const CSKYInstrInfo *TII = STI.getInstrInfo();
2629566cf16SZi Xuan Wu     BuildMI(MBB, LastFrameDestroy, DL, TII->get(TargetOpcode::COPY), SPReg)
2639566cf16SZi Xuan Wu         .addReg(FPReg)
2649566cf16SZi Xuan Wu         .setMIFlag(MachineInstr::NoFlags);
2659566cf16SZi Xuan Wu   } else {
2669566cf16SZi Xuan Wu     adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg, (StackSize - ActualSize),
2679566cf16SZi Xuan Wu               MachineInstr::FrameDestroy);
2689566cf16SZi Xuan Wu   }
2699566cf16SZi Xuan Wu 
2709566cf16SZi Xuan Wu   adjustReg(MBB, MBBI, DL, SPReg, SPReg, ActualSize,
2719566cf16SZi Xuan Wu             MachineInstr::FrameDestroy);
2729566cf16SZi Xuan Wu }
2739566cf16SZi Xuan Wu 
27480fd9f3eSZi Xuan Wu (Zeson) static unsigned EstimateFunctionSizeInBytes(const MachineFunction &MF,
27580fd9f3eSZi Xuan Wu (Zeson)                                             const CSKYInstrInfo &TII) {
27680fd9f3eSZi Xuan Wu (Zeson)   unsigned FnSize = 0;
27780fd9f3eSZi Xuan Wu (Zeson)   for (auto &MBB : MF) {
27880fd9f3eSZi Xuan Wu (Zeson)     for (auto &MI : MBB)
27980fd9f3eSZi Xuan Wu (Zeson)       FnSize += TII.getInstSizeInBytes(MI);
28080fd9f3eSZi Xuan Wu (Zeson)   }
28180fd9f3eSZi Xuan Wu (Zeson)   FnSize += MF.getConstantPool()->getConstants().size() * 4;
28280fd9f3eSZi Xuan Wu (Zeson)   return FnSize;
28380fd9f3eSZi Xuan Wu (Zeson) }
28480fd9f3eSZi Xuan Wu (Zeson) 
2859566cf16SZi Xuan Wu static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
2869566cf16SZi Xuan Wu                                          const CSKYSubtarget &STI) {
2879566cf16SZi Xuan Wu   unsigned Limit = (1 << 12) - 1;
2889566cf16SZi Xuan Wu 
2899566cf16SZi Xuan Wu   for (auto &MBB : MF) {
2909566cf16SZi Xuan Wu     for (auto &MI : MBB) {
2919566cf16SZi Xuan Wu       if (MI.isDebugInstr())
2929566cf16SZi Xuan Wu         continue;
2939566cf16SZi Xuan Wu 
2949566cf16SZi Xuan Wu       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
2959566cf16SZi Xuan Wu         if (!MI.getOperand(i).isFI())
2969566cf16SZi Xuan Wu           continue;
2979566cf16SZi Xuan Wu 
2989566cf16SZi Xuan Wu         if (MI.getOpcode() == CSKY::SPILL_CARRY ||
2999566cf16SZi Xuan Wu             MI.getOpcode() == CSKY::RESTORE_CARRY ||
3009566cf16SZi Xuan Wu             MI.getOpcode() == CSKY::STORE_PAIR ||
3019566cf16SZi Xuan Wu             MI.getOpcode() == CSKY::LOAD_PAIR) {
3029566cf16SZi Xuan Wu           Limit = std::min(Limit, ((1U << 12) - 1) * 4);
3039566cf16SZi Xuan Wu           break;
3049566cf16SZi Xuan Wu         }
3059566cf16SZi Xuan Wu 
3069566cf16SZi Xuan Wu         if (MI.getOpcode() == CSKY::ADDI32) {
3079566cf16SZi Xuan Wu           Limit = std::min(Limit, (1U << 12));
3089566cf16SZi Xuan Wu           break;
3099566cf16SZi Xuan Wu         }
3109566cf16SZi Xuan Wu 
3119566cf16SZi Xuan Wu         if (MI.getOpcode() == CSKY::ADDI16XZ) {
3129566cf16SZi Xuan Wu           Limit = std::min(Limit, (1U << 3));
3139566cf16SZi Xuan Wu           break;
3149566cf16SZi Xuan Wu         }
3159566cf16SZi Xuan Wu 
3169566cf16SZi Xuan Wu         // ADDI16 will not require an extra register,
3179566cf16SZi Xuan Wu         // it can reuse the destination.
3189566cf16SZi Xuan Wu         if (MI.getOpcode() == CSKY::ADDI16)
3199566cf16SZi Xuan Wu           break;
3209566cf16SZi Xuan Wu 
3219566cf16SZi Xuan Wu         // Otherwise check the addressing mode.
3229566cf16SZi Xuan Wu         switch (MI.getDesc().TSFlags & CSKYII::AddrModeMask) {
3239566cf16SZi Xuan Wu         default:
3249566cf16SZi Xuan Wu           LLVM_DEBUG(MI.dump());
3259566cf16SZi Xuan Wu           llvm_unreachable(
3269566cf16SZi Xuan Wu               "Unhandled addressing mode in stack size limit calculation");
3279566cf16SZi Xuan Wu         case CSKYII::AddrMode32B:
3289566cf16SZi Xuan Wu           Limit = std::min(Limit, (1U << 12) - 1);
3299566cf16SZi Xuan Wu           break;
3309566cf16SZi Xuan Wu         case CSKYII::AddrMode32H:
3319566cf16SZi Xuan Wu           Limit = std::min(Limit, ((1U << 12) - 1) * 2);
3329566cf16SZi Xuan Wu           break;
3339566cf16SZi Xuan Wu         case CSKYII::AddrMode32WD:
3349566cf16SZi Xuan Wu           Limit = std::min(Limit, ((1U << 12) - 1) * 4);
3359566cf16SZi Xuan Wu           break;
3369566cf16SZi Xuan Wu         case CSKYII::AddrMode16B:
3379566cf16SZi Xuan Wu           Limit = std::min(Limit, (1U << 5) - 1);
3389566cf16SZi Xuan Wu           break;
3399566cf16SZi Xuan Wu         case CSKYII::AddrMode16H:
3409566cf16SZi Xuan Wu           Limit = std::min(Limit, ((1U << 5) - 1) * 2);
3419566cf16SZi Xuan Wu           break;
3429566cf16SZi Xuan Wu         case CSKYII::AddrMode16W:
3439566cf16SZi Xuan Wu           Limit = std::min(Limit, ((1U << 5) - 1) * 4);
3449566cf16SZi Xuan Wu           break;
3459566cf16SZi Xuan Wu         case CSKYII::AddrMode32SDF:
3469566cf16SZi Xuan Wu           Limit = std::min(Limit, ((1U << 8) - 1) * 4);
3479566cf16SZi Xuan Wu           break;
3489566cf16SZi Xuan Wu         }
3499566cf16SZi Xuan Wu         break; // At most one FI per instruction
3509566cf16SZi Xuan Wu       }
3519566cf16SZi Xuan Wu     }
3529566cf16SZi Xuan Wu   }
3539566cf16SZi Xuan Wu 
3549566cf16SZi Xuan Wu   return Limit;
3559566cf16SZi Xuan Wu }
3569566cf16SZi Xuan Wu 
3579566cf16SZi Xuan Wu void CSKYFrameLowering::determineCalleeSaves(MachineFunction &MF,
3589566cf16SZi Xuan Wu                                              BitVector &SavedRegs,
3599566cf16SZi Xuan Wu                                              RegScavenger *RS) const {
3609566cf16SZi Xuan Wu   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
3619566cf16SZi Xuan Wu 
3629566cf16SZi Xuan Wu   CSKYMachineFunctionInfo *CFI = MF.getInfo<CSKYMachineFunctionInfo>();
3639566cf16SZi Xuan Wu   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
36480fd9f3eSZi Xuan Wu (Zeson)   const CSKYInstrInfo *TII = STI.getInstrInfo();
3659566cf16SZi Xuan Wu   const MachineRegisterInfo &MRI = MF.getRegInfo();
3669566cf16SZi Xuan Wu   MachineFrameInfo &MFI = MF.getFrameInfo();
3679566cf16SZi Xuan Wu 
3689566cf16SZi Xuan Wu   if (hasFP(MF))
3699566cf16SZi Xuan Wu     SavedRegs.set(CSKY::R8);
3709566cf16SZi Xuan Wu 
3719566cf16SZi Xuan Wu   // Mark BP as used if function has dedicated base pointer.
3729566cf16SZi Xuan Wu   if (hasBP(MF))
3739566cf16SZi Xuan Wu     SavedRegs.set(CSKY::R7);
3749566cf16SZi Xuan Wu 
3759566cf16SZi Xuan Wu   // If interrupt is enabled and there are calls in the handler,
3769566cf16SZi Xuan Wu   // unconditionally save all Caller-saved registers and
3779566cf16SZi Xuan Wu   // all FP registers, regardless whether they are used.
3789566cf16SZi Xuan Wu   if (MF.getFunction().hasFnAttribute("interrupt") && MFI.hasCalls()) {
3799566cf16SZi Xuan Wu 
3809566cf16SZi Xuan Wu     static const MCPhysReg CSRegs[] = {CSKY::R0,  CSKY::R1,  CSKY::R2, CSKY::R3,
3819566cf16SZi Xuan Wu                                        CSKY::R12, CSKY::R13, 0};
3829566cf16SZi Xuan Wu 
3839566cf16SZi Xuan Wu     for (unsigned i = 0; CSRegs[i]; ++i)
3849566cf16SZi Xuan Wu       SavedRegs.set(CSRegs[i]);
3859566cf16SZi Xuan Wu 
3869566cf16SZi Xuan Wu     if (STI.hasHighRegisters()) {
3879566cf16SZi Xuan Wu 
3889566cf16SZi Xuan Wu       static const MCPhysReg CSHRegs[] = {CSKY::R18, CSKY::R19, CSKY::R20,
3899566cf16SZi Xuan Wu                                           CSKY::R21, CSKY::R22, CSKY::R23,
3909566cf16SZi Xuan Wu                                           CSKY::R24, CSKY::R25, 0};
3919566cf16SZi Xuan Wu 
3929566cf16SZi Xuan Wu       for (unsigned i = 0; CSHRegs[i]; ++i)
3939566cf16SZi Xuan Wu         SavedRegs.set(CSHRegs[i]);
3949566cf16SZi Xuan Wu     }
3959566cf16SZi Xuan Wu 
3969566cf16SZi Xuan Wu     static const MCPhysReg CSF32Regs[] = {
3979566cf16SZi Xuan Wu         CSKY::F8_32,  CSKY::F9_32,  CSKY::F10_32,
3989566cf16SZi Xuan Wu         CSKY::F11_32, CSKY::F12_32, CSKY::F13_32,
3999566cf16SZi Xuan Wu         CSKY::F14_32, CSKY::F15_32, 0};
4009566cf16SZi Xuan Wu     static const MCPhysReg CSF64Regs[] = {
4019566cf16SZi Xuan Wu         CSKY::F8_64,  CSKY::F9_64,  CSKY::F10_64,
4029566cf16SZi Xuan Wu         CSKY::F11_64, CSKY::F12_64, CSKY::F13_64,
4039566cf16SZi Xuan Wu         CSKY::F14_64, CSKY::F15_64, 0};
4049566cf16SZi Xuan Wu 
4059566cf16SZi Xuan Wu     const MCPhysReg *FRegs = NULL;
4069566cf16SZi Xuan Wu     if (STI.hasFPUv2DoubleFloat() || STI.hasFPUv3DoubleFloat())
4079566cf16SZi Xuan Wu       FRegs = CSF64Regs;
4089566cf16SZi Xuan Wu     else if (STI.hasFPUv2SingleFloat() || STI.hasFPUv3SingleFloat())
4099566cf16SZi Xuan Wu       FRegs = CSF32Regs;
4109566cf16SZi Xuan Wu 
4119566cf16SZi Xuan Wu     if (FRegs != NULL) {
4129566cf16SZi Xuan Wu       const MCPhysReg *Regs = MF.getRegInfo().getCalleeSavedRegs();
4139566cf16SZi Xuan Wu 
4149566cf16SZi Xuan Wu       for (unsigned i = 0; Regs[i]; ++i)
4159566cf16SZi Xuan Wu         if (CSKY::FPR32RegClass.contains(Regs[i]) ||
4169566cf16SZi Xuan Wu             CSKY::FPR64RegClass.contains(Regs[i])) {
4179566cf16SZi Xuan Wu           unsigned x = 0;
4189566cf16SZi Xuan Wu           for (; FRegs[x]; ++x)
4199566cf16SZi Xuan Wu             if (FRegs[x] == Regs[i])
4209566cf16SZi Xuan Wu               break;
4219566cf16SZi Xuan Wu           if (FRegs[x] == 0)
4229566cf16SZi Xuan Wu             SavedRegs.set(Regs[i]);
4239566cf16SZi Xuan Wu         }
4249566cf16SZi Xuan Wu     }
4259566cf16SZi Xuan Wu   }
4269566cf16SZi Xuan Wu 
4279566cf16SZi Xuan Wu   unsigned CSStackSize = 0;
4289566cf16SZi Xuan Wu   for (unsigned Reg : SavedRegs.set_bits()) {
4299566cf16SZi Xuan Wu     auto RegSize = TRI->getRegSizeInBits(Reg, MRI) / 8;
4309566cf16SZi Xuan Wu     CSStackSize += RegSize;
4319566cf16SZi Xuan Wu   }
4329566cf16SZi Xuan Wu 
4339566cf16SZi Xuan Wu   CFI->setCalleeSaveAreaSize(CSStackSize);
4349566cf16SZi Xuan Wu 
4359566cf16SZi Xuan Wu   uint64_t Limit = estimateRSStackSizeLimit(MF, STI);
4369566cf16SZi Xuan Wu 
4379566cf16SZi Xuan Wu   bool BigFrame = (MFI.estimateStackSize(MF) + CSStackSize >= Limit);
4389566cf16SZi Xuan Wu 
4399566cf16SZi Xuan Wu   if (BigFrame || CFI->isCRSpilled() || !STI.hasE2()) {
4409566cf16SZi Xuan Wu     const TargetRegisterClass *RC = &CSKY::GPRRegClass;
4419566cf16SZi Xuan Wu     unsigned size = TRI->getSpillSize(*RC);
4429566cf16SZi Xuan Wu     Align align = TRI->getSpillAlign(*RC);
4439566cf16SZi Xuan Wu 
444*1a935d7aSGuy David     RS->addScavengingFrameIndex(MFI.CreateSpillStackObject(size, align));
4459566cf16SZi Xuan Wu   }
44680fd9f3eSZi Xuan Wu (Zeson) 
44780fd9f3eSZi Xuan Wu (Zeson)   unsigned FnSize = EstimateFunctionSizeInBytes(MF, *TII);
44880fd9f3eSZi Xuan Wu (Zeson)   // Force R15 to be spilled if the function size is > 65534. This enables
44980fd9f3eSZi Xuan Wu (Zeson)   // use of BSR to implement far jump.
45080fd9f3eSZi Xuan Wu (Zeson)   if (FnSize >= ((1 << (16 - 1)) * 2))
45180fd9f3eSZi Xuan Wu (Zeson)     SavedRegs.set(CSKY::R15);
45280fd9f3eSZi Xuan Wu (Zeson) 
45380fd9f3eSZi Xuan Wu (Zeson)   CFI->setLRIsSpilled(SavedRegs.test(CSKY::R15));
4549566cf16SZi Xuan Wu }
4559566cf16SZi Xuan Wu 
4569566cf16SZi Xuan Wu // Not preserve stack space within prologue for outgoing variables when the
4579566cf16SZi Xuan Wu // function contains variable size objects and let eliminateCallFramePseudoInstr
4589566cf16SZi Xuan Wu // preserve stack space for it.
4599566cf16SZi Xuan Wu bool CSKYFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
4609566cf16SZi Xuan Wu   return !MF.getFrameInfo().hasVarSizedObjects();
4619566cf16SZi Xuan Wu }
4629566cf16SZi Xuan Wu 
4639566cf16SZi Xuan Wu bool CSKYFrameLowering::spillCalleeSavedRegisters(
4649566cf16SZi Xuan Wu     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
4659566cf16SZi Xuan Wu     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
4669566cf16SZi Xuan Wu   if (CSI.empty())
4679566cf16SZi Xuan Wu     return true;
4689566cf16SZi Xuan Wu 
4699566cf16SZi Xuan Wu   MachineFunction *MF = MBB.getParent();
4709566cf16SZi Xuan Wu   const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
4719566cf16SZi Xuan Wu   DebugLoc DL;
4729566cf16SZi Xuan Wu   if (MI != MBB.end() && !MI->isDebugInstr())
4739566cf16SZi Xuan Wu     DL = MI->getDebugLoc();
4749566cf16SZi Xuan Wu 
4759566cf16SZi Xuan Wu   for (auto &CS : CSI) {
4769566cf16SZi Xuan Wu     // Insert the spill to the stack frame.
4779566cf16SZi Xuan Wu     Register Reg = CS.getReg();
4789566cf16SZi Xuan Wu     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
479b5efec4bSChristudasan Devadasan     TII.storeRegToStackSlot(MBB, MI, Reg, true, CS.getFrameIdx(), RC, TRI,
480b5efec4bSChristudasan Devadasan                             Register());
4819566cf16SZi Xuan Wu   }
4829566cf16SZi Xuan Wu 
4839566cf16SZi Xuan Wu   return true;
4849566cf16SZi Xuan Wu }
4859566cf16SZi Xuan Wu 
4869566cf16SZi Xuan Wu bool CSKYFrameLowering::restoreCalleeSavedRegisters(
4879566cf16SZi Xuan Wu     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
4889566cf16SZi Xuan Wu     MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
4899566cf16SZi Xuan Wu   if (CSI.empty())
4909566cf16SZi Xuan Wu     return true;
4919566cf16SZi Xuan Wu 
4929566cf16SZi Xuan Wu   MachineFunction *MF = MBB.getParent();
4939566cf16SZi Xuan Wu   const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
4949566cf16SZi Xuan Wu   DebugLoc DL;
4959566cf16SZi Xuan Wu   if (MI != MBB.end() && !MI->isDebugInstr())
4969566cf16SZi Xuan Wu     DL = MI->getDebugLoc();
4979566cf16SZi Xuan Wu 
4989566cf16SZi Xuan Wu   for (auto &CS : reverse(CSI)) {
4999566cf16SZi Xuan Wu     Register Reg = CS.getReg();
5009566cf16SZi Xuan Wu     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
501b5efec4bSChristudasan Devadasan     TII.loadRegFromStackSlot(MBB, MI, Reg, CS.getFrameIdx(), RC, TRI,
502b5efec4bSChristudasan Devadasan                              Register());
5039566cf16SZi Xuan Wu     assert(MI != MBB.begin() && "loadRegFromStackSlot didn't insert any code!");
5049566cf16SZi Xuan Wu   }
5059566cf16SZi Xuan Wu 
5069566cf16SZi Xuan Wu   return true;
5079566cf16SZi Xuan Wu }
5089566cf16SZi Xuan Wu 
5099566cf16SZi Xuan Wu // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
5109566cf16SZi Xuan Wu MachineBasicBlock::iterator CSKYFrameLowering::eliminateCallFramePseudoInstr(
5119566cf16SZi Xuan Wu     MachineFunction &MF, MachineBasicBlock &MBB,
5129566cf16SZi Xuan Wu     MachineBasicBlock::iterator MI) const {
5139566cf16SZi Xuan Wu   Register SPReg = CSKY::R14;
5149566cf16SZi Xuan Wu   DebugLoc DL = MI->getDebugLoc();
5159566cf16SZi Xuan Wu 
5169566cf16SZi Xuan Wu   if (!hasReservedCallFrame(MF)) {
5179566cf16SZi Xuan Wu     // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
5189566cf16SZi Xuan Wu     // ADJCALLSTACKUP must be converted to instructions manipulating the stack
5199566cf16SZi Xuan Wu     // pointer. This is necessary when there is a variable length stack
5209566cf16SZi Xuan Wu     // allocation (e.g. alloca), which means it's not possible to allocate
5219566cf16SZi Xuan Wu     // space for outgoing arguments from within the function prologue.
5229566cf16SZi Xuan Wu     int64_t Amount = MI->getOperand(0).getImm();
5239566cf16SZi Xuan Wu 
5249566cf16SZi Xuan Wu     if (Amount != 0) {
5259566cf16SZi Xuan Wu       // Ensure the stack remains aligned after adjustment.
5269566cf16SZi Xuan Wu       Amount = alignSPAdjust(Amount);
5279566cf16SZi Xuan Wu 
5289566cf16SZi Xuan Wu       if (MI->getOpcode() == CSKY::ADJCALLSTACKDOWN)
5299566cf16SZi Xuan Wu         Amount = -Amount;
5309566cf16SZi Xuan Wu 
5319566cf16SZi Xuan Wu       adjustReg(MBB, MI, DL, SPReg, SPReg, Amount, MachineInstr::NoFlags);
5329566cf16SZi Xuan Wu     }
5339566cf16SZi Xuan Wu   }
5349566cf16SZi Xuan Wu 
5359566cf16SZi Xuan Wu   return MBB.erase(MI);
5369566cf16SZi Xuan Wu }
5379566cf16SZi Xuan Wu 
5389566cf16SZi Xuan Wu void CSKYFrameLowering::adjustReg(MachineBasicBlock &MBB,
5399566cf16SZi Xuan Wu                                   MachineBasicBlock::iterator MBBI,
5409566cf16SZi Xuan Wu                                   const DebugLoc &DL, Register DestReg,
5419566cf16SZi Xuan Wu                                   Register SrcReg, int64_t Val,
5429566cf16SZi Xuan Wu                                   MachineInstr::MIFlag Flag) const {
5439566cf16SZi Xuan Wu   const CSKYInstrInfo *TII = STI.getInstrInfo();
5449566cf16SZi Xuan Wu 
5459566cf16SZi Xuan Wu   if (DestReg == SrcReg && Val == 0)
5469566cf16SZi Xuan Wu     return;
5479566cf16SZi Xuan Wu 
5489566cf16SZi Xuan Wu   // TODO: Add 16-bit instruction support with immediate num
5499566cf16SZi Xuan Wu   if (STI.hasE2() && isUInt<12>(std::abs(Val) - 1)) {
5509566cf16SZi Xuan Wu     BuildMI(MBB, MBBI, DL, TII->get(Val < 0 ? CSKY::SUBI32 : CSKY::ADDI32),
5519566cf16SZi Xuan Wu             DestReg)
5529566cf16SZi Xuan Wu         .addReg(SrcReg)
5539566cf16SZi Xuan Wu         .addImm(std::abs(Val))
5549566cf16SZi Xuan Wu         .setMIFlag(Flag);
5559566cf16SZi Xuan Wu   } else if (!STI.hasE2() && isShiftedUInt<7, 2>(std::abs(Val))) {
5569566cf16SZi Xuan Wu     BuildMI(MBB, MBBI, DL,
5579566cf16SZi Xuan Wu             TII->get(Val < 0 ? CSKY::SUBI16SPSP : CSKY::ADDI16SPSP), CSKY::R14)
5589566cf16SZi Xuan Wu         .addReg(CSKY::R14, RegState::Kill)
5599566cf16SZi Xuan Wu         .addImm(std::abs(Val))
5609566cf16SZi Xuan Wu         .setMIFlag(Flag);
5619566cf16SZi Xuan Wu   } else {
5629566cf16SZi Xuan Wu 
5639566cf16SZi Xuan Wu     unsigned Op = 0;
5649566cf16SZi Xuan Wu 
5659566cf16SZi Xuan Wu     if (STI.hasE2()) {
5669566cf16SZi Xuan Wu       Op = Val < 0 ? CSKY::SUBU32 : CSKY::ADDU32;
5679566cf16SZi Xuan Wu     } else {
5689566cf16SZi Xuan Wu       assert(SrcReg == DestReg);
5699566cf16SZi Xuan Wu       Op = Val < 0 ? CSKY::SUBU16XZ : CSKY::ADDU16XZ;
5709566cf16SZi Xuan Wu     }
5719566cf16SZi Xuan Wu 
5729566cf16SZi Xuan Wu     Register ScratchReg = TII->movImm(MBB, MBBI, DL, std::abs(Val), Flag);
5739566cf16SZi Xuan Wu 
5749566cf16SZi Xuan Wu     BuildMI(MBB, MBBI, DL, TII->get(Op), DestReg)
5759566cf16SZi Xuan Wu         .addReg(SrcReg)
5769566cf16SZi Xuan Wu         .addReg(ScratchReg, RegState::Kill)
5779566cf16SZi Xuan Wu         .setMIFlag(Flag);
5789566cf16SZi Xuan Wu   }
5799566cf16SZi Xuan Wu }
5809566cf16SZi Xuan Wu 
5819566cf16SZi Xuan Wu StackOffset
5829566cf16SZi Xuan Wu CSKYFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
5839566cf16SZi Xuan Wu                                           Register &FrameReg) const {
5849566cf16SZi Xuan Wu   const CSKYMachineFunctionInfo *CFI = MF.getInfo<CSKYMachineFunctionInfo>();
5859566cf16SZi Xuan Wu   const MachineFrameInfo &MFI = MF.getFrameInfo();
5869566cf16SZi Xuan Wu   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
5879566cf16SZi Xuan Wu   const auto &CSI = MFI.getCalleeSavedInfo();
5889566cf16SZi Xuan Wu 
5899566cf16SZi Xuan Wu   int MinCSFI = 0;
5909566cf16SZi Xuan Wu   int MaxCSFI = -1;
5919566cf16SZi Xuan Wu 
5929566cf16SZi Xuan Wu   int Offset = MFI.getObjectOffset(FI) + MFI.getOffsetAdjustment();
5939566cf16SZi Xuan Wu 
5949566cf16SZi Xuan Wu   if (CSI.size()) {
5959566cf16SZi Xuan Wu     MinCSFI = CSI[0].getFrameIdx();
5969566cf16SZi Xuan Wu     MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
5979566cf16SZi Xuan Wu   }
5989566cf16SZi Xuan Wu 
5999566cf16SZi Xuan Wu   if (FI >= MinCSFI && FI <= MaxCSFI) {
6009566cf16SZi Xuan Wu     FrameReg = CSKY::R14;
6019566cf16SZi Xuan Wu     Offset += CFI->getVarArgsSaveSize() + CFI->getCalleeSaveAreaSize();
6029566cf16SZi Xuan Wu   } else if (RI->hasStackRealignment(MF)) {
6039566cf16SZi Xuan Wu     assert(hasFP(MF));
6049566cf16SZi Xuan Wu     if (!MFI.isFixedObjectIndex(FI)) {
6059566cf16SZi Xuan Wu       FrameReg = hasBP(MF) ? getBPReg(STI) : CSKY::R14;
6069566cf16SZi Xuan Wu       Offset += MFI.getStackSize();
6079566cf16SZi Xuan Wu     } else {
6089566cf16SZi Xuan Wu       FrameReg = getFPReg(STI);
6099566cf16SZi Xuan Wu       Offset += CFI->getVarArgsSaveSize() + CFI->getCalleeSaveAreaSize();
6109566cf16SZi Xuan Wu     }
6119566cf16SZi Xuan Wu   } else {
6129566cf16SZi Xuan Wu     if (MFI.isFixedObjectIndex(FI) && hasFP(MF)) {
6139566cf16SZi Xuan Wu       FrameReg = getFPReg(STI);
6149566cf16SZi Xuan Wu       Offset += CFI->getVarArgsSaveSize() + CFI->getCalleeSaveAreaSize();
6159566cf16SZi Xuan Wu     } else {
6169566cf16SZi Xuan Wu       FrameReg = hasBP(MF) ? getBPReg(STI) : CSKY::R14;
6179566cf16SZi Xuan Wu       Offset += MFI.getStackSize();
6189566cf16SZi Xuan Wu     }
6199566cf16SZi Xuan Wu   }
6209566cf16SZi Xuan Wu 
6219566cf16SZi Xuan Wu   return StackOffset::getFixed(Offset);
622cf78715cSZi Xuan Wu }
623