1dff0c46cSDimitry Andric //===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===//
22754fe60SDimitry Andric //
32754fe60SDimitry Andric //                     The LLVM Compiler Infrastructure
42754fe60SDimitry Andric //
52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source
62754fe60SDimitry Andric // License. See LICENSE.TXT for details.
72754fe60SDimitry Andric //
82754fe60SDimitry Andric //===----------------------------------------------------------------------===//
92754fe60SDimitry Andric //
102754fe60SDimitry Andric // This file contains the PPC implementation of TargetFrameLowering class.
112754fe60SDimitry Andric //
122754fe60SDimitry Andric //===----------------------------------------------------------------------===//
132754fe60SDimitry Andric 
142754fe60SDimitry Andric #include "PPCFrameLowering.h"
153861d79fSDimitry Andric #include "PPCInstrBuilder.h"
16139f7f9bSDimitry Andric #include "PPCInstrInfo.h"
172754fe60SDimitry Andric #include "PPCMachineFunctionInfo.h"
1891bc56edSDimitry Andric #include "PPCSubtarget.h"
19ff0cc061SDimitry Andric #include "PPCTargetMachine.h"
20*b5893f02SDimitry Andric #include "llvm/ADT/Statistic.h"
212754fe60SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
222754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
232754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
242754fe60SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
252754fe60SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
262754fe60SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
27139f7f9bSDimitry Andric #include "llvm/IR/Function.h"
282754fe60SDimitry Andric #include "llvm/Target/TargetOptions.h"
292754fe60SDimitry Andric 
302754fe60SDimitry Andric using namespace llvm;
312754fe60SDimitry Andric 
32*b5893f02SDimitry Andric #define DEBUG_TYPE "framelowering"
33*b5893f02SDimitry Andric STATISTIC(NumNoNeedForFrame, "Number of functions without frames");
34*b5893f02SDimitry Andric STATISTIC(NumPESpillVSR, "Number of spills to vector in prologue");
35*b5893f02SDimitry Andric STATISTIC(NumPEReloadVSR, "Number of reloads from vector in epilogue");
36*b5893f02SDimitry Andric 
37*b5893f02SDimitry Andric static cl::opt<bool>
38*b5893f02SDimitry Andric EnablePEVectorSpills("ppc-enable-pe-vector-spills",
39*b5893f02SDimitry Andric                      cl::desc("Enable spills in prologue to vector registers."),
40*b5893f02SDimitry Andric                      cl::init(false), cl::Hidden);
41*b5893f02SDimitry Andric 
422754fe60SDimitry Andric /// VRRegNo - Map from a numbered VR register to its enum value.
432754fe60SDimitry Andric ///
447d523365SDimitry Andric static const MCPhysReg VRRegNo[] = {
452754fe60SDimitry Andric  PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
462754fe60SDimitry Andric  PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
472754fe60SDimitry Andric  PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
482754fe60SDimitry Andric  PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
492754fe60SDimitry Andric };
502754fe60SDimitry Andric 
computeReturnSaveOffset(const PPCSubtarget & STI)51ff0cc061SDimitry Andric static unsigned computeReturnSaveOffset(const PPCSubtarget &STI) {
52ff0cc061SDimitry Andric   if (STI.isDarwinABI())
53ff0cc061SDimitry Andric     return STI.isPPC64() ? 16 : 8;
54ff0cc061SDimitry Andric   // SVR4 ABI:
55ff0cc061SDimitry Andric   return STI.isPPC64() ? 16 : 4;
56ff0cc061SDimitry Andric }
57ff0cc061SDimitry Andric 
computeTOCSaveOffset(const PPCSubtarget & STI)58ff0cc061SDimitry Andric static unsigned computeTOCSaveOffset(const PPCSubtarget &STI) {
59ff0cc061SDimitry Andric   return STI.isELFv2ABI() ? 24 : 40;
60ff0cc061SDimitry Andric }
61ff0cc061SDimitry Andric 
computeFramePointerSaveOffset(const PPCSubtarget & STI)62ff0cc061SDimitry Andric static unsigned computeFramePointerSaveOffset(const PPCSubtarget &STI) {
63ff0cc061SDimitry Andric   // For the Darwin ABI:
64ff0cc061SDimitry Andric   // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area
65ff0cc061SDimitry Andric   // for saving the frame pointer (if needed.)  While the published ABI has
66ff0cc061SDimitry Andric   // not used this slot since at least MacOSX 10.2, there is older code
67ff0cc061SDimitry Andric   // around that does use it, and that needs to continue to work.
68ff0cc061SDimitry Andric   if (STI.isDarwinABI())
69ff0cc061SDimitry Andric     return STI.isPPC64() ? -8U : -4U;
70ff0cc061SDimitry Andric 
71ff0cc061SDimitry Andric   // SVR4 ABI: First slot in the general register save area.
72ff0cc061SDimitry Andric   return STI.isPPC64() ? -8U : -4U;
73ff0cc061SDimitry Andric }
74ff0cc061SDimitry Andric 
computeLinkageSize(const PPCSubtarget & STI)75ff0cc061SDimitry Andric static unsigned computeLinkageSize(const PPCSubtarget &STI) {
76ff0cc061SDimitry Andric   if (STI.isDarwinABI() || STI.isPPC64())
77ff0cc061SDimitry Andric     return (STI.isELFv2ABI() ? 4 : 6) * (STI.isPPC64() ? 8 : 4);
78ff0cc061SDimitry Andric 
79ff0cc061SDimitry Andric   // SVR4 ABI:
80ff0cc061SDimitry Andric   return 8;
81ff0cc061SDimitry Andric }
82ff0cc061SDimitry Andric 
computeBasePointerSaveOffset(const PPCSubtarget & STI)83ff0cc061SDimitry Andric static unsigned computeBasePointerSaveOffset(const PPCSubtarget &STI) {
84ff0cc061SDimitry Andric   if (STI.isDarwinABI())
85ff0cc061SDimitry Andric     return STI.isPPC64() ? -16U : -8U;
86ff0cc061SDimitry Andric 
87ff0cc061SDimitry Andric   // SVR4 ABI: First slot in the general register save area.
88ff0cc061SDimitry Andric   return STI.isPPC64()
89ff0cc061SDimitry Andric              ? -16U
903ca95b02SDimitry Andric              : STI.getTargetMachine().isPositionIndependent() ? -12U : -8U;
91ff0cc061SDimitry Andric }
92ff0cc061SDimitry Andric 
PPCFrameLowering(const PPCSubtarget & STI)9391bc56edSDimitry Andric PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI)
9491bc56edSDimitry Andric     : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
95ff0cc061SDimitry Andric                           STI.getPlatformStackAlignment(), 0),
96ff0cc061SDimitry Andric       Subtarget(STI), ReturnSaveOffset(computeReturnSaveOffset(Subtarget)),
97ff0cc061SDimitry Andric       TOCSaveOffset(computeTOCSaveOffset(Subtarget)),
98ff0cc061SDimitry Andric       FramePointerSaveOffset(computeFramePointerSaveOffset(Subtarget)),
99ff0cc061SDimitry Andric       LinkageSize(computeLinkageSize(Subtarget)),
100ff0cc061SDimitry Andric       BasePointerSaveOffset(computeBasePointerSaveOffset(STI)) {}
10191bc56edSDimitry Andric 
10291bc56edSDimitry Andric // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
getCalleeSavedSpillSlots(unsigned & NumEntries) const10391bc56edSDimitry Andric const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots(
10491bc56edSDimitry Andric     unsigned &NumEntries) const {
10591bc56edSDimitry Andric   if (Subtarget.isDarwinABI()) {
10691bc56edSDimitry Andric     NumEntries = 1;
10791bc56edSDimitry Andric     if (Subtarget.isPPC64()) {
10891bc56edSDimitry Andric       static const SpillSlot darwin64Offsets = {PPC::X31, -8};
10991bc56edSDimitry Andric       return &darwin64Offsets;
11091bc56edSDimitry Andric     } else {
11191bc56edSDimitry Andric       static const SpillSlot darwinOffsets = {PPC::R31, -4};
11291bc56edSDimitry Andric       return &darwinOffsets;
11391bc56edSDimitry Andric     }
11491bc56edSDimitry Andric   }
11591bc56edSDimitry Andric 
11691bc56edSDimitry Andric   // Early exit if not using the SVR4 ABI.
11791bc56edSDimitry Andric   if (!Subtarget.isSVR4ABI()) {
11891bc56edSDimitry Andric     NumEntries = 0;
11991bc56edSDimitry Andric     return nullptr;
12091bc56edSDimitry Andric   }
12191bc56edSDimitry Andric 
12291bc56edSDimitry Andric   // Note that the offsets here overlap, but this is fixed up in
12391bc56edSDimitry Andric   // processFunctionBeforeFrameFinalized.
12491bc56edSDimitry Andric 
12591bc56edSDimitry Andric   static const SpillSlot Offsets[] = {
12691bc56edSDimitry Andric       // Floating-point register save area offsets.
12791bc56edSDimitry Andric       {PPC::F31, -8},
12891bc56edSDimitry Andric       {PPC::F30, -16},
12991bc56edSDimitry Andric       {PPC::F29, -24},
13091bc56edSDimitry Andric       {PPC::F28, -32},
13191bc56edSDimitry Andric       {PPC::F27, -40},
13291bc56edSDimitry Andric       {PPC::F26, -48},
13391bc56edSDimitry Andric       {PPC::F25, -56},
13491bc56edSDimitry Andric       {PPC::F24, -64},
13591bc56edSDimitry Andric       {PPC::F23, -72},
13691bc56edSDimitry Andric       {PPC::F22, -80},
13791bc56edSDimitry Andric       {PPC::F21, -88},
13891bc56edSDimitry Andric       {PPC::F20, -96},
13991bc56edSDimitry Andric       {PPC::F19, -104},
14091bc56edSDimitry Andric       {PPC::F18, -112},
14191bc56edSDimitry Andric       {PPC::F17, -120},
14291bc56edSDimitry Andric       {PPC::F16, -128},
14391bc56edSDimitry Andric       {PPC::F15, -136},
14491bc56edSDimitry Andric       {PPC::F14, -144},
14591bc56edSDimitry Andric 
14691bc56edSDimitry Andric       // General register save area offsets.
14791bc56edSDimitry Andric       {PPC::R31, -4},
14891bc56edSDimitry Andric       {PPC::R30, -8},
14991bc56edSDimitry Andric       {PPC::R29, -12},
15091bc56edSDimitry Andric       {PPC::R28, -16},
15191bc56edSDimitry Andric       {PPC::R27, -20},
15291bc56edSDimitry Andric       {PPC::R26, -24},
15391bc56edSDimitry Andric       {PPC::R25, -28},
15491bc56edSDimitry Andric       {PPC::R24, -32},
15591bc56edSDimitry Andric       {PPC::R23, -36},
15691bc56edSDimitry Andric       {PPC::R22, -40},
15791bc56edSDimitry Andric       {PPC::R21, -44},
15891bc56edSDimitry Andric       {PPC::R20, -48},
15991bc56edSDimitry Andric       {PPC::R19, -52},
16091bc56edSDimitry Andric       {PPC::R18, -56},
16191bc56edSDimitry Andric       {PPC::R17, -60},
16291bc56edSDimitry Andric       {PPC::R16, -64},
16391bc56edSDimitry Andric       {PPC::R15, -68},
16491bc56edSDimitry Andric       {PPC::R14, -72},
16591bc56edSDimitry Andric 
16691bc56edSDimitry Andric       // CR save area offset.  We map each of the nonvolatile CR fields
16791bc56edSDimitry Andric       // to the slot for CR2, which is the first of the nonvolatile CR
16891bc56edSDimitry Andric       // fields to be assigned, so that we only allocate one save slot.
16991bc56edSDimitry Andric       // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
17091bc56edSDimitry Andric       {PPC::CR2, -4},
17191bc56edSDimitry Andric 
17291bc56edSDimitry Andric       // VRSAVE save area offset.
17391bc56edSDimitry Andric       {PPC::VRSAVE, -4},
17491bc56edSDimitry Andric 
17591bc56edSDimitry Andric       // Vector register save area
17691bc56edSDimitry Andric       {PPC::V31, -16},
17791bc56edSDimitry Andric       {PPC::V30, -32},
17891bc56edSDimitry Andric       {PPC::V29, -48},
17991bc56edSDimitry Andric       {PPC::V28, -64},
18091bc56edSDimitry Andric       {PPC::V27, -80},
18191bc56edSDimitry Andric       {PPC::V26, -96},
18291bc56edSDimitry Andric       {PPC::V25, -112},
18391bc56edSDimitry Andric       {PPC::V24, -128},
18491bc56edSDimitry Andric       {PPC::V23, -144},
18591bc56edSDimitry Andric       {PPC::V22, -160},
18691bc56edSDimitry Andric       {PPC::V21, -176},
1874ba319b5SDimitry Andric       {PPC::V20, -192},
1884ba319b5SDimitry Andric 
1894ba319b5SDimitry Andric       // SPE register save area (overlaps Vector save area).
1904ba319b5SDimitry Andric       {PPC::S31, -8},
1914ba319b5SDimitry Andric       {PPC::S30, -16},
1924ba319b5SDimitry Andric       {PPC::S29, -24},
1934ba319b5SDimitry Andric       {PPC::S28, -32},
1944ba319b5SDimitry Andric       {PPC::S27, -40},
1954ba319b5SDimitry Andric       {PPC::S26, -48},
1964ba319b5SDimitry Andric       {PPC::S25, -56},
1974ba319b5SDimitry Andric       {PPC::S24, -64},
1984ba319b5SDimitry Andric       {PPC::S23, -72},
1994ba319b5SDimitry Andric       {PPC::S22, -80},
2004ba319b5SDimitry Andric       {PPC::S21, -88},
2014ba319b5SDimitry Andric       {PPC::S20, -96},
2024ba319b5SDimitry Andric       {PPC::S19, -104},
2034ba319b5SDimitry Andric       {PPC::S18, -112},
2044ba319b5SDimitry Andric       {PPC::S17, -120},
2054ba319b5SDimitry Andric       {PPC::S16, -128},
2064ba319b5SDimitry Andric       {PPC::S15, -136},
2074ba319b5SDimitry Andric       {PPC::S14, -144}};
20891bc56edSDimitry Andric 
20991bc56edSDimitry Andric   static const SpillSlot Offsets64[] = {
21091bc56edSDimitry Andric       // Floating-point register save area offsets.
21191bc56edSDimitry Andric       {PPC::F31, -8},
21291bc56edSDimitry Andric       {PPC::F30, -16},
21391bc56edSDimitry Andric       {PPC::F29, -24},
21491bc56edSDimitry Andric       {PPC::F28, -32},
21591bc56edSDimitry Andric       {PPC::F27, -40},
21691bc56edSDimitry Andric       {PPC::F26, -48},
21791bc56edSDimitry Andric       {PPC::F25, -56},
21891bc56edSDimitry Andric       {PPC::F24, -64},
21991bc56edSDimitry Andric       {PPC::F23, -72},
22091bc56edSDimitry Andric       {PPC::F22, -80},
22191bc56edSDimitry Andric       {PPC::F21, -88},
22291bc56edSDimitry Andric       {PPC::F20, -96},
22391bc56edSDimitry Andric       {PPC::F19, -104},
22491bc56edSDimitry Andric       {PPC::F18, -112},
22591bc56edSDimitry Andric       {PPC::F17, -120},
22691bc56edSDimitry Andric       {PPC::F16, -128},
22791bc56edSDimitry Andric       {PPC::F15, -136},
22891bc56edSDimitry Andric       {PPC::F14, -144},
22991bc56edSDimitry Andric 
23091bc56edSDimitry Andric       // General register save area offsets.
23191bc56edSDimitry Andric       {PPC::X31, -8},
23291bc56edSDimitry Andric       {PPC::X30, -16},
23391bc56edSDimitry Andric       {PPC::X29, -24},
23491bc56edSDimitry Andric       {PPC::X28, -32},
23591bc56edSDimitry Andric       {PPC::X27, -40},
23691bc56edSDimitry Andric       {PPC::X26, -48},
23791bc56edSDimitry Andric       {PPC::X25, -56},
23891bc56edSDimitry Andric       {PPC::X24, -64},
23991bc56edSDimitry Andric       {PPC::X23, -72},
24091bc56edSDimitry Andric       {PPC::X22, -80},
24191bc56edSDimitry Andric       {PPC::X21, -88},
24291bc56edSDimitry Andric       {PPC::X20, -96},
24391bc56edSDimitry Andric       {PPC::X19, -104},
24491bc56edSDimitry Andric       {PPC::X18, -112},
24591bc56edSDimitry Andric       {PPC::X17, -120},
24691bc56edSDimitry Andric       {PPC::X16, -128},
24791bc56edSDimitry Andric       {PPC::X15, -136},
24891bc56edSDimitry Andric       {PPC::X14, -144},
24991bc56edSDimitry Andric 
25091bc56edSDimitry Andric       // VRSAVE save area offset.
25191bc56edSDimitry Andric       {PPC::VRSAVE, -4},
25291bc56edSDimitry Andric 
25391bc56edSDimitry Andric       // Vector register save area
25491bc56edSDimitry Andric       {PPC::V31, -16},
25591bc56edSDimitry Andric       {PPC::V30, -32},
25691bc56edSDimitry Andric       {PPC::V29, -48},
25791bc56edSDimitry Andric       {PPC::V28, -64},
25891bc56edSDimitry Andric       {PPC::V27, -80},
25991bc56edSDimitry Andric       {PPC::V26, -96},
26091bc56edSDimitry Andric       {PPC::V25, -112},
26191bc56edSDimitry Andric       {PPC::V24, -128},
26291bc56edSDimitry Andric       {PPC::V23, -144},
26391bc56edSDimitry Andric       {PPC::V22, -160},
26491bc56edSDimitry Andric       {PPC::V21, -176},
26591bc56edSDimitry Andric       {PPC::V20, -192}};
26691bc56edSDimitry Andric 
26791bc56edSDimitry Andric   if (Subtarget.isPPC64()) {
26891bc56edSDimitry Andric     NumEntries = array_lengthof(Offsets64);
26991bc56edSDimitry Andric 
27091bc56edSDimitry Andric     return Offsets64;
27191bc56edSDimitry Andric   } else {
27291bc56edSDimitry Andric     NumEntries = array_lengthof(Offsets);
27391bc56edSDimitry Andric 
27491bc56edSDimitry Andric     return Offsets;
27591bc56edSDimitry Andric   }
27691bc56edSDimitry Andric }
27791bc56edSDimitry Andric 
2782754fe60SDimitry Andric /// RemoveVRSaveCode - We have found that this function does not need any code
2792754fe60SDimitry Andric /// to manipulate the VRSAVE register, even though it uses vector registers.
2802754fe60SDimitry Andric /// This can happen when the only registers used are known to be live in or out
2812754fe60SDimitry Andric /// of the function.  Remove all of the VRSAVE related code from the function.
2823861d79fSDimitry Andric /// FIXME: The removal of the code results in a compile failure at -O0 when the
2833861d79fSDimitry Andric /// function contains a function call, as the GPR containing original VRSAVE
2843861d79fSDimitry Andric /// contents is spilled and reloaded around the call.  Without the prolog code,
2853861d79fSDimitry Andric /// the spill instruction refers to an undefined register.  This code needs
2863861d79fSDimitry Andric /// to account for all uses of that GPR.
RemoveVRSaveCode(MachineInstr & MI)287d88c1a5aSDimitry Andric static void RemoveVRSaveCode(MachineInstr &MI) {
288d88c1a5aSDimitry Andric   MachineBasicBlock *Entry = MI.getParent();
2892754fe60SDimitry Andric   MachineFunction *MF = Entry->getParent();
2902754fe60SDimitry Andric 
2912754fe60SDimitry Andric   // We know that the MTVRSAVE instruction immediately follows MI.  Remove it.
2922754fe60SDimitry Andric   MachineBasicBlock::iterator MBBI = MI;
2932754fe60SDimitry Andric   ++MBBI;
2942754fe60SDimitry Andric   assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
2952754fe60SDimitry Andric   MBBI->eraseFromParent();
2962754fe60SDimitry Andric 
2972754fe60SDimitry Andric   bool RemovedAllMTVRSAVEs = true;
2982754fe60SDimitry Andric   // See if we can find and remove the MTVRSAVE instruction from all of the
2992754fe60SDimitry Andric   // epilog blocks.
3002754fe60SDimitry Andric   for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
3012754fe60SDimitry Andric     // If last instruction is a return instruction, add an epilogue
3027d523365SDimitry Andric     if (I->isReturnBlock()) {
3032754fe60SDimitry Andric       bool FoundIt = false;
3042754fe60SDimitry Andric       for (MBBI = I->end(); MBBI != I->begin(); ) {
3052754fe60SDimitry Andric         --MBBI;
3062754fe60SDimitry Andric         if (MBBI->getOpcode() == PPC::MTVRSAVE) {
3072754fe60SDimitry Andric           MBBI->eraseFromParent();  // remove it.
3082754fe60SDimitry Andric           FoundIt = true;
3092754fe60SDimitry Andric           break;
3102754fe60SDimitry Andric         }
3112754fe60SDimitry Andric       }
3122754fe60SDimitry Andric       RemovedAllMTVRSAVEs &= FoundIt;
3132754fe60SDimitry Andric     }
3142754fe60SDimitry Andric   }
3152754fe60SDimitry Andric 
3162754fe60SDimitry Andric   // If we found and removed all MTVRSAVE instructions, remove the read of
3172754fe60SDimitry Andric   // VRSAVE as well.
3182754fe60SDimitry Andric   if (RemovedAllMTVRSAVEs) {
3192754fe60SDimitry Andric     MBBI = MI;
3202754fe60SDimitry Andric     assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
3212754fe60SDimitry Andric     --MBBI;
3222754fe60SDimitry Andric     assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
3232754fe60SDimitry Andric     MBBI->eraseFromParent();
3242754fe60SDimitry Andric   }
3252754fe60SDimitry Andric 
3262754fe60SDimitry Andric   // Finally, nuke the UPDATE_VRSAVE.
327d88c1a5aSDimitry Andric   MI.eraseFromParent();
3282754fe60SDimitry Andric }
3292754fe60SDimitry Andric 
3302754fe60SDimitry Andric // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
3312754fe60SDimitry Andric // instruction selector.  Based on the vector registers that have been used,
3322754fe60SDimitry Andric // transform this into the appropriate ORI instruction.
HandleVRSaveUpdate(MachineInstr & MI,const TargetInstrInfo & TII)333d88c1a5aSDimitry Andric static void HandleVRSaveUpdate(MachineInstr &MI, const TargetInstrInfo &TII) {
334d88c1a5aSDimitry Andric   MachineFunction *MF = MI.getParent()->getParent();
33539d628a0SDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
336d88c1a5aSDimitry Andric   DebugLoc dl = MI.getDebugLoc();
3372754fe60SDimitry Andric 
3387d523365SDimitry Andric   const MachineRegisterInfo &MRI = MF->getRegInfo();
3392754fe60SDimitry Andric   unsigned UsedRegMask = 0;
3402754fe60SDimitry Andric   for (unsigned i = 0; i != 32; ++i)
3417d523365SDimitry Andric     if (MRI.isPhysRegModified(VRRegNo[i]))
3422754fe60SDimitry Andric       UsedRegMask |= 1 << (31-i);
3432754fe60SDimitry Andric 
3442754fe60SDimitry Andric   // Live in and live out values already must be in the mask, so don't bother
3452754fe60SDimitry Andric   // marking them.
3462cab237bSDimitry Andric   for (std::pair<unsigned, unsigned> LI : MF->getRegInfo().liveins()) {
3472cab237bSDimitry Andric     unsigned RegNo = TRI->getEncodingValue(LI.first);
3482cab237bSDimitry Andric     if (VRRegNo[RegNo] == LI.first)        // If this really is a vector reg.
3492754fe60SDimitry Andric       UsedRegMask &= ~(1 << (31-RegNo));   // Doesn't need to be marked.
3502754fe60SDimitry Andric   }
351139f7f9bSDimitry Andric 
352139f7f9bSDimitry Andric   // Live out registers appear as use operands on return instructions.
353139f7f9bSDimitry Andric   for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
354139f7f9bSDimitry Andric        UsedRegMask != 0 && BI != BE; ++BI) {
355139f7f9bSDimitry Andric     const MachineBasicBlock &MBB = *BI;
3567d523365SDimitry Andric     if (!MBB.isReturnBlock())
357139f7f9bSDimitry Andric       continue;
358139f7f9bSDimitry Andric     const MachineInstr &Ret = MBB.back();
359139f7f9bSDimitry Andric     for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
360139f7f9bSDimitry Andric       const MachineOperand &MO = Ret.getOperand(I);
361139f7f9bSDimitry Andric       if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
362139f7f9bSDimitry Andric         continue;
363139f7f9bSDimitry Andric       unsigned RegNo = TRI->getEncodingValue(MO.getReg());
364139f7f9bSDimitry Andric       UsedRegMask &= ~(1 << (31-RegNo));
365139f7f9bSDimitry Andric     }
3662754fe60SDimitry Andric   }
3672754fe60SDimitry Andric 
3682754fe60SDimitry Andric   // If no registers are used, turn this into a copy.
3692754fe60SDimitry Andric   if (UsedRegMask == 0) {
3702754fe60SDimitry Andric     // Remove all VRSAVE code.
3712754fe60SDimitry Andric     RemoveVRSaveCode(MI);
3722754fe60SDimitry Andric     return;
3732754fe60SDimitry Andric   }
3742754fe60SDimitry Andric 
375d88c1a5aSDimitry Andric   unsigned SrcReg = MI.getOperand(1).getReg();
376d88c1a5aSDimitry Andric   unsigned DstReg = MI.getOperand(0).getReg();
3772754fe60SDimitry Andric 
3782754fe60SDimitry Andric   if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
3792754fe60SDimitry Andric     if (DstReg != SrcReg)
380d88c1a5aSDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
3812754fe60SDimitry Andric           .addReg(SrcReg)
3822754fe60SDimitry Andric           .addImm(UsedRegMask);
3832754fe60SDimitry Andric     else
384d88c1a5aSDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
3852754fe60SDimitry Andric           .addReg(SrcReg, RegState::Kill)
3862754fe60SDimitry Andric           .addImm(UsedRegMask);
3872754fe60SDimitry Andric   } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
3882754fe60SDimitry Andric     if (DstReg != SrcReg)
389d88c1a5aSDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
3902754fe60SDimitry Andric           .addReg(SrcReg)
3912754fe60SDimitry Andric           .addImm(UsedRegMask >> 16);
3922754fe60SDimitry Andric     else
393d88c1a5aSDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
3942754fe60SDimitry Andric           .addReg(SrcReg, RegState::Kill)
3952754fe60SDimitry Andric           .addImm(UsedRegMask >> 16);
3962754fe60SDimitry Andric   } else {
3972754fe60SDimitry Andric     if (DstReg != SrcReg)
398d88c1a5aSDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
3992754fe60SDimitry Andric           .addReg(SrcReg)
4002754fe60SDimitry Andric           .addImm(UsedRegMask >> 16);
4012754fe60SDimitry Andric     else
402d88c1a5aSDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
4032754fe60SDimitry Andric           .addReg(SrcReg, RegState::Kill)
4042754fe60SDimitry Andric           .addImm(UsedRegMask >> 16);
4052754fe60SDimitry Andric 
406d88c1a5aSDimitry Andric     BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
4072754fe60SDimitry Andric         .addReg(DstReg, RegState::Kill)
4082754fe60SDimitry Andric         .addImm(UsedRegMask & 0xFFFF);
4092754fe60SDimitry Andric   }
4102754fe60SDimitry Andric 
4112754fe60SDimitry Andric   // Remove the old UPDATE_VRSAVE instruction.
412d88c1a5aSDimitry Andric   MI.eraseFromParent();
4132754fe60SDimitry Andric }
4142754fe60SDimitry Andric 
spillsCR(const MachineFunction & MF)4153861d79fSDimitry Andric static bool spillsCR(const MachineFunction &MF) {
4163861d79fSDimitry Andric   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
4173861d79fSDimitry Andric   return FuncInfo->isCRSpilled();
4183861d79fSDimitry Andric }
4193861d79fSDimitry Andric 
spillsVRSAVE(const MachineFunction & MF)420139f7f9bSDimitry Andric static bool spillsVRSAVE(const MachineFunction &MF) {
421139f7f9bSDimitry Andric   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
422139f7f9bSDimitry Andric   return FuncInfo->isVRSAVESpilled();
423139f7f9bSDimitry Andric }
424139f7f9bSDimitry Andric 
hasSpills(const MachineFunction & MF)425139f7f9bSDimitry Andric static bool hasSpills(const MachineFunction &MF) {
426139f7f9bSDimitry Andric   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
427139f7f9bSDimitry Andric   return FuncInfo->hasSpills();
428139f7f9bSDimitry Andric }
429139f7f9bSDimitry Andric 
hasNonRISpills(const MachineFunction & MF)430139f7f9bSDimitry Andric static bool hasNonRISpills(const MachineFunction &MF) {
431139f7f9bSDimitry Andric   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
432139f7f9bSDimitry Andric   return FuncInfo->hasNonRISpills();
433139f7f9bSDimitry Andric }
434139f7f9bSDimitry Andric 
435ff0cc061SDimitry Andric /// MustSaveLR - Return true if this function requires that we save the LR
436ff0cc061SDimitry Andric /// register onto the stack in the prolog and restore it in the epilog of the
437ff0cc061SDimitry Andric /// function.
MustSaveLR(const MachineFunction & MF,unsigned LR)438ff0cc061SDimitry Andric static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
439ff0cc061SDimitry Andric   const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
440ff0cc061SDimitry Andric 
441ff0cc061SDimitry Andric   // We need a save/restore of LR if there is any def of LR (which is
442ff0cc061SDimitry Andric   // defined by calls, including the PIC setup sequence), or if there is
443ff0cc061SDimitry Andric   // some use of the LR stack slot (e.g. for builtin_return_address).
444ff0cc061SDimitry Andric   // (LR comes in 32 and 64 bit versions.)
445ff0cc061SDimitry Andric   MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
446ff0cc061SDimitry Andric   return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
447ff0cc061SDimitry Andric }
448ff0cc061SDimitry Andric 
4492754fe60SDimitry Andric /// determineFrameLayout - Determine the size of the frame and maximum call
4502754fe60SDimitry Andric /// frame size.
determineFrameLayout(MachineFunction & MF,bool UpdateMF,bool UseEstimate) const451139f7f9bSDimitry Andric unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
452139f7f9bSDimitry Andric                                                 bool UpdateMF,
453139f7f9bSDimitry Andric                                                 bool UseEstimate) const {
454d88c1a5aSDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
4552754fe60SDimitry Andric 
4562754fe60SDimitry Andric   // Get the number of bytes to allocate from the FrameInfo
457139f7f9bSDimitry Andric   unsigned FrameSize =
458d88c1a5aSDimitry Andric     UseEstimate ? MFI.estimateStackSize(MF) : MFI.getStackSize();
4592754fe60SDimitry Andric 
460f785676fSDimitry Andric   // Get stack alignments. The frame must be aligned to the greatest of these:
461f785676fSDimitry Andric   unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
462d88c1a5aSDimitry Andric   unsigned MaxAlign = MFI.getMaxAlignment(); // algmt required by data in frame
463f785676fSDimitry Andric   unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
464f785676fSDimitry Andric 
4657a7e6055SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
4662754fe60SDimitry Andric 
467ff0cc061SDimitry Andric   unsigned LR = RegInfo->getRARegister();
4682cab237bSDimitry Andric   bool DisableRedZone = MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
469c4394386SDimitry Andric   bool CanUseRedZone = !MFI.hasVarSizedObjects() && // No dynamic alloca.
470d88c1a5aSDimitry Andric                        !MFI.adjustsStack() &&       // No calls.
471c4394386SDimitry Andric                        !MustSaveLR(MF, LR) &&       // No need to save LR.
472c4394386SDimitry Andric                        !RegInfo->hasBasePointer(MF); // No special alignment.
473c4394386SDimitry Andric 
474c4394386SDimitry Andric   // Note: for PPC32 SVR4ABI (Non-DarwinABI), we can still generate stackless
475c4394386SDimitry Andric   // code if all local vars are reg-allocated.
476c4394386SDimitry Andric   bool FitsInRedZone = FrameSize <= Subtarget.getRedZoneSize();
477c4394386SDimitry Andric 
478c4394386SDimitry Andric   // Check whether we can skip adjusting the stack pointer (by using red zone)
479c4394386SDimitry Andric   if (!DisableRedZone && CanUseRedZone && FitsInRedZone) {
480*b5893f02SDimitry Andric     NumNoNeedForFrame++;
4812754fe60SDimitry Andric     // No need for frame
482139f7f9bSDimitry Andric     if (UpdateMF)
483d88c1a5aSDimitry Andric       MFI.setStackSize(0);
484139f7f9bSDimitry Andric     return 0;
4852754fe60SDimitry Andric   }
4862754fe60SDimitry Andric 
4872754fe60SDimitry Andric   // Get the maximum call frame size of all the calls.
488d88c1a5aSDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
4892754fe60SDimitry Andric 
49091bc56edSDimitry Andric   // Maximum call frame needs to be at least big enough for linkage area.
491ff0cc061SDimitry Andric   unsigned minCallFrameSize = getLinkageSize();
4922754fe60SDimitry Andric   maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
4932754fe60SDimitry Andric 
4942754fe60SDimitry Andric   // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
4952754fe60SDimitry Andric   // that allocations will be aligned.
496d88c1a5aSDimitry Andric   if (MFI.hasVarSizedObjects())
4972754fe60SDimitry Andric     maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
4982754fe60SDimitry Andric 
4992754fe60SDimitry Andric   // Update maximum call frame size.
500139f7f9bSDimitry Andric   if (UpdateMF)
501d88c1a5aSDimitry Andric     MFI.setMaxCallFrameSize(maxCallFrameSize);
5022754fe60SDimitry Andric 
5032754fe60SDimitry Andric   // Include call frame size in total.
5042754fe60SDimitry Andric   FrameSize += maxCallFrameSize;
5052754fe60SDimitry Andric 
5062754fe60SDimitry Andric   // Make sure the frame is aligned.
5072754fe60SDimitry Andric   FrameSize = (FrameSize + AlignMask) & ~AlignMask;
5082754fe60SDimitry Andric 
5092754fe60SDimitry Andric   // Update frame info.
510139f7f9bSDimitry Andric   if (UpdateMF)
511d88c1a5aSDimitry Andric     MFI.setStackSize(FrameSize);
512139f7f9bSDimitry Andric 
513139f7f9bSDimitry Andric   return FrameSize;
5142754fe60SDimitry Andric }
5152754fe60SDimitry Andric 
5162754fe60SDimitry Andric // hasFP - Return true if the specified function actually has a dedicated frame
5172754fe60SDimitry Andric // pointer register.
hasFP(const MachineFunction & MF) const5182754fe60SDimitry Andric bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
519d88c1a5aSDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
5202754fe60SDimitry Andric   // FIXME: This is pretty much broken by design: hasFP() might be called really
5212754fe60SDimitry Andric   // early, before the stack layout was calculated and thus hasFP() might return
5222754fe60SDimitry Andric   // true or false here depending on the time of call.
523d88c1a5aSDimitry Andric   return (MFI.getStackSize()) && needsFP(MF);
5242754fe60SDimitry Andric }
5252754fe60SDimitry Andric 
5262754fe60SDimitry Andric // needsFP - Return true if the specified function should have a dedicated frame
5272754fe60SDimitry Andric // pointer register.  This is true if the function has variable sized allocas or
5282754fe60SDimitry Andric // if frame pointer elimination is disabled.
needsFP(const MachineFunction & MF) const5292754fe60SDimitry Andric bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
530d88c1a5aSDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
5312754fe60SDimitry Andric 
5322754fe60SDimitry Andric   // Naked functions have no stack frame pushed, so we don't have a frame
5332754fe60SDimitry Andric   // pointer.
5342cab237bSDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::Naked))
5352754fe60SDimitry Andric     return false;
5362754fe60SDimitry Andric 
537dff0c46cSDimitry Andric   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
538d88c1a5aSDimitry Andric     MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() ||
539dff0c46cSDimitry Andric     (MF.getTarget().Options.GuaranteedTailCallOpt &&
540dff0c46cSDimitry Andric      MF.getInfo<PPCFunctionInfo>()->hasFastCall());
5412754fe60SDimitry Andric }
5422754fe60SDimitry Andric 
replaceFPWithRealFP(MachineFunction & MF) const543139f7f9bSDimitry Andric void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
544139f7f9bSDimitry Andric   bool is31 = needsFP(MF);
545139f7f9bSDimitry Andric   unsigned FPReg  = is31 ? PPC::R31 : PPC::R1;
546139f7f9bSDimitry Andric   unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
547139f7f9bSDimitry Andric 
5487a7e6055SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
549f785676fSDimitry Andric   bool HasBP = RegInfo->hasBasePointer(MF);
55026e25074SRoman Divacky   unsigned BPReg  = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
551edd7eaddSDimitry Andric   unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FP8Reg;
552f785676fSDimitry Andric 
553139f7f9bSDimitry Andric   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
554139f7f9bSDimitry Andric        BI != BE; ++BI)
555139f7f9bSDimitry Andric     for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
556139f7f9bSDimitry Andric       --MBBI;
557139f7f9bSDimitry Andric       for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
558139f7f9bSDimitry Andric         MachineOperand &MO = MBBI->getOperand(I);
559139f7f9bSDimitry Andric         if (!MO.isReg())
560139f7f9bSDimitry Andric           continue;
561139f7f9bSDimitry Andric 
562139f7f9bSDimitry Andric         switch (MO.getReg()) {
563139f7f9bSDimitry Andric         case PPC::FP:
564139f7f9bSDimitry Andric           MO.setReg(FPReg);
565139f7f9bSDimitry Andric           break;
566139f7f9bSDimitry Andric         case PPC::FP8:
567139f7f9bSDimitry Andric           MO.setReg(FP8Reg);
568139f7f9bSDimitry Andric           break;
569f785676fSDimitry Andric         case PPC::BP:
570f785676fSDimitry Andric           MO.setReg(BPReg);
571f785676fSDimitry Andric           break;
572f785676fSDimitry Andric         case PPC::BP8:
573f785676fSDimitry Andric           MO.setReg(BP8Reg);
574f785676fSDimitry Andric           break;
575f785676fSDimitry Andric 
576139f7f9bSDimitry Andric         }
577139f7f9bSDimitry Andric       }
578139f7f9bSDimitry Andric     }
579139f7f9bSDimitry Andric }
5802754fe60SDimitry Andric 
58109a17a1eSDimitry Andric /*  This function will do the following:
58209a17a1eSDimitry Andric     - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12
58309a17a1eSDimitry Andric       respectively (defaults recommended by the ABI) and return true
58409a17a1eSDimitry Andric     - If MBB is not an entry block, initialize the register scavenger and look
58509a17a1eSDimitry Andric       for available registers.
58609a17a1eSDimitry Andric     - If the defaults (R0/R12) are available, return true
58709a17a1eSDimitry Andric     - If TwoUniqueRegsRequired is set to true, it looks for two unique
58809a17a1eSDimitry Andric       registers. Otherwise, look for a single available register.
58909a17a1eSDimitry Andric       - If the required registers are found, set SR1 and SR2 and return true.
59009a17a1eSDimitry Andric       - If the required registers are not found, set SR2 or both SR1 and SR2 to
59109a17a1eSDimitry Andric         PPC::NoRegister and return false.
59209a17a1eSDimitry Andric 
59309a17a1eSDimitry Andric     Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired
59409a17a1eSDimitry Andric     is not set, this function will attempt to find two different registers, but
59509a17a1eSDimitry Andric     still return true if only one register is available (and set SR1 == SR2).
59609a17a1eSDimitry Andric */
59709a17a1eSDimitry Andric bool
findScratchRegister(MachineBasicBlock * MBB,bool UseAtEnd,bool TwoUniqueRegsRequired,unsigned * SR1,unsigned * SR2) const59809a17a1eSDimitry Andric PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB,
5997d523365SDimitry Andric                                       bool UseAtEnd,
60009a17a1eSDimitry Andric                                       bool TwoUniqueRegsRequired,
60109a17a1eSDimitry Andric                                       unsigned *SR1,
60209a17a1eSDimitry Andric                                       unsigned *SR2) const {
6037d523365SDimitry Andric   RegScavenger RS;
6047d523365SDimitry Andric   unsigned R0 =  Subtarget.isPPC64() ? PPC::X0 : PPC::R0;
60509a17a1eSDimitry Andric   unsigned R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12;
6067d523365SDimitry Andric 
60709a17a1eSDimitry Andric   // Set the defaults for the two scratch registers.
60809a17a1eSDimitry Andric   if (SR1)
60909a17a1eSDimitry Andric     *SR1 = R0;
6107d523365SDimitry Andric 
61109a17a1eSDimitry Andric   if (SR2) {
61209a17a1eSDimitry Andric     assert (SR1 && "Asking for the second scratch register but not the first?");
61309a17a1eSDimitry Andric     *SR2 = R12;
61409a17a1eSDimitry Andric   }
61509a17a1eSDimitry Andric 
61609a17a1eSDimitry Andric   // If MBB is an entry or exit block, use R0 and R12 as the scratch registers.
6177d523365SDimitry Andric   if ((UseAtEnd && MBB->isReturnBlock()) ||
6187d523365SDimitry Andric       (!UseAtEnd && (&MBB->getParent()->front() == MBB)))
6197d523365SDimitry Andric     return true;
6207d523365SDimitry Andric 
6213ca95b02SDimitry Andric   RS.enterBasicBlock(*MBB);
6227d523365SDimitry Andric 
6237d523365SDimitry Andric   if (UseAtEnd && !MBB->empty()) {
62409a17a1eSDimitry Andric     // The scratch register will be used at the end of the block, so must
62509a17a1eSDimitry Andric     // consider all registers used within the block
6267d523365SDimitry Andric 
6277d523365SDimitry Andric     MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator();
6287d523365SDimitry Andric     // If no terminator, back iterator up to previous instruction.
6297d523365SDimitry Andric     if (MBBI == MBB->end())
6307d523365SDimitry Andric       MBBI = std::prev(MBBI);
6317d523365SDimitry Andric 
6327d523365SDimitry Andric     if (MBBI != MBB->begin())
6337d523365SDimitry Andric       RS.forward(MBBI);
6347d523365SDimitry Andric   }
6357d523365SDimitry Andric 
63609a17a1eSDimitry Andric   // If the two registers are available, we're all good.
63709a17a1eSDimitry Andric   // Note that we only return here if both R0 and R12 are available because
63809a17a1eSDimitry Andric   // although the function may not require two unique registers, it may benefit
63909a17a1eSDimitry Andric   // from having two so we should try to provide them.
64009a17a1eSDimitry Andric   if (!RS.isRegUsed(R0) && !RS.isRegUsed(R12))
6417d523365SDimitry Andric     return true;
6427d523365SDimitry Andric 
64309a17a1eSDimitry Andric   // Get the list of callee-saved registers for the target.
6447a7e6055SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
64509a17a1eSDimitry Andric   const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MBB->getParent());
6467d523365SDimitry Andric 
64709a17a1eSDimitry Andric   // Get all the available registers in the block.
64809a17a1eSDimitry Andric   BitVector BV = RS.getRegsAvailable(Subtarget.isPPC64() ? &PPC::G8RCRegClass :
64909a17a1eSDimitry Andric                                      &PPC::GPRCRegClass);
65009a17a1eSDimitry Andric 
65109a17a1eSDimitry Andric   // We shouldn't use callee-saved registers as scratch registers as they may be
65209a17a1eSDimitry Andric   // available when looking for a candidate block for shrink wrapping but not
65309a17a1eSDimitry Andric   // available when the actual prologue/epilogue is being emitted because they
65409a17a1eSDimitry Andric   // were added as live-in to the prologue block by PrologueEpilogueInserter.
65509a17a1eSDimitry Andric   for (int i = 0; CSRegs[i]; ++i)
65609a17a1eSDimitry Andric     BV.reset(CSRegs[i]);
65709a17a1eSDimitry Andric 
65809a17a1eSDimitry Andric   // Set the first scratch register to the first available one.
65909a17a1eSDimitry Andric   if (SR1) {
66009a17a1eSDimitry Andric     int FirstScratchReg = BV.find_first();
66109a17a1eSDimitry Andric     *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg;
66209a17a1eSDimitry Andric   }
66309a17a1eSDimitry Andric 
66409a17a1eSDimitry Andric   // If there is another one available, set the second scratch register to that.
66509a17a1eSDimitry Andric   // Otherwise, set it to either PPC::NoRegister if this function requires two
66609a17a1eSDimitry Andric   // or to whatever SR1 is set to if this function doesn't require two.
66709a17a1eSDimitry Andric   if (SR2) {
66809a17a1eSDimitry Andric     int SecondScratchReg = BV.find_next(*SR1);
66909a17a1eSDimitry Andric     if (SecondScratchReg != -1)
67009a17a1eSDimitry Andric       *SR2 = SecondScratchReg;
67109a17a1eSDimitry Andric     else
67209a17a1eSDimitry Andric       *SR2 = TwoUniqueRegsRequired ? (unsigned)PPC::NoRegister : *SR1;
67309a17a1eSDimitry Andric   }
67409a17a1eSDimitry Andric 
67509a17a1eSDimitry Andric   // Now that we've done our best to provide both registers, double check
67609a17a1eSDimitry Andric   // whether we were unable to provide enough.
6773ca95b02SDimitry Andric   if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U))
6787d523365SDimitry Andric     return false;
6797d523365SDimitry Andric 
6807d523365SDimitry Andric   return true;
6817d523365SDimitry Andric }
6827d523365SDimitry Andric 
68309a17a1eSDimitry Andric // We need a scratch register for spilling LR and for spilling CR. By default,
68409a17a1eSDimitry Andric // we use two scratch registers to hide latency. However, if only one scratch
68509a17a1eSDimitry Andric // register is available, we can adjust for that by not overlapping the spill
68609a17a1eSDimitry Andric // code. However, if we need to realign the stack (i.e. have a base pointer)
68709a17a1eSDimitry Andric // and the stack frame is large, we need two scratch registers.
68809a17a1eSDimitry Andric bool
twoUniqueScratchRegsRequired(MachineBasicBlock * MBB) const68909a17a1eSDimitry Andric PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const {
6907a7e6055SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
69109a17a1eSDimitry Andric   MachineFunction &MF = *(MBB->getParent());
69209a17a1eSDimitry Andric   bool HasBP = RegInfo->hasBasePointer(MF);
69309a17a1eSDimitry Andric   unsigned FrameSize = determineFrameLayout(MF, false);
69409a17a1eSDimitry Andric   int NegFrameSize = -FrameSize;
69509a17a1eSDimitry Andric   bool IsLargeFrame = !isInt<16>(NegFrameSize);
696d88c1a5aSDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
697d88c1a5aSDimitry Andric   unsigned MaxAlign = MFI.getMaxAlignment();
6989dbab393SDimitry Andric   bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
69909a17a1eSDimitry Andric 
7009dbab393SDimitry Andric   return (IsLargeFrame || !HasRedZone) && HasBP && MaxAlign > 1;
70109a17a1eSDimitry Andric }
70209a17a1eSDimitry Andric 
canUseAsPrologue(const MachineBasicBlock & MBB) const7037d523365SDimitry Andric bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
7047d523365SDimitry Andric   MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
7057d523365SDimitry Andric 
70609a17a1eSDimitry Andric   return findScratchRegister(TmpMBB, false,
70709a17a1eSDimitry Andric                              twoUniqueScratchRegsRequired(TmpMBB));
7087d523365SDimitry Andric }
7097d523365SDimitry Andric 
canUseAsEpilogue(const MachineBasicBlock & MBB) const7107d523365SDimitry Andric bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
7117d523365SDimitry Andric   MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
7127d523365SDimitry Andric 
71309a17a1eSDimitry Andric   return findScratchRegister(TmpMBB, true);
7147d523365SDimitry Andric }
7157d523365SDimitry Andric 
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const716ff0cc061SDimitry Andric void PPCFrameLowering::emitPrologue(MachineFunction &MF,
717ff0cc061SDimitry Andric                                     MachineBasicBlock &MBB) const {
7182754fe60SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.begin();
719d88c1a5aSDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
7207a7e6055SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
7217a7e6055SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
7222754fe60SDimitry Andric 
7232754fe60SDimitry Andric   MachineModuleInfo &MMI = MF.getMMI();
724f785676fSDimitry Andric   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
7252754fe60SDimitry Andric   DebugLoc dl;
7269cac79b3SDimitry Andric   bool needsCFI = MMI.hasDebugInfo() ||
7272cab237bSDimitry Andric     MF.getFunction().needsUnwindTableEntry();
7282754fe60SDimitry Andric 
729f785676fSDimitry Andric   // Get processor type.
730f785676fSDimitry Andric   bool isPPC64 = Subtarget.isPPC64();
731f785676fSDimitry Andric   // Get the ABI.
732f785676fSDimitry Andric   bool isSVR4ABI = Subtarget.isSVR4ABI();
73391bc56edSDimitry Andric   bool isELFv2ABI = Subtarget.isELFv2ABI();
734ff0cc061SDimitry Andric   assert((Subtarget.isDarwinABI() || isSVR4ABI) &&
735f785676fSDimitry Andric          "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
736f785676fSDimitry Andric 
7372754fe60SDimitry Andric   // Scan the prolog, looking for an UPDATE_VRSAVE instruction.  If we find it,
7382754fe60SDimitry Andric   // process it.
739f785676fSDimitry Andric   if (!isSVR4ABI)
7402754fe60SDimitry Andric     for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
7412754fe60SDimitry Andric       if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
742d88c1a5aSDimitry Andric         HandleVRSaveUpdate(*MBBI, TII);
7432754fe60SDimitry Andric         break;
7442754fe60SDimitry Andric       }
7452754fe60SDimitry Andric     }
7462754fe60SDimitry Andric 
7477d523365SDimitry Andric   // Move MBBI back to the beginning of the prologue block.
7482754fe60SDimitry Andric   MBBI = MBB.begin();
7492754fe60SDimitry Andric 
7502754fe60SDimitry Andric   // Work out frame sizes.
751139f7f9bSDimitry Andric   unsigned FrameSize = determineFrameLayout(MF);
7522754fe60SDimitry Andric   int NegFrameSize = -FrameSize;
753f785676fSDimitry Andric   if (!isInt<32>(NegFrameSize))
754f785676fSDimitry Andric     llvm_unreachable("Unhandled stack size!");
7552754fe60SDimitry Andric 
756d88c1a5aSDimitry Andric   if (MFI.isFrameAddressTaken())
757139f7f9bSDimitry Andric     replaceFPWithRealFP(MF);
758139f7f9bSDimitry Andric 
7592754fe60SDimitry Andric   // Check if the link register (LR) must be saved.
7602754fe60SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
7612754fe60SDimitry Andric   bool MustSaveLR = FI->mustSaveLR();
762f785676fSDimitry Andric   const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
76309a17a1eSDimitry Andric   bool MustSaveCR = !MustSaveCRs.empty();
764f785676fSDimitry Andric   // Do we have a frame pointer and/or base pointer for this function?
7652754fe60SDimitry Andric   bool HasFP = hasFP(MF);
766f785676fSDimitry Andric   bool HasBP = RegInfo->hasBasePointer(MF);
7679dbab393SDimitry Andric   bool HasRedZone = isPPC64 || !isSVR4ABI;
768f785676fSDimitry Andric 
769f785676fSDimitry Andric   unsigned SPReg       = isPPC64 ? PPC::X1  : PPC::R1;
77026e25074SRoman Divacky   unsigned BPReg       = RegInfo->getBaseRegister(MF);
771f785676fSDimitry Andric   unsigned FPReg       = isPPC64 ? PPC::X31 : PPC::R31;
772f785676fSDimitry Andric   unsigned LRReg       = isPPC64 ? PPC::LR8 : PPC::LR;
7737d523365SDimitry Andric   unsigned ScratchReg  = 0;
774f785676fSDimitry Andric   unsigned TempReg     = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
775f785676fSDimitry Andric   //  ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
776f785676fSDimitry Andric   const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
777f785676fSDimitry Andric                                                 : PPC::MFLR );
778f785676fSDimitry Andric   const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
779f785676fSDimitry Andric                                                  : PPC::STW );
780f785676fSDimitry Andric   const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
781f785676fSDimitry Andric                                                      : PPC::STWU );
782f785676fSDimitry Andric   const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
783f785676fSDimitry Andric                                                         : PPC::STWUX);
784f785676fSDimitry Andric   const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
785f785676fSDimitry Andric                                                           : PPC::LIS );
786f785676fSDimitry Andric   const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
787f785676fSDimitry Andric                                                  : PPC::ORI );
788f785676fSDimitry Andric   const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
789f785676fSDimitry Andric                                               : PPC::OR );
790f785676fSDimitry Andric   const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
791f785676fSDimitry Andric                                                             : PPC::SUBFC);
792f785676fSDimitry Andric   const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
793f785676fSDimitry Andric                                                                : PPC::SUBFIC);
794f785676fSDimitry Andric 
795f785676fSDimitry Andric   // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
796f785676fSDimitry Andric   // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
797f785676fSDimitry Andric   // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
798f785676fSDimitry Andric   // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
799f785676fSDimitry Andric   assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
800f785676fSDimitry Andric          "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
8012754fe60SDimitry Andric 
802d88c1a5aSDimitry Andric   // Using the same bool variable as below to suppress compiler warnings.
80309a17a1eSDimitry Andric   bool SingleScratchReg =
80409a17a1eSDimitry Andric     findScratchRegister(&MBB, false, twoUniqueScratchRegsRequired(&MBB),
80509a17a1eSDimitry Andric                         &ScratchReg, &TempReg);
80609a17a1eSDimitry Andric   assert(SingleScratchReg &&
80709a17a1eSDimitry Andric          "Required number of registers not available in this block");
80809a17a1eSDimitry Andric 
80909a17a1eSDimitry Andric   SingleScratchReg = ScratchReg == TempReg;
8107d523365SDimitry Andric 
811ff0cc061SDimitry Andric   int LROffset = getReturnSaveOffset();
8122754fe60SDimitry Andric 
8132754fe60SDimitry Andric   int FPOffset = 0;
8142754fe60SDimitry Andric   if (HasFP) {
815f785676fSDimitry Andric     if (isSVR4ABI) {
816d88c1a5aSDimitry Andric       MachineFrameInfo &MFI = MF.getFrameInfo();
8172754fe60SDimitry Andric       int FPIndex = FI->getFramePointerSaveIndex();
8182754fe60SDimitry Andric       assert(FPIndex && "No Frame Pointer Save Slot!");
819d88c1a5aSDimitry Andric       FPOffset = MFI.getObjectOffset(FPIndex);
8202754fe60SDimitry Andric     } else {
821ff0cc061SDimitry Andric       FPOffset = getFramePointerSaveOffset();
8222754fe60SDimitry Andric     }
8232754fe60SDimitry Andric   }
8242754fe60SDimitry Andric 
825f785676fSDimitry Andric   int BPOffset = 0;
826f785676fSDimitry Andric   if (HasBP) {
827f785676fSDimitry Andric     if (isSVR4ABI) {
828d88c1a5aSDimitry Andric       MachineFrameInfo &MFI = MF.getFrameInfo();
829f785676fSDimitry Andric       int BPIndex = FI->getBasePointerSaveIndex();
830f785676fSDimitry Andric       assert(BPIndex && "No Base Pointer Save Slot!");
831d88c1a5aSDimitry Andric       BPOffset = MFI.getObjectOffset(BPIndex);
832f785676fSDimitry Andric     } else {
833ff0cc061SDimitry Andric       BPOffset = getBasePointerSaveOffset();
834f785676fSDimitry Andric     }
835f785676fSDimitry Andric   }
8362754fe60SDimitry Andric 
83739d628a0SDimitry Andric   int PBPOffset = 0;
83839d628a0SDimitry Andric   if (FI->usesPICBase()) {
839d88c1a5aSDimitry Andric     MachineFrameInfo &MFI = MF.getFrameInfo();
84039d628a0SDimitry Andric     int PBPIndex = FI->getPICBasePointerSaveIndex();
84139d628a0SDimitry Andric     assert(PBPIndex && "No PIC Base Pointer Save Slot!");
842d88c1a5aSDimitry Andric     PBPOffset = MFI.getObjectOffset(PBPIndex);
84339d628a0SDimitry Andric   }
84439d628a0SDimitry Andric 
845f785676fSDimitry Andric   // Get stack alignments.
846d88c1a5aSDimitry Andric   unsigned MaxAlign = MFI.getMaxAlignment();
847f785676fSDimitry Andric   if (HasBP && MaxAlign > 1)
848f785676fSDimitry Andric     assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
849f785676fSDimitry Andric            "Invalid alignment!");
850f785676fSDimitry Andric 
851f785676fSDimitry Andric   // Frames of 32KB & larger require special handling because they cannot be
852f785676fSDimitry Andric   // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
853f785676fSDimitry Andric   bool isLargeFrame = !isInt<16>(NegFrameSize);
854f785676fSDimitry Andric 
85509a17a1eSDimitry Andric   assert((isPPC64 || !MustSaveCR) &&
85609a17a1eSDimitry Andric          "Prologue CR saving supported only in 64-bit mode");
85709a17a1eSDimitry Andric 
85809a17a1eSDimitry Andric   // If we need to spill the CR and the LR but we don't have two separate
85909a17a1eSDimitry Andric   // registers available, we must spill them one at a time
86009a17a1eSDimitry Andric   if (MustSaveCR && SingleScratchReg && MustSaveLR) {
8613ca95b02SDimitry Andric     // In the ELFv2 ABI, we are not required to save all CR fields.
8623ca95b02SDimitry Andric     // If only one or two CR fields are clobbered, it is more efficient to use
8633ca95b02SDimitry Andric     // mfocrf to selectively save just those fields, because mfocrf has short
8643ca95b02SDimitry Andric     // latency compares to mfcr.
8653ca95b02SDimitry Andric     unsigned MfcrOpcode = PPC::MFCR8;
8663ca95b02SDimitry Andric     unsigned CrState = RegState::ImplicitKill;
8673ca95b02SDimitry Andric     if (isELFv2ABI && MustSaveCRs.size() == 1) {
8683ca95b02SDimitry Andric       MfcrOpcode = PPC::MFOCRF8;
8693ca95b02SDimitry Andric       CrState = RegState::Kill;
8703ca95b02SDimitry Andric     }
87109a17a1eSDimitry Andric     MachineInstrBuilder MIB =
8723ca95b02SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
87309a17a1eSDimitry Andric     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
8743ca95b02SDimitry Andric       MIB.addReg(MustSaveCRs[i], CrState);
87509a17a1eSDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
87609a17a1eSDimitry Andric       .addReg(TempReg, getKillRegState(true))
87709a17a1eSDimitry Andric       .addImm(8)
87809a17a1eSDimitry Andric       .addReg(SPReg);
87909a17a1eSDimitry Andric   }
88009a17a1eSDimitry Andric 
881f785676fSDimitry Andric   if (MustSaveLR)
882f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
883f785676fSDimitry Andric 
88409a17a1eSDimitry Andric   if (MustSaveCR &&
88509a17a1eSDimitry Andric       !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
8863ca95b02SDimitry Andric     // In the ELFv2 ABI, we are not required to save all CR fields.
8873ca95b02SDimitry Andric     // If only one or two CR fields are clobbered, it is more efficient to use
8883ca95b02SDimitry Andric     // mfocrf to selectively save just those fields, because mfocrf has short
8893ca95b02SDimitry Andric     // latency compares to mfcr.
8903ca95b02SDimitry Andric     unsigned MfcrOpcode = PPC::MFCR8;
8913ca95b02SDimitry Andric     unsigned CrState = RegState::ImplicitKill;
8923ca95b02SDimitry Andric     if (isELFv2ABI && MustSaveCRs.size() == 1) {
8933ca95b02SDimitry Andric       MfcrOpcode = PPC::MFOCRF8;
8943ca95b02SDimitry Andric       CrState = RegState::Kill;
8953ca95b02SDimitry Andric     }
896284c1978SDimitry Andric     MachineInstrBuilder MIB =
8973ca95b02SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
898284c1978SDimitry Andric     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
8993ca95b02SDimitry Andric       MIB.addReg(MustSaveCRs[i], CrState);
900284c1978SDimitry Andric   }
901284c1978SDimitry Andric 
9029dbab393SDimitry Andric   if (HasRedZone) {
9032754fe60SDimitry Andric     if (HasFP)
904f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, StoreInst)
905f785676fSDimitry Andric         .addReg(FPReg)
9062754fe60SDimitry Andric         .addImm(FPOffset)
907f785676fSDimitry Andric         .addReg(SPReg);
90839d628a0SDimitry Andric     if (FI->usesPICBase())
90939d628a0SDimitry Andric       BuildMI(MBB, MBBI, dl, StoreInst)
91039d628a0SDimitry Andric         .addReg(PPC::R30)
91139d628a0SDimitry Andric         .addImm(PBPOffset)
91239d628a0SDimitry Andric         .addReg(SPReg);
913f785676fSDimitry Andric     if (HasBP)
914f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, StoreInst)
915f785676fSDimitry Andric         .addReg(BPReg)
916f785676fSDimitry Andric         .addImm(BPOffset)
917f785676fSDimitry Andric         .addReg(SPReg);
9189dbab393SDimitry Andric   }
919284c1978SDimitry Andric 
9202754fe60SDimitry Andric   if (MustSaveLR)
921f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, StoreInst)
9223ca95b02SDimitry Andric       .addReg(ScratchReg, getKillRegState(true))
9232754fe60SDimitry Andric       .addImm(LROffset)
924f785676fSDimitry Andric       .addReg(SPReg);
9252754fe60SDimitry Andric 
92609a17a1eSDimitry Andric   if (MustSaveCR &&
9279dbab393SDimitry Andric       !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
9289dbab393SDimitry Andric     assert(HasRedZone && "A red zone is always available on PPC64");
929f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
930f785676fSDimitry Andric       .addReg(TempReg, getKillRegState(true))
931f785676fSDimitry Andric       .addImm(8)
932f785676fSDimitry Andric       .addReg(SPReg);
9339dbab393SDimitry Andric   }
934f785676fSDimitry Andric 
935f785676fSDimitry Andric   // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
9369dbab393SDimitry Andric   if (!FrameSize)
9379dbab393SDimitry Andric     return;
9382754fe60SDimitry Andric 
9392754fe60SDimitry Andric   // Adjust stack pointer: r1 += NegFrameSize.
9402754fe60SDimitry Andric   // If there is a preferred stack alignment, align R1 now
9412754fe60SDimitry Andric 
9429dbab393SDimitry Andric   if (HasBP && HasRedZone) {
943f785676fSDimitry Andric     // Save a copy of r1 as the base pointer.
944f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, OrInst, BPReg)
945f785676fSDimitry Andric       .addReg(SPReg)
946f785676fSDimitry Andric       .addReg(SPReg);
947f785676fSDimitry Andric   }
948f785676fSDimitry Andric 
9499dbab393SDimitry Andric   // Have we generated a STUX instruction to claim stack frame? If so,
95026aa2dc5SDimitry Andric   // the negated frame size will be placed in ScratchReg.
9519dbab393SDimitry Andric   bool HasSTUX = false;
9529dbab393SDimitry Andric 
95309a17a1eSDimitry Andric   // This condition must be kept in sync with canUseAsPrologue.
954f785676fSDimitry Andric   if (HasBP && MaxAlign > 1) {
955f785676fSDimitry Andric     if (isPPC64)
956f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
957f785676fSDimitry Andric         .addReg(SPReg)
958f785676fSDimitry Andric         .addImm(0)
959f785676fSDimitry Andric         .addImm(64 - Log2_32(MaxAlign));
960f785676fSDimitry Andric     else // PPC32...
961f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
962f785676fSDimitry Andric         .addReg(SPReg)
9632754fe60SDimitry Andric         .addImm(0)
9642754fe60SDimitry Andric         .addImm(32 - Log2_32(MaxAlign))
9652754fe60SDimitry Andric         .addImm(31);
966f785676fSDimitry Andric     if (!isLargeFrame) {
967f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
968f785676fSDimitry Andric         .addReg(ScratchReg, RegState::Kill)
9692754fe60SDimitry Andric         .addImm(NegFrameSize);
970f785676fSDimitry Andric     } else {
97109a17a1eSDimitry Andric       assert(!SingleScratchReg && "Only a single scratch reg available");
972f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
973f785676fSDimitry Andric         .addImm(NegFrameSize >> 16);
974f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
975f785676fSDimitry Andric         .addReg(TempReg, RegState::Kill)
976f785676fSDimitry Andric         .addImm(NegFrameSize & 0xFFFF);
977f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
978f785676fSDimitry Andric         .addReg(ScratchReg, RegState::Kill)
979f785676fSDimitry Andric         .addReg(TempReg, RegState::Kill);
980f785676fSDimitry Andric     }
9819dbab393SDimitry Andric 
982f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
983f785676fSDimitry Andric       .addReg(SPReg, RegState::Kill)
984f785676fSDimitry Andric       .addReg(SPReg)
985f785676fSDimitry Andric       .addReg(ScratchReg);
9869dbab393SDimitry Andric     HasSTUX = true;
987f785676fSDimitry Andric 
988f785676fSDimitry Andric   } else if (!isLargeFrame) {
989f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
990f785676fSDimitry Andric       .addReg(SPReg)
9912754fe60SDimitry Andric       .addImm(NegFrameSize)
992f785676fSDimitry Andric       .addReg(SPReg);
9932754fe60SDimitry Andric 
9942754fe60SDimitry Andric   } else {
995f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
9962754fe60SDimitry Andric       .addImm(NegFrameSize >> 16);
997f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
998f785676fSDimitry Andric       .addReg(ScratchReg, RegState::Kill)
9992754fe60SDimitry Andric       .addImm(NegFrameSize & 0xFFFF);
1000f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
1001f785676fSDimitry Andric       .addReg(SPReg, RegState::Kill)
1002f785676fSDimitry Andric       .addReg(SPReg)
1003f785676fSDimitry Andric       .addReg(ScratchReg);
10049dbab393SDimitry Andric     HasSTUX = true;
10059dbab393SDimitry Andric   }
10069dbab393SDimitry Andric 
10079dbab393SDimitry Andric   if (!HasRedZone) {
10089dbab393SDimitry Andric     assert(!isPPC64 && "A red zone is always available on PPC64");
10099dbab393SDimitry Andric     if (HasSTUX) {
101026aa2dc5SDimitry Andric       // The negated frame size is in ScratchReg, and the SPReg has been
101126aa2dc5SDimitry Andric       // decremented by the frame size: SPReg = old SPReg + ScratchReg.
101226aa2dc5SDimitry Andric       // Since FPOffset, PBPOffset, etc. are relative to the beginning of
101326aa2dc5SDimitry Andric       // the stack frame (i.e. the old SP), ideally, we would put the old
101426aa2dc5SDimitry Andric       // SP into a register and use it as the base for the stores. The
101526aa2dc5SDimitry Andric       // problem is that the only available register may be ScratchReg,
101626aa2dc5SDimitry Andric       // which could be R0, and R0 cannot be used as a base address.
101726aa2dc5SDimitry Andric 
101826aa2dc5SDimitry Andric       // First, set ScratchReg to the old SP. This may need to be modified
101926aa2dc5SDimitry Andric       // later.
10209dbab393SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBF), ScratchReg)
10219dbab393SDimitry Andric         .addReg(ScratchReg, RegState::Kill)
10229dbab393SDimitry Andric         .addReg(SPReg);
10239dbab393SDimitry Andric 
102426aa2dc5SDimitry Andric       if (ScratchReg == PPC::R0) {
102526aa2dc5SDimitry Andric         // R0 cannot be used as a base register, but it can be used as an
102626aa2dc5SDimitry Andric         // index in a store-indexed.
102726aa2dc5SDimitry Andric         int LastOffset = 0;
102826aa2dc5SDimitry Andric         if (HasFP)  {
102926aa2dc5SDimitry Andric           // R0 += (FPOffset-LastOffset).
103026aa2dc5SDimitry Andric           // Need addic, since addi treats R0 as 0.
103126aa2dc5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
103226aa2dc5SDimitry Andric             .addReg(ScratchReg)
103326aa2dc5SDimitry Andric             .addImm(FPOffset-LastOffset);
103426aa2dc5SDimitry Andric           LastOffset = FPOffset;
103526aa2dc5SDimitry Andric           // Store FP into *R0.
103626aa2dc5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
103726aa2dc5SDimitry Andric             .addReg(FPReg, RegState::Kill)  // Save FP.
103826aa2dc5SDimitry Andric             .addReg(PPC::ZERO)
103926aa2dc5SDimitry Andric             .addReg(ScratchReg);  // This will be the index (R0 is ok here).
104026aa2dc5SDimitry Andric         }
104126aa2dc5SDimitry Andric         if (FI->usesPICBase()) {
104226aa2dc5SDimitry Andric           // R0 += (PBPOffset-LastOffset).
104326aa2dc5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
104426aa2dc5SDimitry Andric             .addReg(ScratchReg)
104526aa2dc5SDimitry Andric             .addImm(PBPOffset-LastOffset);
104626aa2dc5SDimitry Andric           LastOffset = PBPOffset;
104726aa2dc5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
104826aa2dc5SDimitry Andric             .addReg(PPC::R30, RegState::Kill)  // Save PIC base pointer.
104926aa2dc5SDimitry Andric             .addReg(PPC::ZERO)
105026aa2dc5SDimitry Andric             .addReg(ScratchReg);  // This will be the index (R0 is ok here).
105126aa2dc5SDimitry Andric         }
105226aa2dc5SDimitry Andric         if (HasBP) {
105326aa2dc5SDimitry Andric           // R0 += (BPOffset-LastOffset).
105426aa2dc5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
105526aa2dc5SDimitry Andric             .addReg(ScratchReg)
105626aa2dc5SDimitry Andric             .addImm(BPOffset-LastOffset);
105726aa2dc5SDimitry Andric           LastOffset = BPOffset;
105826aa2dc5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
105926aa2dc5SDimitry Andric             .addReg(BPReg, RegState::Kill)  // Save BP.
106026aa2dc5SDimitry Andric             .addReg(PPC::ZERO)
106126aa2dc5SDimitry Andric             .addReg(ScratchReg);  // This will be the index (R0 is ok here).
106226aa2dc5SDimitry Andric           // BP = R0-LastOffset
106326aa2dc5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), BPReg)
106426aa2dc5SDimitry Andric             .addReg(ScratchReg, RegState::Kill)
106526aa2dc5SDimitry Andric             .addImm(-LastOffset);
106626aa2dc5SDimitry Andric         }
106726aa2dc5SDimitry Andric       } else {
106826aa2dc5SDimitry Andric         // ScratchReg is not R0, so use it as the base register. It is
106926aa2dc5SDimitry Andric         // already set to the old SP, so we can use the offsets directly.
107026aa2dc5SDimitry Andric 
10719dbab393SDimitry Andric         // Now that the stack frame has been allocated, save all the necessary
10729dbab393SDimitry Andric         // registers using ScratchReg as the base address.
10739dbab393SDimitry Andric         if (HasFP)
10749dbab393SDimitry Andric           BuildMI(MBB, MBBI, dl, StoreInst)
10759dbab393SDimitry Andric             .addReg(FPReg)
10769dbab393SDimitry Andric             .addImm(FPOffset)
10779dbab393SDimitry Andric             .addReg(ScratchReg);
10789dbab393SDimitry Andric         if (FI->usesPICBase())
10799dbab393SDimitry Andric           BuildMI(MBB, MBBI, dl, StoreInst)
10809dbab393SDimitry Andric             .addReg(PPC::R30)
10819dbab393SDimitry Andric             .addImm(PBPOffset)
10829dbab393SDimitry Andric             .addReg(ScratchReg);
10839dbab393SDimitry Andric         if (HasBP) {
10849dbab393SDimitry Andric           BuildMI(MBB, MBBI, dl, StoreInst)
10859dbab393SDimitry Andric             .addReg(BPReg)
10869dbab393SDimitry Andric             .addImm(BPOffset)
10879dbab393SDimitry Andric             .addReg(ScratchReg);
10889dbab393SDimitry Andric           BuildMI(MBB, MBBI, dl, OrInst, BPReg)
10899dbab393SDimitry Andric             .addReg(ScratchReg, RegState::Kill)
10909dbab393SDimitry Andric             .addReg(ScratchReg);
10919dbab393SDimitry Andric         }
109226aa2dc5SDimitry Andric       }
10939dbab393SDimitry Andric     } else {
10949dbab393SDimitry Andric       // The frame size is a known 16-bit constant (fitting in the immediate
10959dbab393SDimitry Andric       // field of STWU). To be here we have to be compiling for PPC32.
10969dbab393SDimitry Andric       // Since the SPReg has been decreased by FrameSize, add it back to each
10979dbab393SDimitry Andric       // offset.
10989dbab393SDimitry Andric       if (HasFP)
10999dbab393SDimitry Andric         BuildMI(MBB, MBBI, dl, StoreInst)
11009dbab393SDimitry Andric           .addReg(FPReg)
11019dbab393SDimitry Andric           .addImm(FrameSize + FPOffset)
11029dbab393SDimitry Andric           .addReg(SPReg);
11039dbab393SDimitry Andric       if (FI->usesPICBase())
11049dbab393SDimitry Andric         BuildMI(MBB, MBBI, dl, StoreInst)
11059dbab393SDimitry Andric           .addReg(PPC::R30)
11069dbab393SDimitry Andric           .addImm(FrameSize + PBPOffset)
11079dbab393SDimitry Andric           .addReg(SPReg);
11089dbab393SDimitry Andric       if (HasBP) {
11099dbab393SDimitry Andric         BuildMI(MBB, MBBI, dl, StoreInst)
11109dbab393SDimitry Andric           .addReg(BPReg)
11119dbab393SDimitry Andric           .addImm(FrameSize + BPOffset)
11129dbab393SDimitry Andric           .addReg(SPReg);
11139dbab393SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), BPReg)
11149dbab393SDimitry Andric           .addReg(SPReg)
11159dbab393SDimitry Andric           .addImm(FrameSize);
11169dbab393SDimitry Andric       }
11179dbab393SDimitry Andric     }
11182754fe60SDimitry Andric   }
11192754fe60SDimitry Andric 
11209cac79b3SDimitry Andric   // Add Call Frame Information for the instructions we generated above.
11219cac79b3SDimitry Andric   if (needsCFI) {
11229cac79b3SDimitry Andric     unsigned CFIIndex;
11239cac79b3SDimitry Andric 
11249cac79b3SDimitry Andric     if (HasBP) {
11259cac79b3SDimitry Andric       // Define CFA in terms of BP. Do this in preference to using FP/SP,
11269cac79b3SDimitry Andric       // because if the stack needed aligning then CFA won't be at a fixed
11279cac79b3SDimitry Andric       // offset from FP/SP.
11289cac79b3SDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
1129d88c1a5aSDimitry Andric       CFIIndex = MF.addFrameInst(
11309cac79b3SDimitry Andric           MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
11319cac79b3SDimitry Andric     } else {
11329cac79b3SDimitry Andric       // Adjust the definition of CFA to account for the change in SP.
1133f785676fSDimitry Andric       assert(NegFrameSize);
1134d88c1a5aSDimitry Andric       CFIIndex = MF.addFrameInst(
113591bc56edSDimitry Andric           MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
11369cac79b3SDimitry Andric     }
113791bc56edSDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
113891bc56edSDimitry Andric         .addCFIIndex(CFIIndex);
11392754fe60SDimitry Andric 
11402754fe60SDimitry Andric     if (HasFP) {
11419cac79b3SDimitry Andric       // Describe where FP was saved, at a fixed offset from CFA.
1142f785676fSDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
1143d88c1a5aSDimitry Andric       CFIIndex = MF.addFrameInst(
114491bc56edSDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
114591bc56edSDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
114691bc56edSDimitry Andric           .addCFIIndex(CFIIndex);
1147f785676fSDimitry Andric     }
1148f785676fSDimitry Andric 
114939d628a0SDimitry Andric     if (FI->usesPICBase()) {
115039d628a0SDimitry Andric       // Describe where FP was saved, at a fixed offset from CFA.
115139d628a0SDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true);
1152d88c1a5aSDimitry Andric       CFIIndex = MF.addFrameInst(
115339d628a0SDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset));
115439d628a0SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
115539d628a0SDimitry Andric           .addCFIIndex(CFIIndex);
115639d628a0SDimitry Andric     }
115739d628a0SDimitry Andric 
1158f785676fSDimitry Andric     if (HasBP) {
11599cac79b3SDimitry Andric       // Describe where BP was saved, at a fixed offset from CFA.
1160f785676fSDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
1161d88c1a5aSDimitry Andric       CFIIndex = MF.addFrameInst(
116291bc56edSDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
116391bc56edSDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
116491bc56edSDimitry Andric           .addCFIIndex(CFIIndex);
11652754fe60SDimitry Andric     }
11662754fe60SDimitry Andric 
11672754fe60SDimitry Andric     if (MustSaveLR) {
11689cac79b3SDimitry Andric       // Describe where LR was saved, at a fixed offset from CFA.
1169f785676fSDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
1170d88c1a5aSDimitry Andric       CFIIndex = MF.addFrameInst(
117191bc56edSDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
117291bc56edSDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
117391bc56edSDimitry Andric           .addCFIIndex(CFIIndex);
11742754fe60SDimitry Andric     }
11752754fe60SDimitry Andric   }
11762754fe60SDimitry Andric 
11772754fe60SDimitry Andric   // If there is a frame pointer, copy R1 into R31
11782754fe60SDimitry Andric   if (HasFP) {
1179f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1180f785676fSDimitry Andric       .addReg(SPReg)
1181f785676fSDimitry Andric       .addReg(SPReg);
11822754fe60SDimitry Andric 
11839cac79b3SDimitry Andric     if (!HasBP && needsCFI) {
11849cac79b3SDimitry Andric       // Change the definition of CFA from SP+offset to FP+offset, because SP
11859cac79b3SDimitry Andric       // will change at every alloca.
1186f785676fSDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
1187d88c1a5aSDimitry Andric       unsigned CFIIndex = MF.addFrameInst(
118891bc56edSDimitry Andric           MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
118991bc56edSDimitry Andric 
119091bc56edSDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
119191bc56edSDimitry Andric           .addCFIIndex(CFIIndex);
11922754fe60SDimitry Andric     }
11932754fe60SDimitry Andric   }
11942754fe60SDimitry Andric 
11959cac79b3SDimitry Andric   if (needsCFI) {
11969cac79b3SDimitry Andric     // Describe where callee saved registers were saved, at fixed offsets from
11979cac79b3SDimitry Andric     // CFA.
1198d88c1a5aSDimitry Andric     const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
11992754fe60SDimitry Andric     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
12002754fe60SDimitry Andric       unsigned Reg = CSI[I].getReg();
12012754fe60SDimitry Andric       if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
1202bd5abe19SDimitry Andric 
1203bd5abe19SDimitry Andric       // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
1204bd5abe19SDimitry Andric       // subregisters of CR2. We just need to emit a move of CR2.
12057ae0e2c9SDimitry Andric       if (PPC::CRBITRCRegClass.contains(Reg))
1206bd5abe19SDimitry Andric         continue;
1207bd5abe19SDimitry Andric 
12083861d79fSDimitry Andric       // For SVR4, don't emit a move for the CR spill slot if we haven't
12093861d79fSDimitry Andric       // spilled CRs.
1210f785676fSDimitry Andric       if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
121109a17a1eSDimitry Andric           && !MustSaveCR)
12123861d79fSDimitry Andric         continue;
12133861d79fSDimitry Andric 
12143861d79fSDimitry Andric       // For 64-bit SVR4 when we have spilled CRs, the spill location
12153861d79fSDimitry Andric       // is SP+8, not a frame-relative slot.
1216f785676fSDimitry Andric       if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
121791bc56edSDimitry Andric         // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
121891bc56edSDimitry Andric         // the whole CR word.  In the ELFv2 ABI, every CR that was
121991bc56edSDimitry Andric         // actually saved gets its own CFI record.
122091bc56edSDimitry Andric         unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
1221d88c1a5aSDimitry Andric         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
122291bc56edSDimitry Andric             nullptr, MRI->getDwarfRegNum(CRReg, true), 8));
122391bc56edSDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
122491bc56edSDimitry Andric             .addCFIIndex(CFIIndex);
12253861d79fSDimitry Andric         continue;
12263861d79fSDimitry Andric       }
12273861d79fSDimitry Andric 
1228*b5893f02SDimitry Andric       if (CSI[I].isSpilledToReg()) {
1229*b5893f02SDimitry Andric         unsigned SpilledReg = CSI[I].getDstReg();
1230*b5893f02SDimitry Andric         unsigned CFIRegister = MF.addFrameInst(MCCFIInstruction::createRegister(
1231*b5893f02SDimitry Andric             nullptr, MRI->getDwarfRegNum(Reg, true),
1232*b5893f02SDimitry Andric             MRI->getDwarfRegNum(SpilledReg, true)));
1233*b5893f02SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1234*b5893f02SDimitry Andric           .addCFIIndex(CFIRegister);
1235*b5893f02SDimitry Andric       } else {
1236d88c1a5aSDimitry Andric         int Offset = MFI.getObjectOffset(CSI[I].getFrameIdx());
1237d88c1a5aSDimitry Andric         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
123891bc56edSDimitry Andric             nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
123991bc56edSDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
124091bc56edSDimitry Andric             .addCFIIndex(CFIIndex);
12412754fe60SDimitry Andric       }
12422754fe60SDimitry Andric     }
12432754fe60SDimitry Andric   }
1244*b5893f02SDimitry Andric }
12452754fe60SDimitry Andric 
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const12462754fe60SDimitry Andric void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
12472754fe60SDimitry Andric                                     MachineBasicBlock &MBB) const {
12487d523365SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
12497d523365SDimitry Andric   DebugLoc dl;
12507d523365SDimitry Andric 
12517d523365SDimitry Andric   if (MBBI != MBB.end())
12527d523365SDimitry Andric     dl = MBBI->getDebugLoc();
12537d523365SDimitry Andric 
12547a7e6055SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
12557a7e6055SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
12562754fe60SDimitry Andric 
1257f785676fSDimitry Andric   // Get alignment info so we know how to restore the SP.
1258d88c1a5aSDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
12592754fe60SDimitry Andric 
12602754fe60SDimitry Andric   // Get the number of bytes allocated from the FrameInfo.
1261d88c1a5aSDimitry Andric   int FrameSize = MFI.getStackSize();
12622754fe60SDimitry Andric 
12632754fe60SDimitry Andric   // Get processor type.
12642754fe60SDimitry Andric   bool isPPC64 = Subtarget.isPPC64();
1265f785676fSDimitry Andric   // Get the ABI.
1266f785676fSDimitry Andric   bool isSVR4ABI = Subtarget.isSVR4ABI();
1267f785676fSDimitry Andric 
12682754fe60SDimitry Andric   // Check if the link register (LR) has been saved.
12692754fe60SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
12702754fe60SDimitry Andric   bool MustSaveLR = FI->mustSaveLR();
1271f785676fSDimitry Andric   const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
127209a17a1eSDimitry Andric   bool MustSaveCR = !MustSaveCRs.empty();
1273f785676fSDimitry Andric   // Do we have a frame pointer and/or base pointer for this function?
12742754fe60SDimitry Andric   bool HasFP = hasFP(MF);
1275f785676fSDimitry Andric   bool HasBP = RegInfo->hasBasePointer(MF);
127626aa2dc5SDimitry Andric   bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
1277f785676fSDimitry Andric 
1278f785676fSDimitry Andric   unsigned SPReg      = isPPC64 ? PPC::X1  : PPC::R1;
127926e25074SRoman Divacky   unsigned BPReg      = RegInfo->getBaseRegister(MF);
1280f785676fSDimitry Andric   unsigned FPReg      = isPPC64 ? PPC::X31 : PPC::R31;
12817d523365SDimitry Andric   unsigned ScratchReg = 0;
1282f785676fSDimitry Andric   unsigned TempReg     = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
1283f785676fSDimitry Andric   const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
1284f785676fSDimitry Andric                                                  : PPC::MTLR );
1285f785676fSDimitry Andric   const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
1286f785676fSDimitry Andric                                                  : PPC::LWZ );
1287f785676fSDimitry Andric   const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
1288f785676fSDimitry Andric                                                            : PPC::LIS );
128926aa2dc5SDimitry Andric   const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
129026aa2dc5SDimitry Andric                                               : PPC::OR );
1291f785676fSDimitry Andric   const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
1292f785676fSDimitry Andric                                                   : PPC::ORI );
1293f785676fSDimitry Andric   const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
1294f785676fSDimitry Andric                                                    : PPC::ADDI );
1295f785676fSDimitry Andric   const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
1296f785676fSDimitry Andric                                                 : PPC::ADD4 );
12972754fe60SDimitry Andric 
1298ff0cc061SDimitry Andric   int LROffset = getReturnSaveOffset();
12992754fe60SDimitry Andric 
13002754fe60SDimitry Andric   int FPOffset = 0;
13017d523365SDimitry Andric 
1302d88c1a5aSDimitry Andric   // Using the same bool variable as below to suppress compiler warnings.
130309a17a1eSDimitry Andric   bool SingleScratchReg = findScratchRegister(&MBB, true, false, &ScratchReg,
130409a17a1eSDimitry Andric                                               &TempReg);
130509a17a1eSDimitry Andric   assert(SingleScratchReg &&
130609a17a1eSDimitry Andric          "Could not find an available scratch register");
130709a17a1eSDimitry Andric 
130809a17a1eSDimitry Andric   SingleScratchReg = ScratchReg == TempReg;
13097d523365SDimitry Andric 
13102754fe60SDimitry Andric   if (HasFP) {
1311f785676fSDimitry Andric     if (isSVR4ABI) {
13122754fe60SDimitry Andric       int FPIndex = FI->getFramePointerSaveIndex();
13132754fe60SDimitry Andric       assert(FPIndex && "No Frame Pointer Save Slot!");
1314d88c1a5aSDimitry Andric       FPOffset = MFI.getObjectOffset(FPIndex);
13152754fe60SDimitry Andric     } else {
1316ff0cc061SDimitry Andric       FPOffset = getFramePointerSaveOffset();
13172754fe60SDimitry Andric     }
13182754fe60SDimitry Andric   }
13192754fe60SDimitry Andric 
1320f785676fSDimitry Andric   int BPOffset = 0;
1321f785676fSDimitry Andric   if (HasBP) {
1322f785676fSDimitry Andric     if (isSVR4ABI) {
1323f785676fSDimitry Andric       int BPIndex = FI->getBasePointerSaveIndex();
1324f785676fSDimitry Andric       assert(BPIndex && "No Base Pointer Save Slot!");
1325d88c1a5aSDimitry Andric       BPOffset = MFI.getObjectOffset(BPIndex);
1326f785676fSDimitry Andric     } else {
1327ff0cc061SDimitry Andric       BPOffset = getBasePointerSaveOffset();
1328f785676fSDimitry Andric     }
1329f785676fSDimitry Andric   }
1330f785676fSDimitry Andric 
133139d628a0SDimitry Andric   int PBPOffset = 0;
133239d628a0SDimitry Andric   if (FI->usesPICBase()) {
133339d628a0SDimitry Andric     int PBPIndex = FI->getPICBasePointerSaveIndex();
133439d628a0SDimitry Andric     assert(PBPIndex && "No PIC Base Pointer Save Slot!");
1335d88c1a5aSDimitry Andric     PBPOffset = MFI.getObjectOffset(PBPIndex);
133639d628a0SDimitry Andric   }
133739d628a0SDimitry Andric 
13387d523365SDimitry Andric   bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn());
13397d523365SDimitry Andric 
13407d523365SDimitry Andric   if (IsReturnBlock) {
13417d523365SDimitry Andric     unsigned RetOpcode = MBBI->getOpcode();
13422754fe60SDimitry Andric     bool UsesTCRet =  RetOpcode == PPC::TCRETURNri ||
13432754fe60SDimitry Andric                       RetOpcode == PPC::TCRETURNdi ||
13442754fe60SDimitry Andric                       RetOpcode == PPC::TCRETURNai ||
13452754fe60SDimitry Andric                       RetOpcode == PPC::TCRETURNri8 ||
13462754fe60SDimitry Andric                       RetOpcode == PPC::TCRETURNdi8 ||
13472754fe60SDimitry Andric                       RetOpcode == PPC::TCRETURNai8;
13482754fe60SDimitry Andric 
13492754fe60SDimitry Andric     if (UsesTCRet) {
13502754fe60SDimitry Andric       int MaxTCRetDelta = FI->getTailCallSPDelta();
13512754fe60SDimitry Andric       MachineOperand &StackAdjust = MBBI->getOperand(1);
13522754fe60SDimitry Andric       assert(StackAdjust.isImm() && "Expecting immediate value.");
13532754fe60SDimitry Andric       // Adjust stack pointer.
13542754fe60SDimitry Andric       int StackAdj = StackAdjust.getImm();
13552754fe60SDimitry Andric       int Delta = StackAdj - MaxTCRetDelta;
13562754fe60SDimitry Andric       assert((Delta >= 0) && "Delta must be positive");
13572754fe60SDimitry Andric       if (MaxTCRetDelta>0)
13582754fe60SDimitry Andric         FrameSize += (StackAdj +Delta);
13592754fe60SDimitry Andric       else
13602754fe60SDimitry Andric         FrameSize += StackAdj;
13612754fe60SDimitry Andric     }
13627d523365SDimitry Andric   }
13632754fe60SDimitry Andric 
1364f785676fSDimitry Andric   // Frames of 32KB & larger require special handling because they cannot be
1365f785676fSDimitry Andric   // indexed into with a simple LD/LWZ immediate offset operand.
1366f785676fSDimitry Andric   bool isLargeFrame = !isInt<16>(FrameSize);
1367f785676fSDimitry Andric 
136826aa2dc5SDimitry Andric   // On targets without red zone, the SP needs to be restored last, so that
136926aa2dc5SDimitry Andric   // all live contents of the stack frame are upwards of the SP. This means
137026aa2dc5SDimitry Andric   // that we cannot restore SP just now, since there may be more registers
137126aa2dc5SDimitry Andric   // to restore from the stack frame (e.g. R31). If the frame size is not
137226aa2dc5SDimitry Andric   // a simple immediate value, we will need a spare register to hold the
137326aa2dc5SDimitry Andric   // restored SP. If the frame size is known and small, we can simply adjust
137426aa2dc5SDimitry Andric   // the offsets of the registers to be restored, and still use SP to restore
137526aa2dc5SDimitry Andric   // them. In such case, the final update of SP will be to add the frame
137626aa2dc5SDimitry Andric   // size to it.
137726aa2dc5SDimitry Andric   // To simplify the code, set RBReg to the base register used to restore
137826aa2dc5SDimitry Andric   // values from the stack, and set SPAdd to the value that needs to be added
137926aa2dc5SDimitry Andric   // to the SP at the end. The default values are as if red zone was present.
138026aa2dc5SDimitry Andric   unsigned RBReg = SPReg;
138126aa2dc5SDimitry Andric   unsigned SPAdd = 0;
138226aa2dc5SDimitry Andric 
13832754fe60SDimitry Andric   if (FrameSize) {
138426aa2dc5SDimitry Andric     // In the prologue, the loaded (or persistent) stack pointer value is
138526aa2dc5SDimitry Andric     // offset by the STDU/STDUX/STWU/STWUX instruction. For targets with red
138626aa2dc5SDimitry Andric     // zone add this offset back now.
1387f785676fSDimitry Andric 
13882754fe60SDimitry Andric     // If this function contained a fastcc call and GuaranteedTailCallOpt is
13892754fe60SDimitry Andric     // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
13902754fe60SDimitry Andric     // call which invalidates the stack pointer value in SP(0). So we use the
13912754fe60SDimitry Andric     // value of R31 in this case.
1392f785676fSDimitry Andric     if (FI->hasFastCall()) {
1393f785676fSDimitry Andric       assert(HasFP && "Expecting a valid frame pointer.");
139426aa2dc5SDimitry Andric       if (!HasRedZone)
139526aa2dc5SDimitry Andric         RBReg = FPReg;
1396f785676fSDimitry Andric       if (!isLargeFrame) {
139726aa2dc5SDimitry Andric         BuildMI(MBB, MBBI, dl, AddImmInst, RBReg)
1398f785676fSDimitry Andric           .addReg(FPReg).addImm(FrameSize);
1399f785676fSDimitry Andric       } else {
1400f785676fSDimitry Andric         BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
14012754fe60SDimitry Andric           .addImm(FrameSize >> 16);
1402f785676fSDimitry Andric         BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1403f785676fSDimitry Andric           .addReg(ScratchReg, RegState::Kill)
14042754fe60SDimitry Andric           .addImm(FrameSize & 0xFFFF);
1405f785676fSDimitry Andric         BuildMI(MBB, MBBI, dl, AddInst)
140626aa2dc5SDimitry Andric           .addReg(RBReg)
1407f785676fSDimitry Andric           .addReg(FPReg)
1408f785676fSDimitry Andric           .addReg(ScratchReg);
1409f785676fSDimitry Andric       }
1410d88c1a5aSDimitry Andric     } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) {
141126aa2dc5SDimitry Andric       if (HasRedZone) {
1412f785676fSDimitry Andric         BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1413f785676fSDimitry Andric           .addReg(SPReg)
1414f785676fSDimitry Andric           .addImm(FrameSize);
14152754fe60SDimitry Andric       } else {
141626aa2dc5SDimitry Andric         // Make sure that adding FrameSize will not overflow the max offset
141726aa2dc5SDimitry Andric         // size.
141826aa2dc5SDimitry Andric         assert(FPOffset <= 0 && BPOffset <= 0 && PBPOffset <= 0 &&
141926aa2dc5SDimitry Andric                "Local offsets should be negative");
142026aa2dc5SDimitry Andric         SPAdd = FrameSize;
142126aa2dc5SDimitry Andric         FPOffset += FrameSize;
142226aa2dc5SDimitry Andric         BPOffset += FrameSize;
142326aa2dc5SDimitry Andric         PBPOffset += FrameSize;
142426aa2dc5SDimitry Andric       }
142526aa2dc5SDimitry Andric     } else {
142626aa2dc5SDimitry Andric       // We don't want to use ScratchReg as a base register, because it
142726aa2dc5SDimitry Andric       // could happen to be R0. Use FP instead, but make sure to preserve it.
142826aa2dc5SDimitry Andric       if (!HasRedZone) {
142926aa2dc5SDimitry Andric         // If FP is not saved, copy it to ScratchReg.
143026aa2dc5SDimitry Andric         if (!HasFP)
143126aa2dc5SDimitry Andric           BuildMI(MBB, MBBI, dl, OrInst, ScratchReg)
143226aa2dc5SDimitry Andric             .addReg(FPReg)
143326aa2dc5SDimitry Andric             .addReg(FPReg);
143426aa2dc5SDimitry Andric         RBReg = FPReg;
143526aa2dc5SDimitry Andric       }
143626aa2dc5SDimitry Andric       BuildMI(MBB, MBBI, dl, LoadInst, RBReg)
1437f785676fSDimitry Andric         .addImm(0)
1438f785676fSDimitry Andric         .addReg(SPReg);
14392754fe60SDimitry Andric     }
1440f785676fSDimitry Andric   }
144126aa2dc5SDimitry Andric   assert(RBReg != ScratchReg && "Should have avoided ScratchReg");
144226aa2dc5SDimitry Andric   // If there is no red zone, ScratchReg may be needed for holding a useful
144326aa2dc5SDimitry Andric   // value (although not the base register). Make sure it is not overwritten
144426aa2dc5SDimitry Andric   // too early.
1445284c1978SDimitry Andric 
144609a17a1eSDimitry Andric   assert((isPPC64 || !MustSaveCR) &&
144709a17a1eSDimitry Andric          "Epilogue CR restoring supported only in 64-bit mode");
144809a17a1eSDimitry Andric 
144926aa2dc5SDimitry Andric   // If we need to restore both the LR and the CR and we only have one
145026aa2dc5SDimitry Andric   // available scratch register, we must do them one at a time.
145109a17a1eSDimitry Andric   if (MustSaveCR && SingleScratchReg && MustSaveLR) {
145226aa2dc5SDimitry Andric     // Here TempReg == ScratchReg, and in the absence of red zone ScratchReg
145326aa2dc5SDimitry Andric     // is live here.
145426aa2dc5SDimitry Andric     assert(HasRedZone && "Expecting red zone");
145509a17a1eSDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
145609a17a1eSDimitry Andric       .addImm(8)
145709a17a1eSDimitry Andric       .addReg(SPReg);
145809a17a1eSDimitry Andric     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
145909a17a1eSDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
146009a17a1eSDimitry Andric         .addReg(TempReg, getKillRegState(i == e-1));
146109a17a1eSDimitry Andric   }
146209a17a1eSDimitry Andric 
146326aa2dc5SDimitry Andric   // Delay restoring of the LR if ScratchReg is needed. This is ok, since
146426aa2dc5SDimitry Andric   // LR is stored in the caller's stack frame. ScratchReg will be needed
146526aa2dc5SDimitry Andric   // if RBReg is anything other than SP. We shouldn't use ScratchReg as
146626aa2dc5SDimitry Andric   // a base register anyway, because it may happen to be R0.
146726aa2dc5SDimitry Andric   bool LoadedLR = false;
146826aa2dc5SDimitry Andric   if (MustSaveLR && RBReg == SPReg && isInt<16>(LROffset+SPAdd)) {
1469f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
147026aa2dc5SDimitry Andric       .addImm(LROffset+SPAdd)
147126aa2dc5SDimitry Andric       .addReg(RBReg);
147226aa2dc5SDimitry Andric     LoadedLR = true;
147326aa2dc5SDimitry Andric   }
14742754fe60SDimitry Andric 
147526aa2dc5SDimitry Andric   if (MustSaveCR && !(SingleScratchReg && MustSaveLR)) {
147626aa2dc5SDimitry Andric     // This will only occur for PPC64.
147726aa2dc5SDimitry Andric     assert(isPPC64 && "Expecting 64-bit mode");
147826aa2dc5SDimitry Andric     assert(RBReg == SPReg && "Should be using SP as a base register");
1479f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1480f785676fSDimitry Andric       .addImm(8)
148126aa2dc5SDimitry Andric       .addReg(RBReg);
148226aa2dc5SDimitry Andric   }
1483f785676fSDimitry Andric 
148426aa2dc5SDimitry Andric   if (HasFP) {
148526aa2dc5SDimitry Andric     // If there is red zone, restore FP directly, since SP has already been
148626aa2dc5SDimitry Andric     // restored. Otherwise, restore the value of FP into ScratchReg.
148726aa2dc5SDimitry Andric     if (HasRedZone || RBReg == SPReg)
1488f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
1489f785676fSDimitry Andric         .addImm(FPOffset)
1490f785676fSDimitry Andric         .addReg(SPReg);
149126aa2dc5SDimitry Andric     else
149226aa2dc5SDimitry Andric       BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
149326aa2dc5SDimitry Andric         .addImm(FPOffset)
149426aa2dc5SDimitry Andric         .addReg(RBReg);
149526aa2dc5SDimitry Andric   }
1496f785676fSDimitry Andric 
149739d628a0SDimitry Andric   if (FI->usesPICBase())
14980f5676f4SDimitry Andric     BuildMI(MBB, MBBI, dl, LoadInst, PPC::R30)
149939d628a0SDimitry Andric       .addImm(PBPOffset)
150026aa2dc5SDimitry Andric       .addReg(RBReg);
150139d628a0SDimitry Andric 
1502f785676fSDimitry Andric   if (HasBP)
1503f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1504f785676fSDimitry Andric       .addImm(BPOffset)
150526aa2dc5SDimitry Andric       .addReg(RBReg);
150626aa2dc5SDimitry Andric 
150726aa2dc5SDimitry Andric   // There is nothing more to be loaded from the stack, so now we can
150826aa2dc5SDimitry Andric   // restore SP: SP = RBReg + SPAdd.
150926aa2dc5SDimitry Andric   if (RBReg != SPReg || SPAdd != 0) {
151026aa2dc5SDimitry Andric     assert(!HasRedZone && "This should not happen with red zone");
151126aa2dc5SDimitry Andric     // If SPAdd is 0, generate a copy.
151226aa2dc5SDimitry Andric     if (SPAdd == 0)
151326aa2dc5SDimitry Andric       BuildMI(MBB, MBBI, dl, OrInst, SPReg)
151426aa2dc5SDimitry Andric         .addReg(RBReg)
151526aa2dc5SDimitry Andric         .addReg(RBReg);
151626aa2dc5SDimitry Andric     else
151726aa2dc5SDimitry Andric       BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
151826aa2dc5SDimitry Andric         .addReg(RBReg)
151926aa2dc5SDimitry Andric         .addImm(SPAdd);
152026aa2dc5SDimitry Andric 
152126aa2dc5SDimitry Andric     assert(RBReg != ScratchReg && "Should be using FP or SP as base register");
152226aa2dc5SDimitry Andric     if (RBReg == FPReg)
152326aa2dc5SDimitry Andric       BuildMI(MBB, MBBI, dl, OrInst, FPReg)
152426aa2dc5SDimitry Andric         .addReg(ScratchReg)
152526aa2dc5SDimitry Andric         .addReg(ScratchReg);
152626aa2dc5SDimitry Andric 
152726aa2dc5SDimitry Andric     // Now load the LR from the caller's stack frame.
152826aa2dc5SDimitry Andric     if (MustSaveLR && !LoadedLR)
152926aa2dc5SDimitry Andric       BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
153026aa2dc5SDimitry Andric         .addImm(LROffset)
1531f785676fSDimitry Andric         .addReg(SPReg);
153226aa2dc5SDimitry Andric   }
1533f785676fSDimitry Andric 
153409a17a1eSDimitry Andric   if (MustSaveCR &&
153509a17a1eSDimitry Andric       !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
1536f785676fSDimitry Andric     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1537f785676fSDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1538f785676fSDimitry Andric         .addReg(TempReg, getKillRegState(i == e-1));
15392754fe60SDimitry Andric 
15402754fe60SDimitry Andric   if (MustSaveLR)
1541f785676fSDimitry Andric     BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
15422754fe60SDimitry Andric 
15432754fe60SDimitry Andric   // Callee pop calling convention. Pop parameter/linkage area. Used for tail
15442754fe60SDimitry Andric   // call optimization
15457d523365SDimitry Andric   if (IsReturnBlock) {
15467d523365SDimitry Andric     unsigned RetOpcode = MBBI->getOpcode();
154739d628a0SDimitry Andric     if (MF.getTarget().Options.GuaranteedTailCallOpt &&
154839d628a0SDimitry Andric         (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
15492cab237bSDimitry Andric         MF.getFunction().getCallingConv() == CallingConv::Fast) {
15502754fe60SDimitry Andric       PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
15512754fe60SDimitry Andric       unsigned CallerAllocatedAmt = FI->getMinReservedArea();
15522754fe60SDimitry Andric 
15532754fe60SDimitry Andric       if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
1554f785676fSDimitry Andric         BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1555f785676fSDimitry Andric           .addReg(SPReg).addImm(CallerAllocatedAmt);
15562754fe60SDimitry Andric       } else {
1557f785676fSDimitry Andric         BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
15582754fe60SDimitry Andric           .addImm(CallerAllocatedAmt >> 16);
1559f785676fSDimitry Andric         BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1560f785676fSDimitry Andric           .addReg(ScratchReg, RegState::Kill)
15612754fe60SDimitry Andric           .addImm(CallerAllocatedAmt & 0xFFFF);
1562f785676fSDimitry Andric         BuildMI(MBB, MBBI, dl, AddInst)
1563f785676fSDimitry Andric           .addReg(SPReg)
15642754fe60SDimitry Andric           .addReg(FPReg)
1565f785676fSDimitry Andric           .addReg(ScratchReg);
15662754fe60SDimitry Andric       }
15673ca95b02SDimitry Andric     } else {
15683ca95b02SDimitry Andric       createTailCallBranchInstr(MBB);
15693ca95b02SDimitry Andric     }
15703ca95b02SDimitry Andric   }
15713ca95b02SDimitry Andric }
15723ca95b02SDimitry Andric 
createTailCallBranchInstr(MachineBasicBlock & MBB) const15733ca95b02SDimitry Andric void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const {
15743ca95b02SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
15753ca95b02SDimitry Andric 
1576da09e106SDimitry Andric   // If we got this far a first terminator should exist.
1577da09e106SDimitry Andric   assert(MBBI != MBB.end() && "Failed to find the first terminator.");
15783ca95b02SDimitry Andric 
1579da09e106SDimitry Andric   DebugLoc dl = MBBI->getDebugLoc();
15807a7e6055SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
15813ca95b02SDimitry Andric 
15823ca95b02SDimitry Andric   // Create branch instruction for pseudo tail call return instruction
15833ca95b02SDimitry Andric   unsigned RetOpcode = MBBI->getOpcode();
15843ca95b02SDimitry Andric   if (RetOpcode == PPC::TCRETURNdi) {
15852754fe60SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
15862754fe60SDimitry Andric     MachineOperand &JumpTarget = MBBI->getOperand(0);
15872754fe60SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
15882754fe60SDimitry Andric       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
15892754fe60SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNri) {
15902754fe60SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
15912754fe60SDimitry Andric     assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
15922754fe60SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
15932754fe60SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNai) {
15942754fe60SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
15952754fe60SDimitry Andric     MachineOperand &JumpTarget = MBBI->getOperand(0);
15962754fe60SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
15972754fe60SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNdi8) {
15982754fe60SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
15992754fe60SDimitry Andric     MachineOperand &JumpTarget = MBBI->getOperand(0);
16002754fe60SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
16012754fe60SDimitry Andric       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
16022754fe60SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNri8) {
16032754fe60SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
16042754fe60SDimitry Andric     assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
16052754fe60SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
16062754fe60SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNai8) {
16072754fe60SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
16082754fe60SDimitry Andric     MachineOperand &JumpTarget = MBBI->getOperand(0);
16092754fe60SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
16102754fe60SDimitry Andric   }
16112754fe60SDimitry Andric }
16122754fe60SDimitry Andric 
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const1613875ed548SDimitry Andric void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF,
1614875ed548SDimitry Andric                                             BitVector &SavedRegs,
1615875ed548SDimitry Andric                                             RegScavenger *RS) const {
1616875ed548SDimitry Andric   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1617875ed548SDimitry Andric 
16187a7e6055SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
16192754fe60SDimitry Andric 
16202754fe60SDimitry Andric   //  Save and clear the LR state.
16212754fe60SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
16222754fe60SDimitry Andric   unsigned LR = RegInfo->getRARegister();
16232754fe60SDimitry Andric   FI->setMustSaveLR(MustSaveLR(MF, LR));
1624875ed548SDimitry Andric   SavedRegs.reset(LR);
16252754fe60SDimitry Andric 
16262754fe60SDimitry Andric   //  Save R31 if necessary
16272754fe60SDimitry Andric   int FPSI = FI->getFramePointerSaveIndex();
16282754fe60SDimitry Andric   bool isPPC64 = Subtarget.isPPC64();
16292754fe60SDimitry Andric   bool isDarwinABI  = Subtarget.isDarwinABI();
1630d88c1a5aSDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
16312754fe60SDimitry Andric 
16322754fe60SDimitry Andric   // If the frame pointer save index hasn't been defined yet.
16332754fe60SDimitry Andric   if (!FPSI && needsFP(MF)) {
16342754fe60SDimitry Andric     // Find out what the fix offset of the frame pointer save area.
1635ff0cc061SDimitry Andric     int FPOffset = getFramePointerSaveOffset();
16362754fe60SDimitry Andric     // Allocate the frame index for frame pointer save area.
1637d88c1a5aSDimitry Andric     FPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
16382754fe60SDimitry Andric     // Save the result.
16392754fe60SDimitry Andric     FI->setFramePointerSaveIndex(FPSI);
16402754fe60SDimitry Andric   }
16412754fe60SDimitry Andric 
1642f785676fSDimitry Andric   int BPSI = FI->getBasePointerSaveIndex();
1643f785676fSDimitry Andric   if (!BPSI && RegInfo->hasBasePointer(MF)) {
1644ff0cc061SDimitry Andric     int BPOffset = getBasePointerSaveOffset();
1645f785676fSDimitry Andric     // Allocate the frame index for the base pointer save area.
1646d88c1a5aSDimitry Andric     BPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
1647f785676fSDimitry Andric     // Save the result.
1648f785676fSDimitry Andric     FI->setBasePointerSaveIndex(BPSI);
1649f785676fSDimitry Andric   }
1650f785676fSDimitry Andric 
165139d628a0SDimitry Andric   // Reserve stack space for the PIC Base register (R30).
165239d628a0SDimitry Andric   // Only used in SVR4 32-bit.
165339d628a0SDimitry Andric   if (FI->usesPICBase()) {
1654d88c1a5aSDimitry Andric     int PBPSI = MFI.CreateFixedObject(4, -8, true);
165539d628a0SDimitry Andric     FI->setPICBasePointerSaveIndex(PBPSI);
165639d628a0SDimitry Andric   }
165739d628a0SDimitry Andric 
16581efa33efSDimitry Andric   // Make sure we don't explicitly spill r31, because, for example, we have
16594ba319b5SDimitry Andric   // some inline asm which explicitly clobbers it, when we otherwise have a
16601efa33efSDimitry Andric   // frame pointer and are using r31's spill slot for the prologue/epilogue
16611efa33efSDimitry Andric   // code. Same goes for the base pointer and the PIC base register.
16621efa33efSDimitry Andric   if (needsFP(MF))
16631efa33efSDimitry Andric     SavedRegs.reset(isPPC64 ? PPC::X31 : PPC::R31);
16641efa33efSDimitry Andric   if (RegInfo->hasBasePointer(MF))
16651efa33efSDimitry Andric     SavedRegs.reset(RegInfo->getBaseRegister(MF));
16661efa33efSDimitry Andric   if (FI->usesPICBase())
16671efa33efSDimitry Andric     SavedRegs.reset(PPC::R30);
16681efa33efSDimitry Andric 
16692754fe60SDimitry Andric   // Reserve stack space to move the linkage area to in case of a tail call.
16702754fe60SDimitry Andric   int TCSPDelta = 0;
1671dff0c46cSDimitry Andric   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1672dff0c46cSDimitry Andric       (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
1673d88c1a5aSDimitry Andric     MFI.CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
16742754fe60SDimitry Andric   }
16752754fe60SDimitry Andric 
1676139f7f9bSDimitry Andric   // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
1677139f7f9bSDimitry Andric   // function uses CR 2, 3, or 4.
1678139f7f9bSDimitry Andric   if (!isPPC64 && !isDarwinABI &&
1679875ed548SDimitry Andric       (SavedRegs.test(PPC::CR2) ||
1680875ed548SDimitry Andric        SavedRegs.test(PPC::CR3) ||
1681875ed548SDimitry Andric        SavedRegs.test(PPC::CR4))) {
1682d88c1a5aSDimitry Andric     int FrameIdx = MFI.CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1683139f7f9bSDimitry Andric     FI->setCRSpillFrameIndex(FrameIdx);
16842754fe60SDimitry Andric   }
16852754fe60SDimitry Andric }
16862754fe60SDimitry Andric 
processFunctionBeforeFrameFinalized(MachineFunction & MF,RegScavenger * RS) const1687139f7f9bSDimitry Andric void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
1688139f7f9bSDimitry Andric                                                        RegScavenger *RS) const {
16892754fe60SDimitry Andric   // Early exit if not using the SVR4 ABI.
1690139f7f9bSDimitry Andric   if (!Subtarget.isSVR4ABI()) {
1691139f7f9bSDimitry Andric     addScavengingSpillSlot(MF, RS);
16922754fe60SDimitry Andric     return;
1693139f7f9bSDimitry Andric   }
16942754fe60SDimitry Andric 
16952754fe60SDimitry Andric   // Get callee saved register information.
1696d88c1a5aSDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
1697d88c1a5aSDimitry Andric   const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
16982754fe60SDimitry Andric 
16993ca95b02SDimitry Andric   // If the function is shrink-wrapped, and if the function has a tail call, the
17003ca95b02SDimitry Andric   // tail call might not be in the new RestoreBlock, so real branch instruction
17013ca95b02SDimitry Andric   // won't be generated by emitEpilogue(), because shrink-wrap has chosen new
17023ca95b02SDimitry Andric   // RestoreBlock. So we handle this case here.
1703d88c1a5aSDimitry Andric   if (MFI.getSavePoint() && MFI.hasTailCall()) {
1704d88c1a5aSDimitry Andric     MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
17053ca95b02SDimitry Andric     for (MachineBasicBlock &MBB : MF) {
17063ca95b02SDimitry Andric       if (MBB.isReturnBlock() && (&MBB) != RestoreBlock)
17073ca95b02SDimitry Andric         createTailCallBranchInstr(MBB);
17083ca95b02SDimitry Andric     }
17093ca95b02SDimitry Andric   }
17103ca95b02SDimitry Andric 
17112754fe60SDimitry Andric   // Early exit if no callee saved registers are modified!
17122754fe60SDimitry Andric   if (CSI.empty() && !needsFP(MF)) {
1713139f7f9bSDimitry Andric     addScavengingSpillSlot(MF, RS);
17142754fe60SDimitry Andric     return;
17152754fe60SDimitry Andric   }
17162754fe60SDimitry Andric 
17172754fe60SDimitry Andric   unsigned MinGPR = PPC::R31;
17182754fe60SDimitry Andric   unsigned MinG8R = PPC::X31;
17192754fe60SDimitry Andric   unsigned MinFPR = PPC::F31;
17204ba319b5SDimitry Andric   unsigned MinVR = Subtarget.hasSPE() ? PPC::S31 : PPC::V31;
17212754fe60SDimitry Andric 
17222754fe60SDimitry Andric   bool HasGPSaveArea = false;
17232754fe60SDimitry Andric   bool HasG8SaveArea = false;
17242754fe60SDimitry Andric   bool HasFPSaveArea = false;
17252754fe60SDimitry Andric   bool HasVRSAVESaveArea = false;
17262754fe60SDimitry Andric   bool HasVRSaveArea = false;
17272754fe60SDimitry Andric 
17282754fe60SDimitry Andric   SmallVector<CalleeSavedInfo, 18> GPRegs;
17292754fe60SDimitry Andric   SmallVector<CalleeSavedInfo, 18> G8Regs;
17302754fe60SDimitry Andric   SmallVector<CalleeSavedInfo, 18> FPRegs;
17312754fe60SDimitry Andric   SmallVector<CalleeSavedInfo, 18> VRegs;
17322754fe60SDimitry Andric 
17332754fe60SDimitry Andric   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
17342754fe60SDimitry Andric     unsigned Reg = CSI[i].getReg();
17354ba319b5SDimitry Andric     if (PPC::GPRCRegClass.contains(Reg) ||
17364ba319b5SDimitry Andric         PPC::SPE4RCRegClass.contains(Reg)) {
17372754fe60SDimitry Andric       HasGPSaveArea = true;
17382754fe60SDimitry Andric 
17392754fe60SDimitry Andric       GPRegs.push_back(CSI[i]);
17402754fe60SDimitry Andric 
17412754fe60SDimitry Andric       if (Reg < MinGPR) {
17422754fe60SDimitry Andric         MinGPR = Reg;
17432754fe60SDimitry Andric       }
17447ae0e2c9SDimitry Andric     } else if (PPC::G8RCRegClass.contains(Reg)) {
17452754fe60SDimitry Andric       HasG8SaveArea = true;
17462754fe60SDimitry Andric 
17472754fe60SDimitry Andric       G8Regs.push_back(CSI[i]);
17482754fe60SDimitry Andric 
17492754fe60SDimitry Andric       if (Reg < MinG8R) {
17502754fe60SDimitry Andric         MinG8R = Reg;
17512754fe60SDimitry Andric       }
17527ae0e2c9SDimitry Andric     } else if (PPC::F8RCRegClass.contains(Reg)) {
17532754fe60SDimitry Andric       HasFPSaveArea = true;
17542754fe60SDimitry Andric 
17552754fe60SDimitry Andric       FPRegs.push_back(CSI[i]);
17562754fe60SDimitry Andric 
17572754fe60SDimitry Andric       if (Reg < MinFPR) {
17582754fe60SDimitry Andric         MinFPR = Reg;
17592754fe60SDimitry Andric       }
17607ae0e2c9SDimitry Andric     } else if (PPC::CRBITRCRegClass.contains(Reg) ||
17617ae0e2c9SDimitry Andric                PPC::CRRCRegClass.contains(Reg)) {
17623861d79fSDimitry Andric       ; // do nothing, as we already know whether CRs are spilled
17637ae0e2c9SDimitry Andric     } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
17642754fe60SDimitry Andric       HasVRSAVESaveArea = true;
17654ba319b5SDimitry Andric     } else if (PPC::VRRCRegClass.contains(Reg) ||
17664ba319b5SDimitry Andric                PPC::SPERCRegClass.contains(Reg)) {
17674ba319b5SDimitry Andric       // Altivec and SPE are mutually exclusive, but have the same stack
17684ba319b5SDimitry Andric       // alignment requirements, so overload the save area for both cases.
17692754fe60SDimitry Andric       HasVRSaveArea = true;
17702754fe60SDimitry Andric 
17712754fe60SDimitry Andric       VRegs.push_back(CSI[i]);
17722754fe60SDimitry Andric 
17732754fe60SDimitry Andric       if (Reg < MinVR) {
17742754fe60SDimitry Andric         MinVR = Reg;
17752754fe60SDimitry Andric       }
17762754fe60SDimitry Andric     } else {
17772754fe60SDimitry Andric       llvm_unreachable("Unknown RegisterClass!");
17782754fe60SDimitry Andric     }
17792754fe60SDimitry Andric   }
17802754fe60SDimitry Andric 
17812754fe60SDimitry Andric   PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
1782ff0cc061SDimitry Andric   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
17832754fe60SDimitry Andric 
17842754fe60SDimitry Andric   int64_t LowerBound = 0;
17852754fe60SDimitry Andric 
17862754fe60SDimitry Andric   // Take into account stack space reserved for tail calls.
17872754fe60SDimitry Andric   int TCSPDelta = 0;
1788dff0c46cSDimitry Andric   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1789dff0c46cSDimitry Andric       (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
17902754fe60SDimitry Andric     LowerBound = TCSPDelta;
17912754fe60SDimitry Andric   }
17922754fe60SDimitry Andric 
17932754fe60SDimitry Andric   // The Floating-point register save area is right below the back chain word
17942754fe60SDimitry Andric   // of the previous stack frame.
17952754fe60SDimitry Andric   if (HasFPSaveArea) {
17962754fe60SDimitry Andric     for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
17972754fe60SDimitry Andric       int FI = FPRegs[i].getFrameIdx();
17982754fe60SDimitry Andric 
1799d88c1a5aSDimitry Andric       MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
18002754fe60SDimitry Andric     }
18012754fe60SDimitry Andric 
1802139f7f9bSDimitry Andric     LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
18032754fe60SDimitry Andric   }
18042754fe60SDimitry Andric 
18052754fe60SDimitry Andric   // Check whether the frame pointer register is allocated. If so, make sure it
18062754fe60SDimitry Andric   // is spilled to the correct offset.
18072754fe60SDimitry Andric   if (needsFP(MF)) {
18082754fe60SDimitry Andric     int FI = PFI->getFramePointerSaveIndex();
18092754fe60SDimitry Andric     assert(FI && "No Frame Pointer Save Slot!");
1810d88c1a5aSDimitry Andric     MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
181160ff8e32SDimitry Andric     // FP is R31/X31, so no need to update MinGPR/MinG8R.
181260ff8e32SDimitry Andric     HasGPSaveArea = true;
18132754fe60SDimitry Andric   }
18142754fe60SDimitry Andric 
181539d628a0SDimitry Andric   if (PFI->usesPICBase()) {
181639d628a0SDimitry Andric     int FI = PFI->getPICBasePointerSaveIndex();
181739d628a0SDimitry Andric     assert(FI && "No PIC Base Pointer Save Slot!");
1818d88c1a5aSDimitry Andric     MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
181960ff8e32SDimitry Andric 
182060ff8e32SDimitry Andric     MinGPR = std::min<unsigned>(MinGPR, PPC::R30);
182160ff8e32SDimitry Andric     HasGPSaveArea = true;
182239d628a0SDimitry Andric   }
182339d628a0SDimitry Andric 
18247a7e6055SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1825f785676fSDimitry Andric   if (RegInfo->hasBasePointer(MF)) {
1826f785676fSDimitry Andric     int FI = PFI->getBasePointerSaveIndex();
1827f785676fSDimitry Andric     assert(FI && "No Base Pointer Save Slot!");
1828d88c1a5aSDimitry Andric     MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
182960ff8e32SDimitry Andric 
183060ff8e32SDimitry Andric     unsigned BP = RegInfo->getBaseRegister(MF);
183160ff8e32SDimitry Andric     if (PPC::G8RCRegClass.contains(BP)) {
183260ff8e32SDimitry Andric       MinG8R = std::min<unsigned>(MinG8R, BP);
183360ff8e32SDimitry Andric       HasG8SaveArea = true;
183460ff8e32SDimitry Andric     } else if (PPC::GPRCRegClass.contains(BP)) {
183560ff8e32SDimitry Andric       MinGPR = std::min<unsigned>(MinGPR, BP);
183660ff8e32SDimitry Andric       HasGPSaveArea = true;
183760ff8e32SDimitry Andric     }
1838f785676fSDimitry Andric   }
1839f785676fSDimitry Andric 
18402754fe60SDimitry Andric   // General register save area starts right below the Floating-point
18412754fe60SDimitry Andric   // register save area.
18422754fe60SDimitry Andric   if (HasGPSaveArea || HasG8SaveArea) {
18432754fe60SDimitry Andric     // Move general register save area spill slots down, taking into account
18442754fe60SDimitry Andric     // the size of the Floating-point register save area.
18452754fe60SDimitry Andric     for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1846*b5893f02SDimitry Andric       if (!GPRegs[i].isSpilledToReg()) {
18472754fe60SDimitry Andric         int FI = GPRegs[i].getFrameIdx();
1848d88c1a5aSDimitry Andric         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
18492754fe60SDimitry Andric       }
1850*b5893f02SDimitry Andric     }
18512754fe60SDimitry Andric 
18522754fe60SDimitry Andric     // Move general register save area spill slots down, taking into account
18532754fe60SDimitry Andric     // the size of the Floating-point register save area.
18542754fe60SDimitry Andric     for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1855*b5893f02SDimitry Andric       if (!G8Regs[i].isSpilledToReg()) {
18562754fe60SDimitry Andric         int FI = G8Regs[i].getFrameIdx();
1857d88c1a5aSDimitry Andric         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
18582754fe60SDimitry Andric       }
1859*b5893f02SDimitry Andric     }
18602754fe60SDimitry Andric 
18612754fe60SDimitry Andric     unsigned MinReg =
1862139f7f9bSDimitry Andric       std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1863139f7f9bSDimitry Andric                          TRI->getEncodingValue(MinG8R));
18642754fe60SDimitry Andric 
18652754fe60SDimitry Andric     if (Subtarget.isPPC64()) {
18662754fe60SDimitry Andric       LowerBound -= (31 - MinReg + 1) * 8;
18672754fe60SDimitry Andric     } else {
18682754fe60SDimitry Andric       LowerBound -= (31 - MinReg + 1) * 4;
18692754fe60SDimitry Andric     }
18702754fe60SDimitry Andric   }
18712754fe60SDimitry Andric 
18723861d79fSDimitry Andric   // For 32-bit only, the CR save area is below the general register
18733861d79fSDimitry Andric   // save area.  For 64-bit SVR4, the CR save area is addressed relative
18743861d79fSDimitry Andric   // to the stack pointer and hence does not need an adjustment here.
18753861d79fSDimitry Andric   // Only CR2 (the first nonvolatile spilled) has an associated frame
18763861d79fSDimitry Andric   // index so that we have a single uniform save area.
18773861d79fSDimitry Andric   if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
18782754fe60SDimitry Andric     // Adjust the frame index of the CR spill slot.
18792754fe60SDimitry Andric     for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
18802754fe60SDimitry Andric       unsigned Reg = CSI[i].getReg();
18812754fe60SDimitry Andric 
18823861d79fSDimitry Andric       if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
18833861d79fSDimitry Andric           // Leave Darwin logic as-is.
18843861d79fSDimitry Andric           || (!Subtarget.isSVR4ABI() &&
18853861d79fSDimitry Andric               (PPC::CRBITRCRegClass.contains(Reg) ||
18863861d79fSDimitry Andric                PPC::CRRCRegClass.contains(Reg)))) {
18872754fe60SDimitry Andric         int FI = CSI[i].getFrameIdx();
18882754fe60SDimitry Andric 
1889d88c1a5aSDimitry Andric         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
18902754fe60SDimitry Andric       }
18912754fe60SDimitry Andric     }
18922754fe60SDimitry Andric 
18932754fe60SDimitry Andric     LowerBound -= 4; // The CR save area is always 4 bytes long.
18942754fe60SDimitry Andric   }
18952754fe60SDimitry Andric 
18962754fe60SDimitry Andric   if (HasVRSAVESaveArea) {
18972754fe60SDimitry Andric     // FIXME SVR4: Is it actually possible to have multiple elements in CSI
18982754fe60SDimitry Andric     //             which have the VRSAVE register class?
18992754fe60SDimitry Andric     // Adjust the frame index of the VRSAVE spill slot.
19002754fe60SDimitry Andric     for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
19012754fe60SDimitry Andric       unsigned Reg = CSI[i].getReg();
19022754fe60SDimitry Andric 
19037ae0e2c9SDimitry Andric       if (PPC::VRSAVERCRegClass.contains(Reg)) {
19042754fe60SDimitry Andric         int FI = CSI[i].getFrameIdx();
19052754fe60SDimitry Andric 
1906d88c1a5aSDimitry Andric         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
19072754fe60SDimitry Andric       }
19082754fe60SDimitry Andric     }
19092754fe60SDimitry Andric 
19102754fe60SDimitry Andric     LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
19112754fe60SDimitry Andric   }
19122754fe60SDimitry Andric 
19134ba319b5SDimitry Andric   // Both Altivec and SPE have the same alignment and padding requirements
19144ba319b5SDimitry Andric   // within the stack frame.
19152754fe60SDimitry Andric   if (HasVRSaveArea) {
19164ba319b5SDimitry Andric     // Insert alignment padding, we need 16-byte alignment. Note: for positive
1917c4394386SDimitry Andric     // number the alignment formula is : y = (x + (n-1)) & (~(n-1)). But since
1918c4394386SDimitry Andric     // we are using negative number here (the stack grows downward). We should
1919c4394386SDimitry Andric     // use formula : y = x & (~(n-1)). Where x is the size before aligning, n
1920c4394386SDimitry Andric     // is the alignment size ( n = 16 here) and y is the size after aligning.
1921c4394386SDimitry Andric     assert(LowerBound <= 0 && "Expect LowerBound have a non-positive value!");
1922c4394386SDimitry Andric     LowerBound &= ~(15);
19232754fe60SDimitry Andric 
19242754fe60SDimitry Andric     for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
19252754fe60SDimitry Andric       int FI = VRegs[i].getFrameIdx();
19262754fe60SDimitry Andric 
1927d88c1a5aSDimitry Andric       MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
19282754fe60SDimitry Andric     }
19292754fe60SDimitry Andric   }
1930139f7f9bSDimitry Andric 
1931139f7f9bSDimitry Andric   addScavengingSpillSlot(MF, RS);
1932139f7f9bSDimitry Andric }
1933139f7f9bSDimitry Andric 
1934139f7f9bSDimitry Andric void
addScavengingSpillSlot(MachineFunction & MF,RegScavenger * RS) const1935139f7f9bSDimitry Andric PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1936139f7f9bSDimitry Andric                                          RegScavenger *RS) const {
1937139f7f9bSDimitry Andric   // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1938139f7f9bSDimitry Andric   // a large stack, which will require scavenging a register to materialize a
1939139f7f9bSDimitry Andric   // large offset.
1940139f7f9bSDimitry Andric 
1941139f7f9bSDimitry Andric   // We need to have a scavenger spill slot for spills if the frame size is
1942139f7f9bSDimitry Andric   // large. In case there is no free register for large-offset addressing,
1943139f7f9bSDimitry Andric   // this slot is used for the necessary emergency spill. Also, we need the
1944139f7f9bSDimitry Andric   // slot for dynamic stack allocations.
1945139f7f9bSDimitry Andric 
1946139f7f9bSDimitry Andric   // The scavenger might be invoked if the frame offset does not fit into
1947139f7f9bSDimitry Andric   // the 16-bit immediate. We don't know the complete frame size here
1948139f7f9bSDimitry Andric   // because we've not yet computed callee-saved register spills or the
1949139f7f9bSDimitry Andric   // needed alignment padding.
1950139f7f9bSDimitry Andric   unsigned StackSize = determineFrameLayout(MF, false, true);
1951d88c1a5aSDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
1952d88c1a5aSDimitry Andric   if (MFI.hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1953139f7f9bSDimitry Andric       hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
195451690af2SDimitry Andric     const TargetRegisterClass &GPRC = PPC::GPRCRegClass;
195551690af2SDimitry Andric     const TargetRegisterClass &G8RC = PPC::G8RCRegClass;
195651690af2SDimitry Andric     const TargetRegisterClass &RC = Subtarget.isPPC64() ? G8RC : GPRC;
195751690af2SDimitry Andric     const TargetRegisterInfo &TRI = *Subtarget.getRegisterInfo();
195851690af2SDimitry Andric     unsigned Size = TRI.getSpillSize(RC);
195951690af2SDimitry Andric     unsigned Align = TRI.getSpillAlignment(RC);
196051690af2SDimitry Andric     RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
1961139f7f9bSDimitry Andric 
1962f785676fSDimitry Andric     // Might we have over-aligned allocas?
1963d88c1a5aSDimitry Andric     bool HasAlVars = MFI.hasVarSizedObjects() &&
1964d88c1a5aSDimitry Andric                      MFI.getMaxAlignment() > getStackAlignment();
1965f785676fSDimitry Andric 
1966139f7f9bSDimitry Andric     // These kinds of spills might need two registers.
1967f785676fSDimitry Andric     if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
196851690af2SDimitry Andric       RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
1969139f7f9bSDimitry Andric 
1970139f7f9bSDimitry Andric   }
19712754fe60SDimitry Andric }
19723861d79fSDimitry Andric 
1973*b5893f02SDimitry Andric // This function checks if a callee saved gpr can be spilled to a volatile
1974*b5893f02SDimitry Andric // vector register. This occurs for leaf functions when the option
1975*b5893f02SDimitry Andric // ppc-enable-pe-vector-spills is enabled. If there are any remaining registers
1976*b5893f02SDimitry Andric // which were not spilled to vectors, return false so the target independent
1977*b5893f02SDimitry Andric // code can handle them by assigning a FrameIdx to a stack slot.
assignCalleeSavedSpillSlots(MachineFunction & MF,const TargetRegisterInfo * TRI,std::vector<CalleeSavedInfo> & CSI) const1978*b5893f02SDimitry Andric bool PPCFrameLowering::assignCalleeSavedSpillSlots(
1979*b5893f02SDimitry Andric     MachineFunction &MF, const TargetRegisterInfo *TRI,
1980*b5893f02SDimitry Andric     std::vector<CalleeSavedInfo> &CSI) const {
1981*b5893f02SDimitry Andric 
1982*b5893f02SDimitry Andric   if (CSI.empty())
1983*b5893f02SDimitry Andric     return true; // Early exit if no callee saved registers are modified!
1984*b5893f02SDimitry Andric 
1985*b5893f02SDimitry Andric   // Early exit if cannot spill gprs to volatile vector registers.
1986*b5893f02SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
1987*b5893f02SDimitry Andric   if (!EnablePEVectorSpills || MFI.hasCalls() || !Subtarget.hasP9Vector())
1988*b5893f02SDimitry Andric     return false;
1989*b5893f02SDimitry Andric 
1990*b5893f02SDimitry Andric   // Build a BitVector of VSRs that can be used for spilling GPRs.
1991*b5893f02SDimitry Andric   BitVector BVAllocatable = TRI->getAllocatableSet(MF);
1992*b5893f02SDimitry Andric   BitVector BVCalleeSaved(TRI->getNumRegs());
1993*b5893f02SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1994*b5893f02SDimitry Andric   const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
1995*b5893f02SDimitry Andric   for (unsigned i = 0; CSRegs[i]; ++i)
1996*b5893f02SDimitry Andric     BVCalleeSaved.set(CSRegs[i]);
1997*b5893f02SDimitry Andric 
1998*b5893f02SDimitry Andric   for (unsigned Reg : BVAllocatable.set_bits()) {
1999*b5893f02SDimitry Andric     // Set to 0 if the register is not a volatile VF/F8 register, or if it is
2000*b5893f02SDimitry Andric     // used in the function.
2001*b5893f02SDimitry Andric     if (BVCalleeSaved[Reg] ||
2002*b5893f02SDimitry Andric         (!PPC::F8RCRegClass.contains(Reg) &&
2003*b5893f02SDimitry Andric          !PPC::VFRCRegClass.contains(Reg)) ||
2004*b5893f02SDimitry Andric         (MF.getRegInfo().isPhysRegUsed(Reg)))
2005*b5893f02SDimitry Andric       BVAllocatable.reset(Reg);
2006*b5893f02SDimitry Andric   }
2007*b5893f02SDimitry Andric 
2008*b5893f02SDimitry Andric   bool AllSpilledToReg = true;
2009*b5893f02SDimitry Andric   for (auto &CS : CSI) {
2010*b5893f02SDimitry Andric     if (BVAllocatable.none())
2011*b5893f02SDimitry Andric       return false;
2012*b5893f02SDimitry Andric 
2013*b5893f02SDimitry Andric     unsigned Reg = CS.getReg();
2014*b5893f02SDimitry Andric     if (!PPC::G8RCRegClass.contains(Reg) && !PPC::GPRCRegClass.contains(Reg)) {
2015*b5893f02SDimitry Andric       AllSpilledToReg = false;
2016*b5893f02SDimitry Andric       continue;
2017*b5893f02SDimitry Andric     }
2018*b5893f02SDimitry Andric 
2019*b5893f02SDimitry Andric     unsigned VolatileVFReg = BVAllocatable.find_first();
2020*b5893f02SDimitry Andric     if (VolatileVFReg < BVAllocatable.size()) {
2021*b5893f02SDimitry Andric       CS.setDstReg(VolatileVFReg);
2022*b5893f02SDimitry Andric       BVAllocatable.reset(VolatileVFReg);
2023*b5893f02SDimitry Andric     } else {
2024*b5893f02SDimitry Andric       AllSpilledToReg = false;
2025*b5893f02SDimitry Andric     }
2026*b5893f02SDimitry Andric   }
2027*b5893f02SDimitry Andric   return AllSpilledToReg;
2028*b5893f02SDimitry Andric }
2029*b5893f02SDimitry Andric 
2030*b5893f02SDimitry Andric 
20313861d79fSDimitry Andric bool
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI) const20323861d79fSDimitry Andric PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
20333861d79fSDimitry Andric                                      MachineBasicBlock::iterator MI,
20343861d79fSDimitry Andric                                      const std::vector<CalleeSavedInfo> &CSI,
20353861d79fSDimitry Andric                                      const TargetRegisterInfo *TRI) const {
20363861d79fSDimitry Andric 
20373861d79fSDimitry Andric   // Currently, this function only handles SVR4 32- and 64-bit ABIs.
20383861d79fSDimitry Andric   // Return false otherwise to maintain pre-existing behavior.
20393861d79fSDimitry Andric   if (!Subtarget.isSVR4ABI())
20403861d79fSDimitry Andric     return false;
20413861d79fSDimitry Andric 
20423861d79fSDimitry Andric   MachineFunction *MF = MBB.getParent();
20437a7e6055SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
20443861d79fSDimitry Andric   DebugLoc DL;
20453861d79fSDimitry Andric   bool CRSpilled = false;
2046284c1978SDimitry Andric   MachineInstrBuilder CRMIB;
20473861d79fSDimitry Andric 
20483861d79fSDimitry Andric   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
20493861d79fSDimitry Andric     unsigned Reg = CSI[i].getReg();
2050f785676fSDimitry Andric     // Only Darwin actually uses the VRSAVE register, but it can still appear
2051f785676fSDimitry Andric     // here if, for example, @llvm.eh.unwind.init() is used.  If we're not on
2052f785676fSDimitry Andric     // Darwin, ignore it.
2053f785676fSDimitry Andric     if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
2054f785676fSDimitry Andric       continue;
2055f785676fSDimitry Andric 
20563861d79fSDimitry Andric     // CR2 through CR4 are the nonvolatile CR fields.
20573861d79fSDimitry Andric     bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
20583861d79fSDimitry Andric 
20593861d79fSDimitry Andric     // Add the callee-saved register as live-in; it's killed at the spill.
20604ba319b5SDimitry Andric     // Do not do this for callee-saved registers that are live-in to the
20614ba319b5SDimitry Andric     // function because they will already be marked live-in and this will be
20624ba319b5SDimitry Andric     // adding it for a second time. It is an error to add the same register
20634ba319b5SDimitry Andric     // to the set more than once.
20644ba319b5SDimitry Andric     const MachineRegisterInfo &MRI = MF->getRegInfo();
20654ba319b5SDimitry Andric     bool IsLiveIn = MRI.isLiveIn(Reg);
20664ba319b5SDimitry Andric     if (!IsLiveIn)
20673861d79fSDimitry Andric        MBB.addLiveIn(Reg);
20683861d79fSDimitry Andric 
2069284c1978SDimitry Andric     if (CRSpilled && IsCRField) {
2070284c1978SDimitry Andric       CRMIB.addReg(Reg, RegState::ImplicitKill);
2071284c1978SDimitry Andric       continue;
2072284c1978SDimitry Andric     }
2073284c1978SDimitry Andric 
20743861d79fSDimitry Andric     // Insert the spill to the stack frame.
20753861d79fSDimitry Andric     if (IsCRField) {
2076284c1978SDimitry Andric       PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
20773861d79fSDimitry Andric       if (Subtarget.isPPC64()) {
2078284c1978SDimitry Andric         // The actual spill will happen at the start of the prologue.
2079284c1978SDimitry Andric         FuncInfo->addMustSaveCR(Reg);
20803861d79fSDimitry Andric       } else {
2081284c1978SDimitry Andric         CRSpilled = true;
2082284c1978SDimitry Andric         FuncInfo->setSpillsCR();
2083284c1978SDimitry Andric 
20843861d79fSDimitry Andric         // 32-bit:  FP-relative.  Note that we made sure CR2-CR4 all have
20853861d79fSDimitry Andric         // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
2086284c1978SDimitry Andric         CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
2087284c1978SDimitry Andric                   .addReg(Reg, RegState::ImplicitKill);
2088284c1978SDimitry Andric 
2089284c1978SDimitry Andric         MBB.insert(MI, CRMIB);
20903861d79fSDimitry Andric         MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
20913861d79fSDimitry Andric                                          .addReg(PPC::R12,
20923861d79fSDimitry Andric                                                  getKillRegState(true)),
20933861d79fSDimitry Andric                                          CSI[i].getFrameIdx()));
20943861d79fSDimitry Andric       }
20953861d79fSDimitry Andric     } else {
2096*b5893f02SDimitry Andric       if (CSI[i].isSpilledToReg()) {
2097*b5893f02SDimitry Andric         NumPESpillVSR++;
2098*b5893f02SDimitry Andric         BuildMI(MBB, MI, DL, TII.get(PPC::MTVSRD), CSI[i].getDstReg())
2099*b5893f02SDimitry Andric           .addReg(Reg, getKillRegState(true));
2100*b5893f02SDimitry Andric       } else {
21013861d79fSDimitry Andric         const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
21024ba319b5SDimitry Andric         // Use !IsLiveIn for the kill flag.
21034ba319b5SDimitry Andric         // We do not want to kill registers that are live in this function
21044ba319b5SDimitry Andric         // before their use because they will become undefined registers.
21054ba319b5SDimitry Andric         TII.storeRegToStackSlot(MBB, MI, Reg, !IsLiveIn,
21063861d79fSDimitry Andric                                 CSI[i].getFrameIdx(), RC, TRI);
21073861d79fSDimitry Andric       }
21083861d79fSDimitry Andric     }
2109*b5893f02SDimitry Andric   }
21103861d79fSDimitry Andric   return true;
21113861d79fSDimitry Andric }
21123861d79fSDimitry Andric 
21133861d79fSDimitry Andric static void
restoreCRs(bool isPPC64,bool is31,bool CR2Spilled,bool CR3Spilled,bool CR4Spilled,MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,unsigned CSIIndex)2114284c1978SDimitry Andric restoreCRs(bool isPPC64, bool is31,
2115284c1978SDimitry Andric            bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
21163861d79fSDimitry Andric            MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
21173861d79fSDimitry Andric            const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
21183861d79fSDimitry Andric 
21193861d79fSDimitry Andric   MachineFunction *MF = MBB.getParent();
2120ff0cc061SDimitry Andric   const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
21213861d79fSDimitry Andric   DebugLoc DL;
21223861d79fSDimitry Andric   unsigned RestoreOp, MoveReg;
21233861d79fSDimitry Andric 
2124284c1978SDimitry Andric   if (isPPC64)
2125284c1978SDimitry Andric     // This is handled during epilogue generation.
2126284c1978SDimitry Andric     return;
2127284c1978SDimitry Andric   else {
21283861d79fSDimitry Andric     // 32-bit:  FP-relative
21293861d79fSDimitry Andric     MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
21303861d79fSDimitry Andric                                              PPC::R12),
21313861d79fSDimitry Andric                                      CSI[CSIIndex].getFrameIdx()));
2132f785676fSDimitry Andric     RestoreOp = PPC::MTOCRF;
21333861d79fSDimitry Andric     MoveReg = PPC::R12;
21343861d79fSDimitry Andric   }
21353861d79fSDimitry Andric 
21363861d79fSDimitry Andric   if (CR2Spilled)
21373861d79fSDimitry Andric     MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
2138139f7f9bSDimitry Andric                .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
21393861d79fSDimitry Andric 
21403861d79fSDimitry Andric   if (CR3Spilled)
21413861d79fSDimitry Andric     MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
2142139f7f9bSDimitry Andric                .addReg(MoveReg, getKillRegState(!CR4Spilled)));
21433861d79fSDimitry Andric 
21443861d79fSDimitry Andric   if (CR4Spilled)
21453861d79fSDimitry Andric     MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
2146139f7f9bSDimitry Andric                .addReg(MoveReg, getKillRegState(true)));
2147139f7f9bSDimitry Andric }
2148139f7f9bSDimitry Andric 
21493ca95b02SDimitry Andric MachineBasicBlock::iterator PPCFrameLowering::
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const2150139f7f9bSDimitry Andric eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
2151139f7f9bSDimitry Andric                               MachineBasicBlock::iterator I) const {
2152ff0cc061SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
2153139f7f9bSDimitry Andric   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
2154139f7f9bSDimitry Andric       I->getOpcode() == PPC::ADJCALLSTACKUP) {
2155139f7f9bSDimitry Andric     // Add (actually subtract) back the amount the callee popped on return.
2156139f7f9bSDimitry Andric     if (int CalleeAmt =  I->getOperand(1).getImm()) {
2157139f7f9bSDimitry Andric       bool is64Bit = Subtarget.isPPC64();
2158139f7f9bSDimitry Andric       CalleeAmt *= -1;
2159139f7f9bSDimitry Andric       unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
2160139f7f9bSDimitry Andric       unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
2161139f7f9bSDimitry Andric       unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
2162139f7f9bSDimitry Andric       unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
2163139f7f9bSDimitry Andric       unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
2164139f7f9bSDimitry Andric       unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
2165d88c1a5aSDimitry Andric       const DebugLoc &dl = I->getDebugLoc();
2166139f7f9bSDimitry Andric 
2167139f7f9bSDimitry Andric       if (isInt<16>(CalleeAmt)) {
2168139f7f9bSDimitry Andric         BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
2169139f7f9bSDimitry Andric           .addReg(StackReg, RegState::Kill)
2170139f7f9bSDimitry Andric           .addImm(CalleeAmt);
2171139f7f9bSDimitry Andric       } else {
2172139f7f9bSDimitry Andric         MachineBasicBlock::iterator MBBI = I;
2173139f7f9bSDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
2174139f7f9bSDimitry Andric           .addImm(CalleeAmt >> 16);
2175139f7f9bSDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
2176139f7f9bSDimitry Andric           .addReg(TmpReg, RegState::Kill)
2177139f7f9bSDimitry Andric           .addImm(CalleeAmt & 0xFFFF);
2178139f7f9bSDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
2179139f7f9bSDimitry Andric           .addReg(StackReg, RegState::Kill)
2180139f7f9bSDimitry Andric           .addReg(TmpReg);
2181139f7f9bSDimitry Andric       }
2182139f7f9bSDimitry Andric     }
2183139f7f9bSDimitry Andric   }
2184139f7f9bSDimitry Andric   // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
21853ca95b02SDimitry Andric   return MBB.erase(I);
21863861d79fSDimitry Andric }
21873861d79fSDimitry Andric 
21883861d79fSDimitry Andric bool
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI) const21893861d79fSDimitry Andric PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
21903861d79fSDimitry Andric                                         MachineBasicBlock::iterator MI,
21912cab237bSDimitry Andric                                         std::vector<CalleeSavedInfo> &CSI,
21923861d79fSDimitry Andric                                         const TargetRegisterInfo *TRI) const {
21933861d79fSDimitry Andric 
21943861d79fSDimitry Andric   // Currently, this function only handles SVR4 32- and 64-bit ABIs.
21953861d79fSDimitry Andric   // Return false otherwise to maintain pre-existing behavior.
21963861d79fSDimitry Andric   if (!Subtarget.isSVR4ABI())
21973861d79fSDimitry Andric     return false;
21983861d79fSDimitry Andric 
21993861d79fSDimitry Andric   MachineFunction *MF = MBB.getParent();
22007a7e6055SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
22013861d79fSDimitry Andric   bool CR2Spilled = false;
22023861d79fSDimitry Andric   bool CR3Spilled = false;
22033861d79fSDimitry Andric   bool CR4Spilled = false;
22043861d79fSDimitry Andric   unsigned CSIIndex = 0;
22053861d79fSDimitry Andric 
22063861d79fSDimitry Andric   // Initialize insertion-point logic; we will be restoring in reverse
22073861d79fSDimitry Andric   // order of spill.
22083861d79fSDimitry Andric   MachineBasicBlock::iterator I = MI, BeforeI = I;
22093861d79fSDimitry Andric   bool AtStart = I == MBB.begin();
22103861d79fSDimitry Andric 
22113861d79fSDimitry Andric   if (!AtStart)
22123861d79fSDimitry Andric     --BeforeI;
22133861d79fSDimitry Andric 
22143861d79fSDimitry Andric   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
22153861d79fSDimitry Andric     unsigned Reg = CSI[i].getReg();
22163861d79fSDimitry Andric 
2217f785676fSDimitry Andric     // Only Darwin actually uses the VRSAVE register, but it can still appear
2218f785676fSDimitry Andric     // here if, for example, @llvm.eh.unwind.init() is used.  If we're not on
2219f785676fSDimitry Andric     // Darwin, ignore it.
2220f785676fSDimitry Andric     if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
2221f785676fSDimitry Andric       continue;
2222f785676fSDimitry Andric 
22233861d79fSDimitry Andric     if (Reg == PPC::CR2) {
22243861d79fSDimitry Andric       CR2Spilled = true;
22253861d79fSDimitry Andric       // The spill slot is associated only with CR2, which is the
22263861d79fSDimitry Andric       // first nonvolatile spilled.  Save it here.
22273861d79fSDimitry Andric       CSIIndex = i;
22283861d79fSDimitry Andric       continue;
22293861d79fSDimitry Andric     } else if (Reg == PPC::CR3) {
22303861d79fSDimitry Andric       CR3Spilled = true;
22313861d79fSDimitry Andric       continue;
22323861d79fSDimitry Andric     } else if (Reg == PPC::CR4) {
22333861d79fSDimitry Andric       CR4Spilled = true;
22343861d79fSDimitry Andric       continue;
22353861d79fSDimitry Andric     } else {
22363861d79fSDimitry Andric       // When we first encounter a non-CR register after seeing at
22373861d79fSDimitry Andric       // least one CR register, restore all spilled CRs together.
22383861d79fSDimitry Andric       if ((CR2Spilled || CR3Spilled || CR4Spilled)
22393861d79fSDimitry Andric           && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
2240284c1978SDimitry Andric         bool is31 = needsFP(*MF);
2241284c1978SDimitry Andric         restoreCRs(Subtarget.isPPC64(), is31,
2242284c1978SDimitry Andric                    CR2Spilled, CR3Spilled, CR4Spilled,
22433861d79fSDimitry Andric                    MBB, I, CSI, CSIIndex);
22443861d79fSDimitry Andric         CR2Spilled = CR3Spilled = CR4Spilled = false;
22453861d79fSDimitry Andric       }
22463861d79fSDimitry Andric 
2247*b5893f02SDimitry Andric       if (CSI[i].isSpilledToReg()) {
2248*b5893f02SDimitry Andric         DebugLoc DL;
2249*b5893f02SDimitry Andric         NumPEReloadVSR++;
2250*b5893f02SDimitry Andric         BuildMI(MBB, I, DL, TII.get(PPC::MFVSRD), Reg)
2251*b5893f02SDimitry Andric             .addReg(CSI[i].getDstReg(), getKillRegState(true));
2252*b5893f02SDimitry Andric       } else {
22533861d79fSDimitry Andric        // Default behavior for non-CR saves.
22543861d79fSDimitry Andric         const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2255*b5893f02SDimitry Andric         TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(), RC, TRI);
22563861d79fSDimitry Andric         assert(I != MBB.begin() &&
22573861d79fSDimitry Andric                "loadRegFromStackSlot didn't insert any code!");
22583861d79fSDimitry Andric       }
2259*b5893f02SDimitry Andric     }
22603861d79fSDimitry Andric 
22613861d79fSDimitry Andric     // Insert in reverse order.
22623861d79fSDimitry Andric     if (AtStart)
22633861d79fSDimitry Andric       I = MBB.begin();
22643861d79fSDimitry Andric     else {
22653861d79fSDimitry Andric       I = BeforeI;
22663861d79fSDimitry Andric       ++I;
22673861d79fSDimitry Andric     }
22683861d79fSDimitry Andric   }
22693861d79fSDimitry Andric 
22703861d79fSDimitry Andric   // If we haven't yet spilled the CRs, do so now.
2271284c1978SDimitry Andric   if (CR2Spilled || CR3Spilled || CR4Spilled) {
2272284c1978SDimitry Andric     bool is31 = needsFP(*MF);
2273284c1978SDimitry Andric     restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
22743861d79fSDimitry Andric                MBB, I, CSI, CSIIndex);
2275284c1978SDimitry Andric   }
22763861d79fSDimitry Andric 
22773861d79fSDimitry Andric   return true;
22783861d79fSDimitry Andric }
22797d523365SDimitry Andric 
enableShrinkWrapping(const MachineFunction & MF) const22807d523365SDimitry Andric bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
22814ba319b5SDimitry Andric   if (MF.getInfo<PPCFunctionInfo>()->shrinkWrapDisabled())
22824ba319b5SDimitry Andric     return false;
22837d523365SDimitry Andric   return (MF.getSubtarget<PPCSubtarget>().isSVR4ABI() &&
22847d523365SDimitry Andric           MF.getSubtarget<PPCSubtarget>().isPPC64());
22857d523365SDimitry Andric }
2286