Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 1 | //===-- Mips16FrameLowering.cpp - Mips16 Frame Information ----------------===// |
| 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 | // |
| 10 | // This file contains the Mips16 implementation of TargetFrameLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Mips16FrameLowering.h" |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/MipsBaseInfo.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 16 | #include "Mips16InstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "MipsInstrInfo.h" |
Reed Kotler | 0ff4001 | 2013-12-10 14:29:38 +0000 | [diff] [blame] | 18 | #include "MipsRegisterInfo.h" |
Eric Christopher | 4cdb3f9 | 2014-07-02 23:29:55 +0000 | [diff] [blame^] | 19 | #include "MipsSubtarget.h" |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/Function.h" |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetOptions.h" |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Eric Christopher | 4cdb3f9 | 2014-07-02 23:29:55 +0000 | [diff] [blame^] | 32 | Mips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI) |
| 33 | : MipsFrameLowering(STI, STI.stackAlignment()) {} |
| 34 | |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 35 | void Mips16FrameLowering::emitPrologue(MachineFunction &MF) const { |
| 36 | MachineBasicBlock &MBB = MF.front(); |
| 37 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Reed Kotler | d019dbf | 2012-12-20 04:07:42 +0000 | [diff] [blame] | 38 | const Mips16InstrInfo &TII = |
| 39 | *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo()); |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 40 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 41 | DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 42 | uint64_t StackSize = MFI->getStackSize(); |
| 43 | |
| 44 | // No need to allocate space on the stack. |
| 45 | if (StackSize == 0 && !MFI->adjustsStack()) return; |
| 46 | |
Reed Kotler | d11acc7 | 2012-12-20 06:59:37 +0000 | [diff] [blame] | 47 | MachineModuleInfo &MMI = MF.getMMI(); |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 48 | const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); |
Reed Kotler | d11acc7 | 2012-12-20 06:59:37 +0000 | [diff] [blame] | 49 | MachineLocation DstML, SrcML; |
| 50 | |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 51 | // Adjust stack. |
Reed Kotler | d019dbf | 2012-12-20 04:07:42 +0000 | [diff] [blame] | 52 | TII.makeFrame(Mips::SP, StackSize, MBB, MBBI); |
Reed Kotler | 3589dd7 | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 53 | |
Reed Kotler | d11acc7 | 2012-12-20 06:59:37 +0000 | [diff] [blame] | 54 | // emit ".cfi_def_cfa_offset StackSize" |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 55 | unsigned CFIIndex = MMI.addFrameInst( |
| 56 | MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize)); |
| 57 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 58 | .addCFIIndex(CFIIndex); |
Reed Kotler | d11acc7 | 2012-12-20 06:59:37 +0000 | [diff] [blame] | 59 | |
Reed Kotler | 5c29d63 | 2013-12-15 20:49:30 +0000 | [diff] [blame] | 60 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 61 | |
| 62 | if (CSI.size()) { |
Reed Kotler | 06b3c4f | 2013-12-15 23:03:35 +0000 | [diff] [blame] | 63 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
Reed Kotler | 0ff4001 | 2013-12-10 14:29:38 +0000 | [diff] [blame] | 64 | |
Reed Kotler | 06b3c4f | 2013-12-15 23:03:35 +0000 | [diff] [blame] | 65 | for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(), |
Reed Kotler | 5c29d63 | 2013-12-15 20:49:30 +0000 | [diff] [blame] | 66 | E = CSI.end(); I != E; ++I) { |
Reed Kotler | 06b3c4f | 2013-12-15 23:03:35 +0000 | [diff] [blame] | 67 | int64_t Offset = MFI->getObjectOffset(I->getFrameIdx()); |
| 68 | unsigned Reg = I->getReg(); |
| 69 | unsigned DReg = MRI->getDwarfRegNum(Reg, true); |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 70 | unsigned CFIIndex = MMI.addFrameInst( |
| 71 | MCCFIInstruction::createOffset(nullptr, DReg, Offset)); |
| 72 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 73 | .addCFIIndex(CFIIndex); |
Reed Kotler | 06b3c4f | 2013-12-15 23:03:35 +0000 | [diff] [blame] | 74 | } |
Reed Kotler | 5c29d63 | 2013-12-15 20:49:30 +0000 | [diff] [blame] | 75 | } |
Reed Kotler | 3589dd7 | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 76 | if (hasFP(MF)) |
| 77 | BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0) |
Eric Christopher | b45b481 | 2014-04-14 22:21:22 +0000 | [diff] [blame] | 78 | .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup); |
Reed Kotler | 3589dd7 | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 79 | |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void Mips16FrameLowering::emitEpilogue(MachineFunction &MF, |
| 83 | MachineBasicBlock &MBB) const { |
| 84 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
| 85 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Reed Kotler | d019dbf | 2012-12-20 04:07:42 +0000 | [diff] [blame] | 86 | const Mips16InstrInfo &TII = |
| 87 | *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo()); |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 88 | DebugLoc dl = MBBI->getDebugLoc(); |
| 89 | uint64_t StackSize = MFI->getStackSize(); |
| 90 | |
| 91 | if (!StackSize) |
| 92 | return; |
| 93 | |
Reed Kotler | 3589dd7 | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 94 | if (hasFP(MF)) |
| 95 | BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP) |
| 96 | .addReg(Mips::S0); |
| 97 | |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 98 | // Adjust stack. |
Reed Kotler | d019dbf | 2012-12-20 04:07:42 +0000 | [diff] [blame] | 99 | // assumes stacksize multiple of 8 |
| 100 | TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI); |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | bool Mips16FrameLowering:: |
| 104 | spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 105 | MachineBasicBlock::iterator MI, |
| 106 | const std::vector<CalleeSavedInfo> &CSI, |
| 107 | const TargetRegisterInfo *TRI) const { |
Akira Hatanaka | cd04e2b | 2012-09-21 01:08:16 +0000 | [diff] [blame] | 108 | MachineFunction *MF = MBB.getParent(); |
| 109 | MachineBasicBlock *EntryBlock = MF->begin(); |
Akira Hatanaka | cd04e2b | 2012-09-21 01:08:16 +0000 | [diff] [blame] | 110 | |
| 111 | // |
| 112 | // Registers RA, S0,S1 are the callee saved registers and they |
| 113 | // will be saved with the "save" instruction |
| 114 | // during emitPrologue |
| 115 | // |
| 116 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 117 | // Add the callee-saved register as live-in. Do not add if the register is |
| 118 | // RA and return address is taken, because it has already been added in |
| 119 | // method MipsTargetLowering::LowerRETURNADDR. |
| 120 | // It's killed at the spill, unless the register is RA and return address |
| 121 | // is taken. |
| 122 | unsigned Reg = CSI[i].getReg(); |
| 123 | bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA) |
| 124 | && MF->getFrameInfo()->isReturnAddressTaken(); |
| 125 | if (!IsRAAndRetAddrIsTaken) |
| 126 | EntryBlock->addLiveIn(Reg); |
| 127 | } |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 133 | MachineBasicBlock::iterator MI, |
| 134 | const std::vector<CalleeSavedInfo> &CSI, |
| 135 | const TargetRegisterInfo *TRI) const { |
| 136 | // |
| 137 | // Registers RA,S0,S1 are the callee saved registers and they will be restored |
| 138 | // with the restore instruction during emitEpilogue. |
| 139 | // We need to override this virtual function, otherwise llvm will try and |
| 140 | // restore the registers on it's on from the stack. |
| 141 | // |
| 142 | |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 146 | // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions |
| 147 | void Mips16FrameLowering:: |
| 148 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 149 | MachineBasicBlock::iterator I) const { |
| 150 | if (!hasReservedCallFrame(MF)) { |
| 151 | int64_t Amount = I->getOperand(0).getImm(); |
| 152 | |
| 153 | if (I->getOpcode() == Mips::ADJCALLSTACKDOWN) |
| 154 | Amount = -Amount; |
| 155 | |
| 156 | const Mips16InstrInfo &TII = |
| 157 | *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo()); |
| 158 | |
| 159 | TII.adjustStackPtr(Mips::SP, Amount, MBB, I); |
| 160 | } |
| 161 | |
| 162 | MBB.erase(I); |
| 163 | } |
| 164 | |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 165 | bool |
| 166 | Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Reed Kotler | 27a7229 | 2012-10-31 05:21:10 +0000 | [diff] [blame] | 167 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 168 | // Reserve call frame if the size of the maximum call frame fits into 15-bit |
| 169 | // immediate field and there are no variable sized objects on the stack. |
| 170 | return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects(); |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void Mips16FrameLowering:: |
| 174 | processFunctionBeforeCalleeSavedScan(MachineFunction &MF, |
| 175 | RegScavenger *RS) const { |
Reed Kotler | 5c29d63 | 2013-12-15 20:49:30 +0000 | [diff] [blame] | 176 | const Mips16InstrInfo &TII = |
| 177 | *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo()); |
| 178 | const MipsRegisterInfo &RI = TII.getRegisterInfo(); |
| 179 | const BitVector Reserved = RI.getReservedRegs(MF); |
| 180 | bool SaveS2 = Reserved[Mips::S2]; |
| 181 | if (SaveS2) |
| 182 | MF.getRegInfo().setPhysRegUsed(Mips::S2); |
| 183 | if (hasFP(MF)) |
| 184 | MF.getRegInfo().setPhysRegUsed(Mips::S0); |
Akira Hatanaka | d1c43ce | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 185 | } |
Akira Hatanaka | fab8929 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 186 | |
| 187 | const MipsFrameLowering * |
| 188 | llvm::createMips16FrameLowering(const MipsSubtarget &ST) { |
| 189 | return new Mips16FrameLowering(ST); |
| 190 | } |