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