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