Jia Liu | c570711 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===// |
Anton Korobeynikov | 3346491 | 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 | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 9 | // |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 10 | // This file contains the Mips implementation of TargetFrameLowering class. |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 11 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 13 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 14 | #include "MipsFrameLowering.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame^] | 15 | #include "MCTargetDesc/MipsBaseInfo.h" |
Craig Topper | 79aa341 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 16 | #include "MipsAnalyzeImmediate.h" |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 17 | #include "MipsInstrInfo.h" |
| 18 | #include "MipsMachineFunction.h" |
Akira Hatanaka | af26626 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 19 | #include "MipsTargetMachine.h" |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +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" |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 25 | #include "llvm/DataLayout.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame^] | 26 | #include "llvm/Function.h" |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame^] | 28 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 34 | // |
| 35 | // Stack Frame Processing methods |
| 36 | // +----------------------------+ |
| 37 | // |
| 38 | // The stack is allocated decrementing the stack pointer on |
| 39 | // the first instruction of a function prologue. Once decremented, |
| 40 | // all stack references are done thought a positive offset |
| 41 | // from the stack/frame pointer, so the stack is considering |
| 42 | // to grow up! Otherwise terrible hacks would have to be made |
| 43 | // to get this stack ABI compliant :) |
| 44 | // |
| 45 | // The stack frame required by the ABI (after call): |
| 46 | // Offset |
| 47 | // |
| 48 | // 0 ---------- |
| 49 | // 4 Args to pass |
| 50 | // . saved $GP (used in PIC) |
| 51 | // . Alloca allocations |
| 52 | // . Local Area |
| 53 | // . CPU "Callee Saved" Registers |
| 54 | // . saved FP |
| 55 | // . saved RA |
| 56 | // . FPU "Callee Saved" Registers |
| 57 | // StackSize ----------- |
| 58 | // |
| 59 | // Offset - offset from sp after stack allocation on function prologue |
| 60 | // |
| 61 | // The sp is the stack pointer subtracted/added from the stack size |
| 62 | // at the Prologue/Epilogue |
| 63 | // |
| 64 | // References to the previous stack (to obtain arguments) are done |
| 65 | // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1)) |
| 66 | // |
| 67 | // Examples: |
| 68 | // - reference to the actual stack frame |
| 69 | // for any local area var there is smt like : FI >= 0, StackOffset: 4 |
| 70 | // sw REGX, 4(SP) |
| 71 | // |
| 72 | // - reference to previous stack frame |
| 73 | // suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16. |
| 74 | // The emitted instruction will be something like: |
| 75 | // lw REGX, 16+StackSize(SP) |
| 76 | // |
| 77 | // Since the total stack size is unknown on LowerFormalArguments, all |
| 78 | // stack references (ObjectOffset) created to reference the function |
| 79 | // arguments, are negative numbers. This way, on eliminateFrameIndex it's |
| 80 | // possible to detect those references and the offsets are adjusted to |
| 81 | // their real location. |
| 82 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 83 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 84 | |
Akira Hatanaka | af26626 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 85 | const MipsFrameLowering *MipsFrameLowering::create(MipsTargetMachine &TM, |
| 86 | const MipsSubtarget &ST) { |
| 87 | if (TM.getSubtargetImpl()->inMips16Mode()) |
| 88 | return llvm::createMips16FrameLowering(ST); |
| 89 | |
| 90 | return llvm::createMipsSEFrameLowering(ST); |
| 91 | } |
| 92 | |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 93 | // hasFP - Return true if the specified function should have a dedicated frame |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 94 | // pointer register. This is true if the function has variable sized allocas or |
| 95 | // if frame pointer elimination is disabled. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 96 | bool MipsFrameLowering::hasFP(const MachineFunction &MF) const { |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 97 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 98 | return MF.getTarget().Options.DisableFramePointerElim(MF) || |
| 99 | MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken(); |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 100 | } |
Akira Hatanaka | 265f191 | 2012-11-02 21:10:22 +0000 | [diff] [blame] | 101 | |
| 102 | uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const { |
| 103 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 104 | const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo(); |
| 105 | |
| 106 | int64_t Offset = 0; |
| 107 | |
| 108 | // Iterate over fixed sized objects. |
| 109 | for (int I = MFI->getObjectIndexBegin(); I != 0; ++I) |
| 110 | Offset = std::max(Offset, -MFI->getObjectOffset(I)); |
| 111 | |
| 112 | // Conservatively assume all callee-saved registers will be saved. |
| 113 | for (const uint16_t *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) { |
| 114 | unsigned Size = TRI.getMinimalPhysRegClass(*R)->getSize(); |
| 115 | Offset = RoundUpToAlignment(Offset + Size, Size); |
| 116 | } |
| 117 | |
| 118 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 119 | |
| 120 | // Check that MaxAlign is not zero if there is a stack object that is not a |
| 121 | // callee-saved spill. |
| 122 | assert(!MFI->getObjectIndexEnd() || MaxAlign); |
| 123 | |
| 124 | // Iterate over other objects. |
| 125 | for (unsigned I = 0, E = MFI->getObjectIndexEnd(); I != E; ++I) |
| 126 | Offset = RoundUpToAlignment(Offset + MFI->getObjectSize(I), MaxAlign); |
| 127 | |
| 128 | // Call frame. |
| 129 | if (MFI->adjustsStack() && hasReservedCallFrame(MF)) |
| 130 | Offset = RoundUpToAlignment(Offset + MFI->getMaxCallFrameSize(), |
| 131 | std::max(MaxAlign, getStackAlignment())); |
| 132 | |
| 133 | return RoundUpToAlignment(Offset, getStackAlignment()); |
| 134 | } |