Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame^] | 1 | //=====- AlphaFrameInfo.cpp - Alpha 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 Alpha implementation of TargetFrameInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "AlphaFrameInfo.h" |
| 15 | #include "AlphaInstrInfo.h" |
| 16 | #include "AlphaMachineFunctionInfo.h" |
| 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/ADT/Twine.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | static long getUpper16(long l) { |
| 26 | long y = l / Alpha::IMM_MULT; |
| 27 | if (l % Alpha::IMM_MULT > Alpha::IMM_HIGH) |
| 28 | ++y; |
| 29 | return y; |
| 30 | } |
| 31 | |
| 32 | static long getLower16(long l) { |
| 33 | long h = getUpper16(l); |
| 34 | return l - h * Alpha::IMM_MULT; |
| 35 | } |
| 36 | |
| 37 | void AlphaFrameInfo::emitPrologue(MachineFunction &MF) const { |
| 38 | MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB |
| 39 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 40 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 41 | const AlphaRegisterInfo *RegInfo = |
| 42 | static_cast<const AlphaRegisterInfo*>(MF.getTarget().getRegisterInfo()); |
| 43 | const AlphaInstrInfo &TII = |
| 44 | *static_cast<const AlphaInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 45 | |
| 46 | DebugLoc dl = (MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc()); |
| 47 | bool FP = RegInfo->hasFP(MF); |
| 48 | |
| 49 | // Handle GOP offset |
| 50 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAHg), Alpha::R29) |
| 51 | .addGlobalAddress(MF.getFunction()).addReg(Alpha::R27).addImm(++curgpdist); |
| 52 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAg), Alpha::R29) |
| 53 | .addGlobalAddress(MF.getFunction()).addReg(Alpha::R29).addImm(curgpdist); |
| 54 | |
| 55 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::ALTENT)) |
| 56 | .addGlobalAddress(MF.getFunction()); |
| 57 | |
| 58 | // Get the number of bytes to allocate from the FrameInfo |
| 59 | long NumBytes = MFI->getStackSize(); |
| 60 | |
| 61 | if (FP) |
| 62 | NumBytes += 8; //reserve space for the old FP |
| 63 | |
| 64 | // Do we need to allocate space on the stack? |
| 65 | if (NumBytes == 0) return; |
| 66 | |
| 67 | unsigned Align = getStackAlignment(); |
| 68 | NumBytes = (NumBytes+Align-1)/Align*Align; |
| 69 | |
| 70 | // Update frame info to pretend that this is part of the stack... |
| 71 | MFI->setStackSize(NumBytes); |
| 72 | |
| 73 | // adjust stack pointer: r30 -= numbytes |
| 74 | NumBytes = -NumBytes; |
| 75 | if (NumBytes >= Alpha::IMM_LOW) { |
| 76 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes) |
| 77 | .addReg(Alpha::R30); |
| 78 | } else if (getUpper16(NumBytes) >= Alpha::IMM_LOW) { |
| 79 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAH), Alpha::R30) |
| 80 | .addImm(getUpper16(NumBytes)).addReg(Alpha::R30); |
| 81 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30) |
| 82 | .addImm(getLower16(NumBytes)).addReg(Alpha::R30); |
| 83 | } else { |
| 84 | report_fatal_error("Too big a stack frame at " + Twine(NumBytes)); |
| 85 | } |
| 86 | |
| 87 | // Now if we need to, save the old FP and set the new |
| 88 | if (FP) { |
| 89 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::STQ)) |
| 90 | .addReg(Alpha::R15).addImm(0).addReg(Alpha::R30); |
| 91 | // This must be the last instr in the prolog |
| 92 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::BISr), Alpha::R15) |
| 93 | .addReg(Alpha::R30).addReg(Alpha::R30); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | void AlphaFrameInfo::emitEpilogue(MachineFunction &MF, |
| 99 | MachineBasicBlock &MBB) const { |
| 100 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 101 | MachineBasicBlock::iterator MBBI = prior(MBB.end()); |
| 102 | const AlphaRegisterInfo *RegInfo = |
| 103 | static_cast<const AlphaRegisterInfo*>(MF.getTarget().getRegisterInfo()); |
| 104 | const AlphaInstrInfo &TII = |
| 105 | *static_cast<const AlphaInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 106 | |
| 107 | assert((MBBI->getOpcode() == Alpha::RETDAG || |
| 108 | MBBI->getOpcode() == Alpha::RETDAGp) |
| 109 | && "Can only insert epilog into returning blocks"); |
| 110 | DebugLoc dl = MBBI->getDebugLoc(); |
| 111 | |
| 112 | bool FP = RegInfo->hasFP(MF); |
| 113 | |
| 114 | // Get the number of bytes allocated from the FrameInfo... |
| 115 | long NumBytes = MFI->getStackSize(); |
| 116 | |
| 117 | //now if we need to, restore the old FP |
| 118 | if (FP) { |
| 119 | //copy the FP into the SP (discards allocas) |
| 120 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::BISr), Alpha::R30).addReg(Alpha::R15) |
| 121 | .addReg(Alpha::R15); |
| 122 | //restore the FP |
| 123 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDQ), Alpha::R15) |
| 124 | .addImm(0).addReg(Alpha::R15); |
| 125 | } |
| 126 | |
| 127 | if (NumBytes != 0) { |
| 128 | if (NumBytes <= Alpha::IMM_HIGH) { |
| 129 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes) |
| 130 | .addReg(Alpha::R30); |
| 131 | } else if (getUpper16(NumBytes) <= Alpha::IMM_HIGH) { |
| 132 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAH), Alpha::R30) |
| 133 | .addImm(getUpper16(NumBytes)).addReg(Alpha::R30); |
| 134 | BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30) |
| 135 | .addImm(getLower16(NumBytes)).addReg(Alpha::R30); |
| 136 | } else { |
| 137 | report_fatal_error("Too big a stack frame at " + Twine(NumBytes)); |
| 138 | } |
| 139 | } |
| 140 | } |