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 | |
Reed Kotler | 27210d2 | 2012-12-20 06:59:37 +0000 | [diff] [blame^] | 42 | MachineModuleInfo &MMI = MF.getMMI(); |
| 43 | std::vector<MachineMove> &Moves = MMI.getFrameMoves(); |
| 44 | MachineLocation DstML, SrcML; |
| 45 | |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 46 | // Adjust stack. |
Reed Kotler | cef95f7 | 2012-12-20 04:07:42 +0000 | [diff] [blame] | 47 | TII.makeFrame(Mips::SP, StackSize, MBB, MBBI); |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 48 | |
Reed Kotler | 27210d2 | 2012-12-20 06:59:37 +0000 | [diff] [blame^] | 49 | // emit ".cfi_def_cfa_offset StackSize" |
| 50 | MCSymbol *AdjustSPLabel = MMI.getContext().CreateTempSymbol(); |
| 51 | BuildMI(MBB, MBBI, dl, |
| 52 | TII.get(TargetOpcode::PROLOG_LABEL)).addSym(AdjustSPLabel); |
| 53 | DstML = MachineLocation(MachineLocation::VirtualFP); |
| 54 | SrcML = MachineLocation(MachineLocation::VirtualFP, -StackSize); |
| 55 | Moves.push_back(MachineMove(AdjustSPLabel, DstML, SrcML)); |
| 56 | |
| 57 | MCSymbol *CSLabel = MMI.getContext().CreateTempSymbol(); |
| 58 | BuildMI(MBB, MBBI, dl, |
| 59 | TII.get(TargetOpcode::PROLOG_LABEL)).addSym(CSLabel); |
| 60 | DstML = MachineLocation(MachineLocation::VirtualFP, -8); |
| 61 | SrcML = MachineLocation(Mips::S1); |
| 62 | Moves.push_back(MachineMove(CSLabel, DstML, SrcML)); |
| 63 | |
| 64 | DstML = MachineLocation(MachineLocation::VirtualFP, -12); |
| 65 | SrcML = MachineLocation(Mips::S0); |
| 66 | Moves.push_back(MachineMove(CSLabel, DstML, SrcML)); |
| 67 | |
| 68 | DstML = MachineLocation(MachineLocation::VirtualFP, -4); |
| 69 | SrcML = MachineLocation(Mips::RA); |
| 70 | Moves.push_back(MachineMove(CSLabel, DstML, SrcML)); |
| 71 | |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 72 | if (hasFP(MF)) |
| 73 | BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0) |
| 74 | .addReg(Mips::SP); |
| 75 | |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void Mips16FrameLowering::emitEpilogue(MachineFunction &MF, |
| 79 | MachineBasicBlock &MBB) const { |
| 80 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
| 81 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Reed Kotler | cef95f7 | 2012-12-20 04:07:42 +0000 | [diff] [blame] | 82 | const Mips16InstrInfo &TII = |
| 83 | *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo()); |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 84 | DebugLoc dl = MBBI->getDebugLoc(); |
| 85 | uint64_t StackSize = MFI->getStackSize(); |
| 86 | |
| 87 | if (!StackSize) |
| 88 | return; |
| 89 | |
Reed Kotler | f99998a | 2012-10-28 06:02:37 +0000 | [diff] [blame] | 90 | if (hasFP(MF)) |
| 91 | BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP) |
| 92 | .addReg(Mips::S0); |
| 93 | |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 94 | // Adjust stack. |
Reed Kotler | cef95f7 | 2012-12-20 04:07:42 +0000 | [diff] [blame] | 95 | // assumes stacksize multiple of 8 |
| 96 | TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI); |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool Mips16FrameLowering:: |
| 100 | spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 101 | MachineBasicBlock::iterator MI, |
| 102 | const std::vector<CalleeSavedInfo> &CSI, |
| 103 | const TargetRegisterInfo *TRI) const { |
Akira Hatanaka | 0fdf3b0 | 2012-09-21 01:08:16 +0000 | [diff] [blame] | 104 | MachineFunction *MF = MBB.getParent(); |
| 105 | MachineBasicBlock *EntryBlock = MF->begin(); |
Akira Hatanaka | 0fdf3b0 | 2012-09-21 01:08:16 +0000 | [diff] [blame] | 106 | |
| 107 | // |
| 108 | // Registers RA, S0,S1 are the callee saved registers and they |
| 109 | // will be saved with the "save" instruction |
| 110 | // during emitPrologue |
| 111 | // |
| 112 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 113 | // Add the callee-saved register as live-in. Do not add if the register is |
| 114 | // RA and return address is taken, because it has already been added in |
| 115 | // method MipsTargetLowering::LowerRETURNADDR. |
| 116 | // It's killed at the spill, unless the register is RA and return address |
| 117 | // is taken. |
| 118 | unsigned Reg = CSI[i].getReg(); |
| 119 | bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA) |
| 120 | && MF->getFrameInfo()->isReturnAddressTaken(); |
| 121 | if (!IsRAAndRetAddrIsTaken) |
| 122 | EntryBlock->addLiveIn(Reg); |
| 123 | } |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 129 | MachineBasicBlock::iterator MI, |
| 130 | const std::vector<CalleeSavedInfo> &CSI, |
| 131 | const TargetRegisterInfo *TRI) const { |
| 132 | // |
| 133 | // Registers RA,S0,S1 are the callee saved registers and they will be restored |
| 134 | // with the restore instruction during emitEpilogue. |
| 135 | // We need to override this virtual function, otherwise llvm will try and |
| 136 | // restore the registers on it's on from the stack. |
| 137 | // |
| 138 | |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool |
| 143 | Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Reed Kotler | 9441125 | 2012-10-31 05:21:10 +0000 | [diff] [blame] | 144 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 145 | // Reserve call frame if the size of the maximum call frame fits into 15-bit |
| 146 | // immediate field and there are no variable sized objects on the stack. |
| 147 | return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects(); |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void Mips16FrameLowering:: |
| 151 | processFunctionBeforeCalleeSavedScan(MachineFunction &MF, |
| 152 | RegScavenger *RS) const { |
Akira Hatanaka | 0fdf3b0 | 2012-09-21 01:08:16 +0000 | [diff] [blame] | 153 | MF.getRegInfo().setPhysRegUsed(Mips::RA); |
| 154 | MF.getRegInfo().setPhysRegUsed(Mips::S0); |
| 155 | MF.getRegInfo().setPhysRegUsed(Mips::S1); |
Akira Hatanaka | cdb3ba7 | 2012-07-31 22:50:19 +0000 | [diff] [blame] | 156 | } |
Akira Hatanaka | af26626 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 157 | |
| 158 | const MipsFrameLowering * |
| 159 | llvm::createMips16FrameLowering(const MipsSubtarget &ST) { |
| 160 | return new Mips16FrameLowering(ST); |
| 161 | } |