Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 1 | //===- IA64RegisterInfo.cpp - IA64 Register Information ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Duraid Madina and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the IA64 implementation of the MRegisterInfo class. This |
| 11 | // file is responsible for the frame pointer elimination optimization on IA64. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "IA64.h" |
| 16 | #include "IA64RegisterInfo.h" |
| 17 | #include "IA64InstrBuilder.h" |
| 18 | #include "IA64MachineFunctionInfo.h" |
| 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/Type.h" |
| 21 | #include "llvm/CodeGen/ValueTypes.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Jim Laskey | f1d78e8 | 2006-03-23 18:12:57 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineLocation.h" |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetFrameInfo.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetInstrInfo.h" |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/ADT/STLExtras.h" |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 34 | IA64RegisterInfo::IA64RegisterInfo(const TargetInstrInfo &tii) |
| 35 | : IA64GenRegisterInfo(IA64::ADJUSTCALLSTACKDOWN, IA64::ADJUSTCALLSTACKUP), |
| 36 | TII(tii) {} |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 37 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 38 | void IA64RegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
Chris Lattner | 0ffb1a5 | 2005-09-30 01:30:55 +0000 | [diff] [blame] | 39 | MachineBasicBlock::iterator MI, |
| 40 | unsigned SrcReg, int FrameIdx, |
| 41 | const TargetRegisterClass *RC) const{ |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 42 | |
Chris Lattner | a411bef | 2005-10-28 04:57:11 +0000 | [diff] [blame] | 43 | if (RC == IA64::FPRegisterClass) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 44 | BuildMI(MBB, MI, TII.get(IA64::STF_SPILL)).addFrameIndex(FrameIdx).addReg(SrcReg); |
Chris Lattner | a411bef | 2005-10-28 04:57:11 +0000 | [diff] [blame] | 45 | } else if (RC == IA64::GRRegisterClass) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 46 | BuildMI(MBB, MI, TII.get(IA64::ST8)).addFrameIndex(FrameIdx).addReg(SrcReg); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 47 | } |
Chris Lattner | a411bef | 2005-10-28 04:57:11 +0000 | [diff] [blame] | 48 | else if (RC == IA64::PRRegisterClass) { |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 49 | /* we use IA64::r2 as a temporary register for doing this hackery. */ |
| 50 | // first we load 0: |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 51 | BuildMI(MBB, MI, TII.get(IA64::MOV), IA64::r2).addReg(IA64::r0); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 52 | // then conditionally add 1: |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 53 | BuildMI(MBB, MI, TII.get(IA64::CADDIMM22), IA64::r2).addReg(IA64::r2) |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 54 | .addImm(1).addReg(SrcReg); |
| 55 | // and then store it to the stack |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 56 | BuildMI(MBB, MI, TII.get(IA64::ST8)).addFrameIndex(FrameIdx).addReg(IA64::r2); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 57 | } else assert(0 && |
| 58 | "sorry, I don't know how to store this sort of reg in the stack\n"); |
| 59 | } |
| 60 | |
| 61 | void IA64RegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Chris Lattner | 0ffb1a5 | 2005-09-30 01:30:55 +0000 | [diff] [blame] | 62 | MachineBasicBlock::iterator MI, |
| 63 | unsigned DestReg, int FrameIdx, |
| 64 | const TargetRegisterClass *RC)const{ |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 65 | |
Chris Lattner | a411bef | 2005-10-28 04:57:11 +0000 | [diff] [blame] | 66 | if (RC == IA64::FPRegisterClass) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 67 | BuildMI(MBB, MI, TII.get(IA64::LDF_FILL), DestReg).addFrameIndex(FrameIdx); |
Chris Lattner | a411bef | 2005-10-28 04:57:11 +0000 | [diff] [blame] | 68 | } else if (RC == IA64::GRRegisterClass) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 69 | BuildMI(MBB, MI, TII.get(IA64::LD8), DestReg).addFrameIndex(FrameIdx); |
Chris Lattner | a411bef | 2005-10-28 04:57:11 +0000 | [diff] [blame] | 70 | } else if (RC == IA64::PRRegisterClass) { |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 71 | // first we load a byte from the stack into r2, our 'predicate hackery' |
| 72 | // scratch reg |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 73 | BuildMI(MBB, MI, TII.get(IA64::LD8), IA64::r2).addFrameIndex(FrameIdx); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 74 | // then we compare it to zero. If it _is_ zero, compare-not-equal to |
| 75 | // r0 gives us 0, which is what we want, so that's nice. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 76 | BuildMI(MBB, MI, TII.get(IA64::CMPNE), DestReg).addReg(IA64::r2).addReg(IA64::r0); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 77 | } else assert(0 && |
| 78 | "sorry, I don't know how to load this sort of reg from the stack\n"); |
| 79 | } |
| 80 | |
| 81 | void IA64RegisterInfo::copyRegToReg(MachineBasicBlock &MBB, |
| 82 | MachineBasicBlock::iterator MI, |
| 83 | unsigned DestReg, unsigned SrcReg, |
| 84 | const TargetRegisterClass *RC) const { |
| 85 | |
| 86 | if(RC == IA64::PRRegisterClass ) // if a bool, we use pseudocode |
| 87 | // (SrcReg) DestReg = cmp.eq.unc(r0, r0) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 88 | BuildMI(MBB, MI, TII.get(IA64::PCMPEQUNC), DestReg) |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 89 | .addReg(IA64::r0).addReg(IA64::r0).addReg(SrcReg); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 90 | else // otherwise, MOV works (for both gen. regs and FP regs) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 91 | BuildMI(MBB, MI, TII.get(IA64::MOV), DestReg).addReg(SrcReg); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Evan Cheng | c2b861d | 2007-01-02 21:33:40 +0000 | [diff] [blame] | 94 | const unsigned* IA64RegisterInfo::getCalleeSavedRegs() const { |
| 95 | static const unsigned CalleeSavedRegs[] = { |
Evan Cheng | 0f3ac8d | 2006-05-18 00:12:58 +0000 | [diff] [blame] | 96 | IA64::r5, 0 |
| 97 | }; |
Evan Cheng | c2b861d | 2007-01-02 21:33:40 +0000 | [diff] [blame] | 98 | return CalleeSavedRegs; |
Evan Cheng | 0f3ac8d | 2006-05-18 00:12:58 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | const TargetRegisterClass* const* |
Evan Cheng | c2b861d | 2007-01-02 21:33:40 +0000 | [diff] [blame] | 102 | IA64RegisterInfo::getCalleeSavedRegClasses() const { |
| 103 | static const TargetRegisterClass * const CalleeSavedRegClasses[] = { |
Evan Cheng | 0f3ac8d | 2006-05-18 00:12:58 +0000 | [diff] [blame] | 104 | &IA64::GRRegClass, 0 |
| 105 | }; |
Evan Cheng | c2b861d | 2007-01-02 21:33:40 +0000 | [diff] [blame] | 106 | return CalleeSavedRegClasses; |
Evan Cheng | 0f3ac8d | 2006-05-18 00:12:58 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 109 | //===----------------------------------------------------------------------===// |
| 110 | // Stack Frame Processing methods |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | |
| 113 | // hasFP - Return true if the specified function should have a dedicated frame |
| 114 | // pointer register. This is true if the function has variable sized allocas or |
| 115 | // if frame pointer elimination is disabled. |
| 116 | // |
Evan Cheng | dc77540 | 2007-01-23 00:57:47 +0000 | [diff] [blame^] | 117 | bool IA64RegisterInfo::hasFP(const MachineFunction &MF) const { |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 118 | return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects(); |
| 119 | } |
| 120 | |
| 121 | void IA64RegisterInfo:: |
| 122 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 123 | MachineBasicBlock::iterator I) const { |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 124 | if (hasFP(MF)) { |
| 125 | // If we have a frame pointer, turn the adjcallstackup instruction into a |
| 126 | // 'sub SP, <amt>' and the adjcallstackdown instruction into 'add SP, |
| 127 | // <amt>' |
| 128 | MachineInstr *Old = I; |
| 129 | unsigned Amount = Old->getOperand(0).getImmedValue(); |
| 130 | if (Amount != 0) { |
| 131 | // We need to keep the stack aligned properly. To do this, we round the |
| 132 | // amount of space needed for the outgoing arguments up to the next |
| 133 | // alignment boundary. |
| 134 | unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 135 | Amount = (Amount+Align-1)/Align*Align; |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 136 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 137 | MachineInstr *New; |
| 138 | if (Old->getOpcode() == IA64::ADJUSTCALLSTACKDOWN) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 139 | New=BuildMI(TII.get(IA64::ADDIMM22), IA64::r12).addReg(IA64::r12) |
Chris Lattner | 63b3d71 | 2006-05-04 17:21:20 +0000 | [diff] [blame] | 140 | .addImm(-Amount); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 141 | } else { |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 142 | assert(Old->getOpcode() == IA64::ADJUSTCALLSTACKUP); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 143 | New=BuildMI(TII.get(IA64::ADDIMM22), IA64::r12).addReg(IA64::r12) |
Chris Lattner | 63b3d71 | 2006-05-04 17:21:20 +0000 | [diff] [blame] | 144 | .addImm(Amount); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Replace the pseudo instruction with a new instruction... |
| 148 | MBB.insert(I, New); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | MBB.erase(I); |
| 153 | } |
| 154 | |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 155 | void IA64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II)const{ |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 156 | unsigned i = 0; |
| 157 | MachineInstr &MI = *II; |
| 158 | MachineBasicBlock &MBB = *MI.getParent(); |
| 159 | MachineFunction &MF = *MBB.getParent(); |
| 160 | |
| 161 | bool FP = hasFP(MF); |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 162 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 163 | while (!MI.getOperand(i).isFrameIndex()) { |
| 164 | ++i; |
| 165 | assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); |
| 166 | } |
| 167 | |
| 168 | int FrameIndex = MI.getOperand(i).getFrameIndex(); |
| 169 | |
| 170 | // choose a base register: ( hasFP? framepointer : stack pointer ) |
Duraid Madina | b9bcd18 | 2006-01-23 06:08:46 +0000 | [diff] [blame] | 171 | unsigned BaseRegister = FP ? IA64::r5 : IA64::r12; |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 172 | // Add the base register |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 173 | MI.getOperand(i).ChangeToRegister(BaseRegister, false); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 174 | |
| 175 | // Now add the frame object offset to the offset from r1. |
| 176 | int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex); |
| 177 | |
| 178 | // If we're not using a Frame Pointer that has been set to the value of the |
| 179 | // SP before having the stack size subtracted from it, then add the stack size |
| 180 | // to Offset to get the correct offset. |
| 181 | Offset += MF.getFrameInfo()->getStackSize(); |
| 182 | |
| 183 | // XXX: we use 'r22' as another hack+slash temporary register here :( |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 184 | if (Offset <= 8191 && Offset >= -8192) { // smallish offset |
| 185 | // Fix up the old: |
| 186 | MI.getOperand(i).ChangeToRegister(IA64::r22, false); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 187 | //insert the new |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 188 | MachineInstr* nMI=BuildMI(TII.get(IA64::ADDIMM22), IA64::r22) |
Chris Lattner | 63b3d71 | 2006-05-04 17:21:20 +0000 | [diff] [blame] | 189 | .addReg(BaseRegister).addImm(Offset); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 190 | MBB.insert(II, nMI); |
| 191 | } else { // it's big |
| 192 | //fix up the old: |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 193 | MI.getOperand(i).ChangeToRegister(IA64::r22, false); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 194 | MachineInstr* nMI; |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 195 | nMI=BuildMI(TII.get(IA64::MOVLIMM64), IA64::r22).addImm(Offset); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 196 | MBB.insert(II, nMI); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 197 | nMI=BuildMI(TII.get(IA64::ADD), IA64::r22).addReg(BaseRegister) |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 198 | .addReg(IA64::r22); |
| 199 | MBB.insert(II, nMI); |
| 200 | } |
| 201 | |
| 202 | } |
| 203 | |
| 204 | void IA64RegisterInfo::emitPrologue(MachineFunction &MF) const { |
| 205 | MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB |
| 206 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 207 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 208 | MachineInstr *MI; |
| 209 | bool FP = hasFP(MF); |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 210 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 211 | // first, we handle the 'alloc' instruction, that should be right up the |
| 212 | // top of any function |
| 213 | static const unsigned RegsInOrder[96] = { // there are 96 GPRs the |
| 214 | // RSE worries about |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 215 | IA64::r32, IA64::r33, IA64::r34, IA64::r35, |
| 216 | IA64::r36, IA64::r37, IA64::r38, IA64::r39, IA64::r40, IA64::r41, |
| 217 | IA64::r42, IA64::r43, IA64::r44, IA64::r45, IA64::r46, IA64::r47, |
| 218 | IA64::r48, IA64::r49, IA64::r50, IA64::r51, IA64::r52, IA64::r53, |
| 219 | IA64::r54, IA64::r55, IA64::r56, IA64::r57, IA64::r58, IA64::r59, |
| 220 | IA64::r60, IA64::r61, IA64::r62, IA64::r63, IA64::r64, IA64::r65, |
| 221 | IA64::r66, IA64::r67, IA64::r68, IA64::r69, IA64::r70, IA64::r71, |
| 222 | IA64::r72, IA64::r73, IA64::r74, IA64::r75, IA64::r76, IA64::r77, |
| 223 | IA64::r78, IA64::r79, IA64::r80, IA64::r81, IA64::r82, IA64::r83, |
| 224 | IA64::r84, IA64::r85, IA64::r86, IA64::r87, IA64::r88, IA64::r89, |
| 225 | IA64::r90, IA64::r91, IA64::r92, IA64::r93, IA64::r94, IA64::r95, |
| 226 | IA64::r96, IA64::r97, IA64::r98, IA64::r99, IA64::r100, IA64::r101, |
| 227 | IA64::r102, IA64::r103, IA64::r104, IA64::r105, IA64::r106, IA64::r107, |
| 228 | IA64::r108, IA64::r109, IA64::r110, IA64::r111, IA64::r112, IA64::r113, |
| 229 | IA64::r114, IA64::r115, IA64::r116, IA64::r117, IA64::r118, IA64::r119, |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 230 | IA64::r120, IA64::r121, IA64::r122, IA64::r123, IA64::r124, IA64::r125, |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 231 | IA64::r126, IA64::r127 }; |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 232 | |
| 233 | unsigned numStackedGPRsUsed=0; |
| 234 | for(int i=0; i<96; i++) { |
| 235 | if(MF.isPhysRegUsed(RegsInOrder[i])) |
| 236 | numStackedGPRsUsed=i+1; // (i+1 and not ++ - consider fn(fp, fp, int) |
| 237 | } |
| 238 | |
| 239 | unsigned numOutRegsUsed=MF.getInfo<IA64FunctionInfo>()->outRegsUsed; |
| 240 | |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 241 | // XXX FIXME : this code should be a bit more reliable (in case there _isn't_ |
| 242 | // a pseudo_alloc in the MBB) |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 243 | unsigned dstRegOfPseudoAlloc; |
| 244 | for(MBBI = MBB.begin(); /*MBBI->getOpcode() != IA64::PSEUDO_ALLOC*/; ++MBBI) { |
| 245 | assert(MBBI != MBB.end()); |
| 246 | if(MBBI->getOpcode() == IA64::PSEUDO_ALLOC) { |
| 247 | dstRegOfPseudoAlloc=MBBI->getOperand(0).getReg(); |
| 248 | break; |
| 249 | } |
| 250 | } |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 251 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 252 | MI=BuildMI(TII.get(IA64::ALLOC)).addReg(dstRegOfPseudoAlloc).addImm(0). \ |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 253 | addImm(numStackedGPRsUsed).addImm(numOutRegsUsed).addImm(0); |
| 254 | MBB.insert(MBBI, MI); |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 255 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 256 | // Get the number of bytes to allocate from the FrameInfo |
| 257 | unsigned NumBytes = MFI->getStackSize(); |
| 258 | |
| 259 | if (MFI->hasCalls() && !FP) { |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 260 | // We reserve argument space for call sites in the function immediately on |
| 261 | // entry to the current function. This eliminates the need for add/sub |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 262 | // brackets around call sites. |
| 263 | NumBytes += MFI->getMaxCallFrameSize(); |
| 264 | } |
| 265 | |
| 266 | if(FP) |
| 267 | NumBytes += 8; // reserve space for the old FP |
| 268 | |
| 269 | // Do we need to allocate space on the stack? |
| 270 | if (NumBytes == 0) |
| 271 | return; |
| 272 | |
| 273 | // Add 16 bytes at the bottom of the stack (scratch area) |
| 274 | // and round the size to a multiple of the alignment. |
| 275 | unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 276 | unsigned Size = 16 + (FP ? 8 : 0); |
| 277 | NumBytes = (NumBytes+Size+Align-1)/Align*Align; |
| 278 | |
| 279 | // Update frame info to pretend that this is part of the stack... |
| 280 | MFI->setStackSize(NumBytes); |
| 281 | |
| 282 | // adjust stack pointer: r12 -= numbytes |
| 283 | if (NumBytes <= 8191) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 284 | MI=BuildMI(TII.get(IA64::ADDIMM22),IA64::r12).addReg(IA64::r12). |
Evan Cheng | 7ce4578 | 2006-11-13 23:36:35 +0000 | [diff] [blame] | 285 | addImm(-NumBytes); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 286 | MBB.insert(MBBI, MI); |
| 287 | } else { // we use r22 as a scratch register here |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 288 | MI=BuildMI(TII.get(IA64::MOVLIMM64), IA64::r22).addImm(-NumBytes); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 289 | // FIXME: MOVLSI32 expects a _u_32imm |
| 290 | MBB.insert(MBBI, MI); // first load the decrement into r22 |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 291 | MI=BuildMI(TII.get(IA64::ADD), IA64::r12).addReg(IA64::r12).addReg(IA64::r22); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 292 | MBB.insert(MBBI, MI); // then add (subtract) it to r12 (stack ptr) |
| 293 | } |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 294 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 295 | // now if we need to, save the old FP and set the new |
| 296 | if (FP) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 297 | MI = BuildMI(TII.get(IA64::ST8)).addReg(IA64::r12).addReg(IA64::r5); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 298 | MBB.insert(MBBI, MI); |
| 299 | // this must be the last instr in the prolog ? (XXX: why??) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 300 | MI = BuildMI(TII.get(IA64::MOV), IA64::r5).addReg(IA64::r12); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 301 | MBB.insert(MBBI, MI); |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 302 | } |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 303 | |
| 304 | } |
| 305 | |
| 306 | void IA64RegisterInfo::emitEpilogue(MachineFunction &MF, |
| 307 | MachineBasicBlock &MBB) const { |
| 308 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 309 | MachineBasicBlock::iterator MBBI = prior(MBB.end()); |
| 310 | MachineInstr *MI; |
| 311 | assert(MBBI->getOpcode() == IA64::RET && |
| 312 | "Can only insert epilog into returning blocks"); |
| 313 | |
| 314 | bool FP = hasFP(MF); |
| 315 | |
| 316 | // Get the number of bytes allocated from the FrameInfo... |
| 317 | unsigned NumBytes = MFI->getStackSize(); |
| 318 | |
| 319 | //now if we need to, restore the old FP |
| 320 | if (FP) |
| 321 | { |
| 322 | //copy the FP into the SP (discards allocas) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 323 | MI=BuildMI(TII.get(IA64::MOV), IA64::r12).addReg(IA64::r5); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 324 | MBB.insert(MBBI, MI); |
| 325 | //restore the FP |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 326 | MI=BuildMI(TII.get(IA64::LD8), IA64::r5).addReg(IA64::r5); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 327 | MBB.insert(MBBI, MI); |
| 328 | } |
| 329 | |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 330 | if (NumBytes != 0) |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 331 | { |
| 332 | if (NumBytes <= 8191) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 333 | MI=BuildMI(TII.get(IA64::ADDIMM22),IA64::r12).addReg(IA64::r12). |
Evan Cheng | 7ce4578 | 2006-11-13 23:36:35 +0000 | [diff] [blame] | 334 | addImm(NumBytes); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 335 | MBB.insert(MBBI, MI); |
| 336 | } else { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 337 | MI=BuildMI(TII.get(IA64::MOVLIMM64), IA64::r22).addImm(NumBytes); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 338 | MBB.insert(MBBI, MI); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 339 | MI=BuildMI(TII.get(IA64::ADD), IA64::r12).addReg(IA64::r12). |
Evan Cheng | 7ce4578 | 2006-11-13 23:36:35 +0000 | [diff] [blame] | 340 | addReg(IA64::r22); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 341 | MBB.insert(MBBI, MI); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | } |
| 346 | |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 347 | unsigned IA64RegisterInfo::getRARegister() const { |
| 348 | assert(0 && "What is the return address register"); |
| 349 | return 0; |
| 350 | } |
| 351 | |
Jim Laskey | a997918 | 2006-03-28 13:48:33 +0000 | [diff] [blame] | 352 | unsigned IA64RegisterInfo::getFrameRegister(MachineFunction &MF) const { |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 353 | return hasFP(MF) ? IA64::r5 : IA64::r12; |
Jim Laskey | f1d78e8 | 2006-03-23 18:12:57 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 356 | #include "IA64GenRegisterInfo.inc" |
| 357 | |