Jia Liu | 9f61011 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===// |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +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 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 9 | // |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 10 | // This file contains the Mips implementation of TargetFrameLowering class. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 11 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 13 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 14 | #include "MipsFrameLowering.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/MipsBaseInfo.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 16 | #include "MipsInstrInfo.h" |
| 17 | #include "MipsMachineFunction.h" |
Akira Hatanaka | fab8929 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 18 | #include "MipsTargetMachine.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/Function.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 32 | // |
| 33 | // Stack Frame Processing methods |
| 34 | // +----------------------------+ |
| 35 | // |
| 36 | // The stack is allocated decrementing the stack pointer on |
| 37 | // the first instruction of a function prologue. Once decremented, |
| 38 | // all stack references are done thought a positive offset |
| 39 | // from the stack/frame pointer, so the stack is considering |
| 40 | // to grow up! Otherwise terrible hacks would have to be made |
| 41 | // to get this stack ABI compliant :) |
| 42 | // |
| 43 | // The stack frame required by the ABI (after call): |
| 44 | // Offset |
| 45 | // |
| 46 | // 0 ---------- |
| 47 | // 4 Args to pass |
| 48 | // . saved $GP (used in PIC) |
| 49 | // . Alloca allocations |
| 50 | // . Local Area |
| 51 | // . CPU "Callee Saved" Registers |
| 52 | // . saved FP |
| 53 | // . saved RA |
| 54 | // . FPU "Callee Saved" Registers |
| 55 | // StackSize ----------- |
| 56 | // |
| 57 | // Offset - offset from sp after stack allocation on function prologue |
| 58 | // |
| 59 | // The sp is the stack pointer subtracted/added from the stack size |
| 60 | // at the Prologue/Epilogue |
| 61 | // |
| 62 | // References to the previous stack (to obtain arguments) are done |
| 63 | // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1)) |
| 64 | // |
| 65 | // Examples: |
| 66 | // - reference to the actual stack frame |
| 67 | // for any local area var there is smt like : FI >= 0, StackOffset: 4 |
| 68 | // sw REGX, 4(SP) |
| 69 | // |
| 70 | // - reference to previous stack frame |
| 71 | // suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16. |
| 72 | // The emitted instruction will be something like: |
| 73 | // lw REGX, 16+StackSize(SP) |
| 74 | // |
| 75 | // Since the total stack size is unknown on LowerFormalArguments, all |
| 76 | // stack references (ObjectOffset) created to reference the function |
| 77 | // arguments, are negative numbers. This way, on eliminateFrameIndex it's |
| 78 | // possible to detect those references and the offsets are adjusted to |
| 79 | // their real location. |
| 80 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 81 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 82 | |
Eric Christopher | e54f10e | 2014-07-18 23:33:47 +0000 | [diff] [blame] | 83 | const MipsFrameLowering *MipsFrameLowering::create(const MipsSubtarget &ST) { |
| 84 | if (ST.inMips16Mode()) |
Akira Hatanaka | fab8929 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 85 | return llvm::createMips16FrameLowering(ST); |
| 86 | |
| 87 | return llvm::createMipsSEFrameLowering(ST); |
| 88 | } |
| 89 | |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 90 | // hasFP - Return true if the specified function should have a dedicated frame |
Vasileios Kalintiris | bb698c7 | 2015-06-02 13:14:46 +0000 | [diff] [blame] | 91 | // pointer register. This is true if the function has variable sized allocas, |
| 92 | // if it needs dynamic stack realignment, if frame pointer elimination is |
| 93 | // disabled, or if the frame address is taken. |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 94 | bool MipsFrameLowering::hasFP(const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 95 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Vasileios Kalintiris | bb698c7 | 2015-06-02 13:14:46 +0000 | [diff] [blame] | 96 | const TargetRegisterInfo *TRI = STI.getRegisterInfo(); |
| 97 | |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 98 | return MF.getTarget().Options.DisableFramePointerElim(MF) || |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 99 | MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() || |
Vasileios Kalintiris | bb698c7 | 2015-06-02 13:14:46 +0000 | [diff] [blame] | 100 | TRI->needsStackRealignment(MF); |
| 101 | } |
| 102 | |
| 103 | bool MipsFrameLowering::hasBP(const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 104 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Vasileios Kalintiris | bb698c7 | 2015-06-02 13:14:46 +0000 | [diff] [blame] | 105 | const TargetRegisterInfo *TRI = STI.getRegisterInfo(); |
| 106 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 107 | return MFI.hasVarSizedObjects() && TRI->needsStackRealignment(MF); |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 108 | } |
Akira Hatanaka | 97b43d8b | 2012-11-02 21:10:22 +0000 | [diff] [blame] | 109 | |
Simon Dardis | 725acb2 | 2017-11-02 12:47:22 +0000 | [diff] [blame] | 110 | // Estimate the size of the stack, including the incoming arguments. We need to |
| 111 | // account for register spills, local objects, reserved call frame and incoming |
| 112 | // arguments. This is required to determine the largest possible positive offset |
| 113 | // from $sp so that it can be determined if an emergency spill slot for stack |
| 114 | // addresses is required. |
Akira Hatanaka | 97b43d8b | 2012-11-02 21:10:22 +0000 | [diff] [blame] | 115 | uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 116 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 117 | const TargetRegisterInfo &TRI = *STI.getRegisterInfo(); |
Akira Hatanaka | 97b43d8b | 2012-11-02 21:10:22 +0000 | [diff] [blame] | 118 | |
Simon Dardis | 725acb2 | 2017-11-02 12:47:22 +0000 | [diff] [blame] | 119 | int64_t Size = 0; |
Akira Hatanaka | 97b43d8b | 2012-11-02 21:10:22 +0000 | [diff] [blame] | 120 | |
Simon Dardis | 725acb2 | 2017-11-02 12:47:22 +0000 | [diff] [blame] | 121 | // Iterate over fixed sized objects which are incoming arguments. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 122 | for (int I = MFI.getObjectIndexBegin(); I != 0; ++I) |
Simon Dardis | 725acb2 | 2017-11-02 12:47:22 +0000 | [diff] [blame] | 123 | if (MFI.getObjectOffset(I) > 0) |
| 124 | Size += MFI.getObjectSize(I); |
Akira Hatanaka | 97b43d8b | 2012-11-02 21:10:22 +0000 | [diff] [blame] | 125 | |
| 126 | // Conservatively assume all callee-saved registers will be saved. |
Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 127 | for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) { |
Simon Dardis | 725acb2 | 2017-11-02 12:47:22 +0000 | [diff] [blame] | 128 | unsigned RegSize = TRI.getSpillSize(*TRI.getMinimalPhysRegClass(*R)); |
| 129 | Size = alignTo(Size + RegSize, RegSize); |
Akira Hatanaka | 97b43d8b | 2012-11-02 21:10:22 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Simon Dardis | 725acb2 | 2017-11-02 12:47:22 +0000 | [diff] [blame] | 132 | // Get the size of the rest of the frame objects and any possible reserved |
| 133 | // call frame, accounting for alignment. |
| 134 | return Size + MFI.estimateStackSize(MF); |
Akira Hatanaka | 97b43d8b | 2012-11-02 21:10:22 +0000 | [diff] [blame] | 135 | } |
Vasileios Kalintiris | 580f067 | 2015-04-02 11:09:40 +0000 | [diff] [blame] | 136 | |
| 137 | // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 138 | MachineBasicBlock::iterator MipsFrameLowering:: |
Vasileios Kalintiris | 580f067 | 2015-04-02 11:09:40 +0000 | [diff] [blame] | 139 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 140 | MachineBasicBlock::iterator I) const { |
| 141 | unsigned SP = STI.getABI().IsN64() ? Mips::SP_64 : Mips::SP; |
| 142 | |
| 143 | if (!hasReservedCallFrame(MF)) { |
| 144 | int64_t Amount = I->getOperand(0).getImm(); |
| 145 | if (I->getOpcode() == Mips::ADJCALLSTACKDOWN) |
| 146 | Amount = -Amount; |
| 147 | |
| 148 | STI.getInstrInfo()->adjustStackPtr(SP, Amount, MBB, I); |
| 149 | } |
| 150 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 151 | return MBB.erase(I); |
Vasileios Kalintiris | 580f067 | 2015-04-02 11:09:40 +0000 | [diff] [blame] | 152 | } |