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