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