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