Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- SparcFrameLowering.cpp - Sparc Frame Information ------------------===// |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 10 | // This file contains the Sparc implementation of TargetFrameLowering class. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 14 | #include "SparcFrameLowering.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 15 | #include "SparcInstrInfo.h" |
| 16 | #include "SparcMachineFunctionInfo.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 20 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/Function.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 29 | static cl::opt<bool> |
| 30 | DisableLeafProc("disable-sparc-leaf-proc", |
Venkatraman Govindaraju | 3e8c7d9 | 2013-06-02 02:24:27 +0000 | [diff] [blame] | 31 | cl::init(false), |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 32 | cl::desc("Disable Sparc leaf procedure optimization."), |
| 33 | cl::Hidden); |
| 34 | |
| 35 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 36 | void SparcFrameLowering::emitPrologue(MachineFunction &MF) const { |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 37 | SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>(); |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 38 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 39 | MachineBasicBlock &MBB = MF.front(); |
| 40 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 41 | const SparcInstrInfo &TII = |
| 42 | *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 43 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 44 | DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 45 | |
| 46 | // Get the number of bytes to allocate from the FrameInfo |
| 47 | int NumBytes = (int) MFI->getStackSize(); |
| 48 | |
Venkatraman Govindaraju | 3521dcd | 2013-06-01 04:51:18 +0000 | [diff] [blame] | 49 | unsigned SAVEri = SP::SAVEri; |
| 50 | unsigned SAVErr = SP::SAVErr; |
| 51 | if (FuncInfo->isLeafProc()) { |
| 52 | if (NumBytes == 0) |
| 53 | return; |
| 54 | SAVEri = SP::ADDri; |
| 55 | SAVErr = SP::ADDrr; |
Jakob Stoklund Olesen | 2cfe46f | 2013-04-09 04:37:47 +0000 | [diff] [blame] | 56 | } |
Venkatraman Govindaraju | 3521dcd | 2013-06-01 04:51:18 +0000 | [diff] [blame] | 57 | NumBytes = - SubTarget.getAdjustedFrameSize(NumBytes); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 58 | |
| 59 | if (NumBytes >= -4096) { |
Venkatraman Govindaraju | 3521dcd | 2013-06-01 04:51:18 +0000 | [diff] [blame] | 60 | BuildMI(MBB, MBBI, dl, TII.get(SAVEri), SP::O6) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 61 | .addReg(SP::O6).addImm(NumBytes); |
| 62 | } else { |
| 63 | // Emit this the hard way. This clobbers G1 which we always know is |
| 64 | // available here. |
| 65 | unsigned OffHi = (unsigned)NumBytes >> 10U; |
| 66 | BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi); |
| 67 | // Emit G1 = G1 + I6 |
| 68 | BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1) |
| 69 | .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1)); |
Venkatraman Govindaraju | 3521dcd | 2013-06-01 04:51:18 +0000 | [diff] [blame] | 70 | BuildMI(MBB, MBBI, dl, TII.get(SAVErr), SP::O6) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 71 | .addReg(SP::O6).addReg(SP::G1); |
| 72 | } |
Venkatraman Govindaraju | 4c0cdd7 | 2013-09-26 15:11:00 +0000 | [diff] [blame^] | 73 | MachineModuleInfo &MMI = MF.getMMI(); |
| 74 | const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); |
| 75 | MCSymbol *FrameLabel = MMI.getContext().CreateTempSymbol(); |
| 76 | BuildMI(MBB, MBBI, dl, TII.get(SP::PROLOG_LABEL)).addSym(FrameLabel); |
| 77 | |
| 78 | unsigned regFP = MRI->getDwarfRegNum(SP::I6, true); |
| 79 | |
| 80 | // Emit ".cfi_def_cfa_register 30". |
| 81 | MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(FrameLabel, |
| 82 | regFP)); |
| 83 | // Emit ".cfi_window_save". |
| 84 | MMI.addFrameInst(MCCFIInstruction::createWindowSave(FrameLabel)); |
| 85 | |
| 86 | unsigned regInRA = MRI->getDwarfRegNum(SP::I7, true); |
| 87 | unsigned regOutRA = MRI->getDwarfRegNum(SP::O7, true); |
| 88 | // Emit ".cfi_register 15, 31". |
| 89 | MMI.addFrameInst(MCCFIInstruction::createRegister(FrameLabel, |
| 90 | regOutRA, |
| 91 | regInRA)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 94 | void SparcFrameLowering:: |
| 95 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 96 | MachineBasicBlock::iterator I) const { |
Jakob Stoklund Olesen | 2cfe46f | 2013-04-09 04:37:47 +0000 | [diff] [blame] | 97 | if (!hasReservedCallFrame(MF)) { |
| 98 | MachineInstr &MI = *I; |
| 99 | DebugLoc DL = MI.getDebugLoc(); |
| 100 | int Size = MI.getOperand(0).getImm(); |
| 101 | if (MI.getOpcode() == SP::ADJCALLSTACKDOWN) |
| 102 | Size = -Size; |
| 103 | const SparcInstrInfo &TII = |
| 104 | *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 105 | if (Size) |
| 106 | BuildMI(MBB, I, DL, TII.get(SP::ADDri), SP::O6).addReg(SP::O6) |
| 107 | .addImm(Size); |
| 108 | } |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 109 | MBB.erase(I); |
| 110 | } |
| 111 | |
| 112 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 113 | void SparcFrameLowering::emitEpilogue(MachineFunction &MF, |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 114 | MachineBasicBlock &MBB) const { |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 115 | SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>(); |
Jakob Stoklund Olesen | 4bc5e38 | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 116 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 117 | const SparcInstrInfo &TII = |
| 118 | *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 119 | DebugLoc dl = MBBI->getDebugLoc(); |
| 120 | assert(MBBI->getOpcode() == SP::RETL && |
| 121 | "Can only put epilog before 'retl' instruction!"); |
Venkatraman Govindaraju | 3521dcd | 2013-06-01 04:51:18 +0000 | [diff] [blame] | 122 | if (!FuncInfo->isLeafProc()) { |
| 123 | BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0) |
| 124 | .addReg(SP::G0); |
| 125 | return; |
| 126 | } |
| 127 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 128 | |
| 129 | int NumBytes = (int) MFI->getStackSize(); |
| 130 | if (NumBytes == 0) |
| 131 | return; |
| 132 | |
| 133 | NumBytes = SubTarget.getAdjustedFrameSize(NumBytes); |
| 134 | |
| 135 | if (NumBytes < 4096) { |
| 136 | BuildMI(MBB, MBBI, dl, TII.get(SP::ADDri), SP::O6) |
| 137 | .addReg(SP::O6).addImm(NumBytes); |
| 138 | } else { |
| 139 | // Emit this the hard way. This clobbers G1 which we always know is |
| 140 | // available here. |
| 141 | unsigned OffHi = (unsigned)NumBytes >> 10U; |
| 142 | BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi); |
| 143 | // Emit G1 = G1 + I6 |
| 144 | BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1) |
| 145 | .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1)); |
| 146 | BuildMI(MBB, MBBI, dl, TII.get(SP::ADDrr), SP::O6) |
| 147 | .addReg(SP::O6).addReg(SP::G1); |
| 148 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 149 | } |
Venkatraman Govindaraju | 641b0b5 | 2013-05-17 15:14:34 +0000 | [diff] [blame] | 150 | |
| 151 | bool SparcFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Venkatraman Govindaraju | a54533ed | 2013-06-04 18:33:25 +0000 | [diff] [blame] | 152 | // Reserve call frame if there are no variable sized objects on the stack. |
Venkatraman Govindaraju | 641b0b5 | 2013-05-17 15:14:34 +0000 | [diff] [blame] | 153 | return !MF.getFrameInfo()->hasVarSizedObjects(); |
| 154 | } |
| 155 | |
| 156 | // hasFP - Return true if the specified function should have a dedicated frame |
| 157 | // pointer register. This is true if the function has variable sized allocas or |
| 158 | // if frame pointer elimination is disabled. |
| 159 | bool SparcFrameLowering::hasFP(const MachineFunction &MF) const { |
| 160 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 161 | return MF.getTarget().Options.DisableFramePointerElim(MF) || |
| 162 | MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken(); |
| 163 | } |
| 164 | |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 165 | |
NAKAMURA Takumi | dbd3bbe | 2013-05-29 12:10:42 +0000 | [diff] [blame] | 166 | static bool LLVM_ATTRIBUTE_UNUSED verifyLeafProcRegUse(MachineRegisterInfo *MRI) |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 167 | { |
| 168 | |
| 169 | for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) |
| 170 | if (MRI->isPhysRegUsed(reg)) |
| 171 | return false; |
| 172 | |
| 173 | for (unsigned reg = SP::L0; reg <= SP::L7; ++reg) |
| 174 | if (MRI->isPhysRegUsed(reg)) |
| 175 | return false; |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | bool SparcFrameLowering::isLeafProc(MachineFunction &MF) const |
| 181 | { |
| 182 | |
| 183 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 184 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 185 | |
| 186 | return !(MFI->hasCalls() // has calls |
| 187 | || MRI.isPhysRegUsed(SP::L0) // Too many registers needed |
| 188 | || MRI.isPhysRegUsed(SP::O6) // %SP is used |
| 189 | || hasFP(MF)); // need %FP |
| 190 | } |
| 191 | |
| 192 | void SparcFrameLowering::remapRegsForLeafProc(MachineFunction &MF) const { |
| 193 | |
| 194 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 195 | |
Venkatraman Govindaraju | a54533ed | 2013-06-04 18:33:25 +0000 | [diff] [blame] | 196 | // Remap %i[0-7] to %o[0-7]. |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 197 | for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) { |
| 198 | if (!MRI.isPhysRegUsed(reg)) |
| 199 | continue; |
| 200 | unsigned mapped_reg = (reg - SP::I0 + SP::O0); |
| 201 | assert(!MRI.isPhysRegUsed(mapped_reg)); |
| 202 | |
Venkatraman Govindaraju | a54533ed | 2013-06-04 18:33:25 +0000 | [diff] [blame] | 203 | // Replace I register with O register. |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 204 | MRI.replaceRegWith(reg, mapped_reg); |
| 205 | |
Venkatraman Govindaraju | a54533ed | 2013-06-04 18:33:25 +0000 | [diff] [blame] | 206 | // Mark the reg unused. |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 207 | MRI.setPhysRegUnused(reg); |
| 208 | } |
| 209 | |
Venkatraman Govindaraju | fee76fa | 2013-07-30 19:53:10 +0000 | [diff] [blame] | 210 | // Rewrite MBB's Live-ins. |
| 211 | for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); |
| 212 | MBB != E; ++MBB) { |
| 213 | for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) { |
| 214 | if (!MBB->isLiveIn(reg)) |
| 215 | continue; |
| 216 | MBB->removeLiveIn(reg); |
| 217 | MBB->addLiveIn(reg - SP::I0 + SP::O0); |
| 218 | } |
| 219 | } |
| 220 | |
Venkatraman Govindaraju | ca0fe2f5 | 2013-05-29 04:46:31 +0000 | [diff] [blame] | 221 | assert(verifyLeafProcRegUse(&MRI)); |
| 222 | #ifdef XDEBUG |
| 223 | MF.verify(0, "After LeafProc Remapping"); |
| 224 | #endif |
| 225 | } |
| 226 | |
| 227 | void SparcFrameLowering::processFunctionBeforeCalleeSavedScan |
| 228 | (MachineFunction &MF, RegScavenger *RS) const { |
| 229 | |
| 230 | if (!DisableLeafProc && isLeafProc(MF)) { |
| 231 | SparcMachineFunctionInfo *MFI = MF.getInfo<SparcMachineFunctionInfo>(); |
| 232 | MFI->setLeafProc(true); |
| 233 | |
| 234 | remapRegsForLeafProc(MF); |
| 235 | } |
| 236 | |
| 237 | } |