Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1 | //=====- SystemZFrameInfo.cpp - SystemZ Frame Information ------*- C++ -*-====// |
| 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 SystemZ implementation of TargetFrameInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SystemZFrameInfo.h" |
| 15 | #include "SystemZInstrBuilder.h" |
| 16 | #include "SystemZInstrInfo.h" |
| 17 | #include "SystemZMachineFunctionInfo.h" |
| 18 | #include "llvm/Function.h" |
| 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" |
| 24 | #include "llvm/Target/TargetData.h" |
| 25 | #include "llvm/Target/TargetOptions.h" |
| 26 | #include "llvm/Support/CommandLine.h" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | /// emitSPUpdate - Emit a series of instructions to increment / decrement the |
| 31 | /// stack pointer by a constant value. |
| 32 | static |
| 33 | void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, |
| 34 | int64_t NumBytes, const TargetInstrInfo &TII) { |
| 35 | unsigned Opc; uint64_t Chunk; |
| 36 | bool isSub = NumBytes < 0; |
| 37 | uint64_t Offset = isSub ? -NumBytes : NumBytes; |
| 38 | |
| 39 | if (Offset >= (1LL << 15) - 1) { |
| 40 | Opc = SystemZ::ADD64ri32; |
| 41 | Chunk = (1LL << 31) - 1; |
| 42 | } else { |
| 43 | Opc = SystemZ::ADD64ri16; |
| 44 | Chunk = (1LL << 15) - 1; |
| 45 | } |
| 46 | |
| 47 | DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 48 | |
| 49 | while (Offset) { |
| 50 | uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset; |
| 51 | MachineInstr *MI = |
| 52 | BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D) |
| 53 | .addReg(SystemZ::R15D).addImm(isSub ? -ThisVal : ThisVal); |
| 54 | // The PSW implicit def is dead. |
| 55 | MI->getOperand(3).setIsDead(); |
| 56 | Offset -= ThisVal; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void SystemZFrameInfo::emitPrologue(MachineFunction &MF) const { |
| 61 | MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB |
| 62 | const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo(); |
| 63 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 64 | const SystemZRegisterInfo *RegInfo = |
| 65 | static_cast<const SystemZRegisterInfo*>(MF.getTarget().getRegisterInfo()); |
| 66 | const SystemZInstrInfo &TII = |
| 67 | *static_cast<const SystemZInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 68 | SystemZMachineFunctionInfo *SystemZMFI = |
| 69 | MF.getInfo<SystemZMachineFunctionInfo>(); |
| 70 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 71 | DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 72 | |
| 73 | // Get the number of bytes to allocate from the FrameInfo. |
| 74 | // Note that area for callee-saved stuff is already allocated, thus we need to |
| 75 | // 'undo' the stack movement. |
| 76 | uint64_t StackSize = MFI->getStackSize(); |
| 77 | StackSize -= SystemZMFI->getCalleeSavedFrameSize(); |
| 78 | |
| 79 | uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea(); |
| 80 | |
| 81 | // Skip the callee-saved push instructions. |
| 82 | while (MBBI != MBB.end() && |
| 83 | (MBBI->getOpcode() == SystemZ::MOV64mr || |
| 84 | MBBI->getOpcode() == SystemZ::MOV64mrm)) |
| 85 | ++MBBI; |
| 86 | |
| 87 | if (MBBI != MBB.end()) |
| 88 | DL = MBBI->getDebugLoc(); |
| 89 | |
| 90 | // adjust stack pointer: R15 -= numbytes |
| 91 | if (StackSize || MFI->hasCalls()) { |
| 92 | assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) && |
| 93 | "Invalid stack frame calculation!"); |
| 94 | emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII); |
| 95 | } |
| 96 | |
| 97 | if (RegInfo->hasFP(MF)) { |
| 98 | // Update R11 with the new base value... |
| 99 | BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D) |
| 100 | .addReg(SystemZ::R15D); |
| 101 | |
| 102 | // Mark the FramePtr as live-in in every block except the entry. |
| 103 | for (MachineFunction::iterator I = llvm::next(MF.begin()), E = MF.end(); |
| 104 | I != E; ++I) |
| 105 | I->addLiveIn(SystemZ::R11D); |
| 106 | |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void SystemZFrameInfo::emitEpilogue(MachineFunction &MF, |
| 111 | MachineBasicBlock &MBB) const { |
| 112 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 113 | const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo(); |
| 114 | MachineBasicBlock::iterator MBBI = prior(MBB.end()); |
| 115 | const SystemZInstrInfo &TII = |
| 116 | *static_cast<const SystemZInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 117 | SystemZMachineFunctionInfo *SystemZMFI = |
| 118 | MF.getInfo<SystemZMachineFunctionInfo>(); |
| 119 | unsigned RetOpcode = MBBI->getOpcode(); |
| 120 | |
| 121 | switch (RetOpcode) { |
| 122 | case SystemZ::RET: break; // These are ok |
| 123 | default: |
| 124 | assert(0 && "Can only insert epilog into returning blocks"); |
| 125 | } |
| 126 | |
| 127 | // Get the number of bytes to allocate from the FrameInfo |
| 128 | // Note that area for callee-saved stuff is already allocated, thus we need to |
| 129 | // 'undo' the stack movement. |
| 130 | uint64_t StackSize = |
| 131 | MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize(); |
| 132 | uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea(); |
| 133 | |
| 134 | // Skip the final terminator instruction. |
| 135 | while (MBBI != MBB.begin()) { |
| 136 | MachineBasicBlock::iterator PI = prior(MBBI); |
| 137 | --MBBI; |
| 138 | if (!PI->getDesc().isTerminator()) |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | // During callee-saved restores emission stack frame was not yet finialized |
| 143 | // (and thus - the stack size was unknown). Tune the offset having full stack |
| 144 | // size in hands. |
| 145 | if (StackSize || MFI->hasCalls()) { |
| 146 | assert((MBBI->getOpcode() == SystemZ::MOV64rmm || |
| 147 | MBBI->getOpcode() == SystemZ::MOV64rm) && |
| 148 | "Expected to see callee-save register restore code"); |
| 149 | assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) && |
| 150 | "Invalid stack frame calculation!"); |
| 151 | |
| 152 | unsigned i = 0; |
| 153 | MachineInstr &MI = *MBBI; |
| 154 | while (!MI.getOperand(i).isImm()) { |
| 155 | ++i; |
| 156 | assert(i < MI.getNumOperands() && "Unexpected restore code!"); |
| 157 | } |
| 158 | |
| 159 | uint64_t Offset = NumBytes + MI.getOperand(i).getImm(); |
| 160 | // If Offset does not fit into 20-bit signed displacement field we need to |
| 161 | // emit some additional code... |
| 162 | if (Offset > 524287) { |
| 163 | // Fold the displacement into load instruction as much as possible. |
| 164 | NumBytes = Offset - 524287; |
| 165 | Offset = 524287; |
| 166 | emitSPUpdate(MBB, MBBI, NumBytes, TII); |
| 167 | } |
| 168 | |
| 169 | MI.getOperand(i).ChangeToImmediate(Offset); |
| 170 | } |
| 171 | } |