Akira Hatanaka | cdb3ba7 | 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" |
Reed Kotler | cef95f7 | 2012-12-20 04:07:42 +0000 | [diff] [blame^] | 15 | #include "Mips16InstrInfo.h" |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/MipsBaseInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "MipsInstrInfo.h" |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +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" |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 23 | #include "llvm/DataLayout.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Function.h" |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOptions.h" |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | void Mips16FrameLowering::emitPrologue(MachineFunction &MF) const { |
| 31 | MachineBasicBlock &MBB = MF.front(); |
| 32 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Reed Kotler | cef95f7 | 2012-12-20 04:07:42 +0000 | [diff] [blame^] | 33 | const Mips16InstrInfo &TII = |
| 34 | *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo()); |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 35 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 36 | DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 37 | uint64_t StackSize = MFI->getStackSize(); |
| 38 | |
| 39 | // No need to allocate space on the stack. |
| 40 | if (StackSize == 0 && !MFI->adjustsStack()) return; |
| 41 | |
| 42 | // Adjust stack. |
Reed Kotler | cef95f7 | 2012-12-20 04:07:42 +0000 | [diff] [blame^] | 43 | TII.makeFrame(Mips::SP, StackSize, MBB, MBBI); |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 44 | |
| 45 | if (hasFP(MF)) |
| 46 | BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0) |
| 47 | .addReg(Mips::SP); |
| 48 | |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void Mips16FrameLowering::emitEpilogue(MachineFunction &MF, |
| 52 | MachineBasicBlock &MBB) const { |
| 53 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
| 54 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Reed Kotler | cef95f7 | 2012-12-20 04:07:42 +0000 | [diff] [blame^] | 55 | const Mips16InstrInfo &TII = |
| 56 | *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo()); |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 57 | DebugLoc dl = MBBI->getDebugLoc(); |
| 58 | uint64_t StackSize = MFI->getStackSize(); |
| 59 | |
| 60 | if (!StackSize) |
| 61 | return; |
| 62 | |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 63 | if (hasFP(MF)) |
| 64 | BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP) |
| 65 | .addReg(Mips::S0); |
| 66 | |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 67 | // Adjust stack. |
Reed Kotler | cef95f7 | 2012-12-20 04:07:42 +0000 | [diff] [blame^] | 68 | // assumes stacksize multiple of 8 |
| 69 | TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI); |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool Mips16FrameLowering:: |
| 73 | spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 74 | MachineBasicBlock::iterator MI, |
| 75 | const std::vector<CalleeSavedInfo> &CSI, |
| 76 | const TargetRegisterInfo *TRI) const { |
Akira Hatanaka | 0fdf3b0 | 2012-09-21 01:08:16 +0000 | [diff] [blame] | 77 | MachineFunction *MF = MBB.getParent(); |
| 78 | MachineBasicBlock *EntryBlock = MF->begin(); |
Akira Hatanaka | 0fdf3b0 | 2012-09-21 01:08:16 +0000 | [diff] [blame] | 79 | |
| 80 | // |
| 81 | // Registers RA, S0,S1 are the callee saved registers and they |
| 82 | // will be saved with the "save" instruction |
| 83 | // during emitPrologue |
| 84 | // |
| 85 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 86 | // Add the callee-saved register as live-in. Do not add if the register is |
| 87 | // RA and return address is taken, because it has already been added in |
| 88 | // method MipsTargetLowering::LowerRETURNADDR. |
| 89 | // It's killed at the spill, unless the register is RA and return address |
| 90 | // is taken. |
| 91 | unsigned Reg = CSI[i].getReg(); |
| 92 | bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA) |
| 93 | && MF->getFrameInfo()->isReturnAddressTaken(); |
| 94 | if (!IsRAAndRetAddrIsTaken) |
| 95 | EntryBlock->addLiveIn(Reg); |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 102 | MachineBasicBlock::iterator MI, |
| 103 | const std::vector<CalleeSavedInfo> &CSI, |
| 104 | const TargetRegisterInfo *TRI) const { |
| 105 | // |
| 106 | // Registers RA,S0,S1 are the callee saved registers and they will be restored |
| 107 | // with the restore instruction during emitEpilogue. |
| 108 | // We need to override this virtual function, otherwise llvm will try and |
| 109 | // restore the registers on it's on from the stack. |
| 110 | // |
| 111 | |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
| 115 | bool |
| 116 | Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Reed Kotler | 9441125 | 2012-10-31 05:21:10 +0000 | [diff] [blame] | 117 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 118 | // Reserve call frame if the size of the maximum call frame fits into 15-bit |
| 119 | // immediate field and there are no variable sized objects on the stack. |
| 120 | return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects(); |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void Mips16FrameLowering:: |
| 124 | processFunctionBeforeCalleeSavedScan(MachineFunction &MF, |
| 125 | RegScavenger *RS) const { |
Akira Hatanaka | 0fdf3b0 | 2012-09-21 01:08:16 +0000 | [diff] [blame] | 126 | MF.getRegInfo().setPhysRegUsed(Mips::RA); |
| 127 | MF.getRegInfo().setPhysRegUsed(Mips::S0); |
| 128 | MF.getRegInfo().setPhysRegUsed(Mips::S1); |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 129 | } |
Akira Hatanaka | af26626 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 130 | |
| 131 | const MipsFrameLowering * |
| 132 | llvm::createMips16FrameLowering(const MipsSubtarget &ST) { |
| 133 | return new Mips16FrameLowering(ST); |
| 134 | } |