Misha Brukman | cf2b9ac | 2002-11-22 22:43:47 +0000 | [diff] [blame] | 1 | //===- X86RegisterInfo.cpp - X86 Register Information -----------*- C++ -*-===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 10 | // This file contains the X86 implementation of the MRegisterInfo class. This |
| 11 | // file is responsible for the frame pointer elimination optimization on X86. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Misha Brukman | b83b286 | 2002-11-20 18:59:43 +0000 | [diff] [blame] | 15 | #include "X86.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 16 | #include "X86RegisterInfo.h" |
Misha Brukman | cf2b9ac | 2002-11-22 22:43:47 +0000 | [diff] [blame] | 17 | #include "X86InstrBuilder.h" |
Misha Brukman | b83b286 | 2002-11-20 18:59:43 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Type.h" |
Chris Lattner | c8c377d | 2003-07-29 05:14:16 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/ValueTypes.h" |
Misha Brukman | b83b286 | 2002-11-20 18:59:43 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 198ab64 | 2002-12-15 20:06:35 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | f158da2 | 2003-01-16 02:20:12 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Target/TargetFrameInfo.h" |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 26 | #include "Support/CommandLine.h" |
Alkis Evlogimenos | f81af21 | 2004-02-14 01:18:34 +0000 | [diff] [blame] | 27 | #include "Support/STLExtras.h" |
Chris Lattner | 300d0ed | 2004-02-14 06:00:36 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | cl::opt<bool> |
Chris Lattner | ddd5b41 | 2003-02-26 20:00:41 +0000 | [diff] [blame] | 32 | NoFPElim("disable-fp-elim", |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 33 | cl::desc("Disable frame pointer elimination optimization")); |
| 34 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 7ad3e06 | 2003-08-03 15:48:14 +0000 | [diff] [blame] | 36 | X86RegisterInfo::X86RegisterInfo() |
| 37 | : X86GenRegisterInfo(X86::ADJCALLSTACKDOWN, X86::ADJCALLSTACKUP) {} |
| 38 | |
Chris Lattner | 8e58179 | 2002-12-25 05:07:09 +0000 | [diff] [blame] | 39 | static unsigned getIdx(const TargetRegisterClass *RC) { |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 40 | switch (RC->getSize()) { |
Chris Lattner | 6eaa469 | 2002-12-20 04:13:28 +0000 | [diff] [blame] | 41 | default: assert(0 && "Invalid data size!"); |
Chris Lattner | 8e58179 | 2002-12-25 05:07:09 +0000 | [diff] [blame] | 42 | case 1: return 0; |
| 43 | case 2: return 1; |
| 44 | case 4: return 2; |
| 45 | case 10: return 3; |
Misha Brukman | 87e369d | 2002-12-13 12:00:06 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Alkis Evlogimenos | e668dab | 2003-11-04 22:57:09 +0000 | [diff] [blame] | 49 | int X86RegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
Alkis Evlogimenos | 024126e | 2004-02-12 08:11:04 +0000 | [diff] [blame] | 50 | MachineBasicBlock::iterator MI, |
Alkis Evlogimenos | e668dab | 2003-11-04 22:57:09 +0000 | [diff] [blame] | 51 | unsigned SrcReg, int FrameIdx, |
| 52 | const TargetRegisterClass *RC) const { |
Chris Lattner | 8e58179 | 2002-12-25 05:07:09 +0000 | [diff] [blame] | 53 | static const unsigned Opcode[] = |
| 54 | { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32, X86::FSTPr80 }; |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 55 | MachineInstr *I = addFrameReference(BuildMI(Opcode[getIdx(RC)], 5), |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 56 | FrameIdx).addReg(SrcReg); |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 57 | MBB.insert(MI, I); |
Alkis Evlogimenos | e668dab | 2003-11-04 22:57:09 +0000 | [diff] [blame] | 58 | return 1; |
Misha Brukman | b83b286 | 2002-11-20 18:59:43 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Alkis Evlogimenos | e668dab | 2003-11-04 22:57:09 +0000 | [diff] [blame] | 61 | int X86RegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Alkis Evlogimenos | 024126e | 2004-02-12 08:11:04 +0000 | [diff] [blame] | 62 | MachineBasicBlock::iterator MI, |
Alkis Evlogimenos | e668dab | 2003-11-04 22:57:09 +0000 | [diff] [blame] | 63 | unsigned DestReg, int FrameIdx, |
| 64 | const TargetRegisterClass *RC) const{ |
Chris Lattner | 8e58179 | 2002-12-25 05:07:09 +0000 | [diff] [blame] | 65 | static const unsigned Opcode[] = |
| 66 | { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, X86::FLDr80 }; |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 67 | unsigned OC = Opcode[getIdx(RC)]; |
| 68 | MBB.insert(MI, addFrameReference(BuildMI(OC, 4, DestReg), FrameIdx)); |
Alkis Evlogimenos | e668dab | 2003-11-04 22:57:09 +0000 | [diff] [blame] | 69 | return 1; |
Misha Brukman | b83b286 | 2002-11-20 18:59:43 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Alkis Evlogimenos | e668dab | 2003-11-04 22:57:09 +0000 | [diff] [blame] | 72 | int X86RegisterInfo::copyRegToReg(MachineBasicBlock &MBB, |
Alkis Evlogimenos | 024126e | 2004-02-12 08:11:04 +0000 | [diff] [blame] | 73 | MachineBasicBlock::iterator MI, |
Alkis Evlogimenos | e668dab | 2003-11-04 22:57:09 +0000 | [diff] [blame] | 74 | unsigned DestReg, unsigned SrcReg, |
| 75 | const TargetRegisterClass *RC) const { |
Chris Lattner | 8e58179 | 2002-12-25 05:07:09 +0000 | [diff] [blame] | 76 | static const unsigned Opcode[] = |
| 77 | { X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV }; |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 78 | MBB.insert(MI, BuildMI(Opcode[getIdx(RC)],1,DestReg).addReg(SrcReg)); |
Alkis Evlogimenos | e668dab | 2003-11-04 22:57:09 +0000 | [diff] [blame] | 79 | return 1; |
Misha Brukman | 2b46e8e | 2002-12-13 09:54:12 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 82 | //===----------------------------------------------------------------------===// |
| 83 | // Stack Frame Processing methods |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | // hasFP - Return true if the specified function should have a dedicated frame |
| 87 | // pointer register. This is true if the function has variable sized allocas or |
| 88 | // if frame pointer elimination is disabled. |
| 89 | // |
| 90 | static bool hasFP(MachineFunction &MF) { |
| 91 | return NoFPElim || MF.getFrameInfo()->hasVarSizedObjects(); |
Misha Brukman | 03c6faf | 2002-12-03 23:11:21 +0000 | [diff] [blame] | 92 | } |
Misha Brukman | 2adb395 | 2002-12-04 23:57:03 +0000 | [diff] [blame] | 93 | |
Chris Lattner | bb07ef9 | 2004-02-14 19:49:54 +0000 | [diff] [blame] | 94 | void X86RegisterInfo:: |
| 95 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 96 | MachineBasicBlock::iterator I) const { |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 97 | if (hasFP(MF)) { |
| 98 | // If we have a frame pointer, turn the adjcallstackup instruction into a |
| 99 | // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP, |
| 100 | // <amt>' |
Chris Lattner | bb07ef9 | 2004-02-14 19:49:54 +0000 | [diff] [blame] | 101 | MachineInstr *Old = I; |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 102 | unsigned Amount = Old->getOperand(0).getImmedValue(); |
| 103 | if (Amount != 0) { |
Chris Lattner | f158da2 | 2003-01-16 02:20:12 +0000 | [diff] [blame] | 104 | // We need to keep the stack aligned properly. To do this, we round the |
| 105 | // amount of space needed for the outgoing arguments up to the next |
| 106 | // alignment boundary. |
| 107 | unsigned Align = MF.getTarget().getFrameInfo().getStackAlignment(); |
| 108 | Amount = (Amount+Align-1)/Align*Align; |
| 109 | |
Chris Lattner | bb07ef9 | 2004-02-14 19:49:54 +0000 | [diff] [blame] | 110 | MachineInstr *New; |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 111 | if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) { |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 112 | New=BuildMI(X86::SUBri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(Amount); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 113 | } else { |
| 114 | assert(Old->getOpcode() == X86::ADJCALLSTACKUP); |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 115 | New=BuildMI(X86::ADDri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(Amount); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | bb07ef9 | 2004-02-14 19:49:54 +0000 | [diff] [blame] | 117 | |
| 118 | // Replace the pseudo instruction with a new instruction... |
| 119 | MBB.insert(I, New); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Chris Lattner | bb07ef9 | 2004-02-14 19:49:54 +0000 | [diff] [blame] | 123 | MBB.erase(I); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | bb07ef9 | 2004-02-14 19:49:54 +0000 | [diff] [blame] | 126 | void X86RegisterInfo::eliminateFrameIndex(MachineFunction &MF, |
Alkis Evlogimenos | 024126e | 2004-02-12 08:11:04 +0000 | [diff] [blame] | 127 | MachineBasicBlock::iterator II) const { |
Chris Lattner | d264bec | 2003-01-13 00:50:33 +0000 | [diff] [blame] | 128 | unsigned i = 0; |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 129 | MachineInstr &MI = *II; |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 130 | while (!MI.getOperand(i).isFrameIndex()) { |
| 131 | ++i; |
| 132 | assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); |
| 133 | } |
| 134 | |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 135 | int FrameIndex = MI.getOperand(i).getFrameIndex(); |
Chris Lattner | d264bec | 2003-01-13 00:50:33 +0000 | [diff] [blame] | 136 | |
| 137 | // This must be part of a four operand memory reference. Replace the |
| 138 | // FrameIndex with base register with EBP. Add add an offset to the offset. |
| 139 | MI.SetMachineOperandReg(i, hasFP(MF) ? X86::EBP : X86::ESP); |
| 140 | |
| 141 | // Now add the frame object offset to the offset from EBP. |
| 142 | int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) + |
Chris Lattner | eafa423 | 2003-01-15 22:57:35 +0000 | [diff] [blame] | 143 | MI.getOperand(i+3).getImmedValue()+4; |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 144 | |
Chris Lattner | d5b7c47 | 2003-10-14 18:52:41 +0000 | [diff] [blame] | 145 | if (!hasFP(MF)) |
| 146 | Offset += MF.getFrameInfo()->getStackSize(); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 147 | |
Chris Lattner | d264bec | 2003-01-13 00:50:33 +0000 | [diff] [blame] | 148 | MI.SetMachineOperandConst(i+3, MachineOperand::MO_SignExtendedImmed, Offset); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chris Lattner | bb07ef9 | 2004-02-14 19:49:54 +0000 | [diff] [blame] | 151 | void |
| 152 | X86RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF) const{ |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 153 | if (hasFP(MF)) { |
| 154 | // Create a frame entry for the EBP register that must be saved. |
| 155 | int FrameIdx = MF.getFrameInfo()->CreateStackObject(4, 4); |
| 156 | assert(FrameIdx == MF.getFrameInfo()->getObjectIndexEnd()-1 && |
| 157 | "Slot for EBP register must be last in order to be found!"); |
| 158 | } |
| 159 | } |
| 160 | |
Chris Lattner | bb07ef9 | 2004-02-14 19:49:54 +0000 | [diff] [blame] | 161 | void X86RegisterInfo::emitPrologue(MachineFunction &MF) const { |
Chris Lattner | 198ab64 | 2002-12-15 20:06:35 +0000 | [diff] [blame] | 162 | MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB |
| 163 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
Chris Lattner | eafa423 | 2003-01-15 22:57:35 +0000 | [diff] [blame] | 164 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 165 | MachineInstr *MI; |
Misha Brukman | 2adb395 | 2002-12-04 23:57:03 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 167 | // Get the number of bytes to allocate from the FrameInfo |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 168 | unsigned NumBytes = MFI->getStackSize(); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 169 | if (hasFP(MF)) { |
| 170 | // Get the offset of the stack slot for the EBP register... which is |
| 171 | // guaranteed to be the last slot by processFunctionBeforeFrameFinalized. |
Chris Lattner | eafa423 | 2003-01-15 22:57:35 +0000 | [diff] [blame] | 172 | int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1)+4; |
Chris Lattner | 3bbe7cc | 2002-12-17 03:15:26 +0000 | [diff] [blame] | 173 | |
Chris Lattner | c2b81f6 | 2003-10-14 19:09:05 +0000 | [diff] [blame] | 174 | if (NumBytes) { // adjust stack pointer: ESP -= numbytes |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 175 | MI= BuildMI(X86::SUBri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(NumBytes); |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 176 | MBB.insert(MBBI, MI); |
Chris Lattner | c2b81f6 | 2003-10-14 19:09:05 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Save EBP into the appropriate stack slot... |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 180 | MI = addRegOffset(BuildMI(X86::MOVrm32, 5), // mov [ESP-<offset>], EBP |
Chris Lattner | c2b81f6 | 2003-10-14 19:09:05 +0000 | [diff] [blame] | 181 | X86::ESP, EBPOffset+NumBytes).addReg(X86::EBP); |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 182 | MBB.insert(MBBI, MI); |
Chris Lattner | c2b81f6 | 2003-10-14 19:09:05 +0000 | [diff] [blame] | 183 | |
| 184 | // Update EBP with the new base value... |
| 185 | if (NumBytes == 0) // mov EBP, ESP |
| 186 | MI = BuildMI(X86::MOVrr32, 2, X86::EBP).addReg(X86::ESP); |
| 187 | else // lea EBP, [ESP+StackSize] |
| 188 | MI = addRegOffset(BuildMI(X86::LEAr32, 5, X86::EBP), X86::ESP, NumBytes); |
| 189 | |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 190 | MBB.insert(MBBI, MI); |
Chris Lattner | c2b81f6 | 2003-10-14 19:09:05 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 192 | } else { |
Chris Lattner | e9dd84e | 2004-02-14 20:11:07 +0000 | [diff] [blame^] | 193 | if (MFI->hasCalls()) { |
| 194 | // When we have no frame pointer, we reserve argument space for call sites |
| 195 | // in the function immediately on entry to the current function. This |
| 196 | // eliminates the need for add/sub ESP brackets around call sites. |
| 197 | // |
| 198 | NumBytes += MFI->getMaxCallFrameSize(); |
| 199 | |
| 200 | // Round the size to a multiple of the alignment (don't forget the 4 byte |
| 201 | // offset though). |
| 202 | unsigned Align = MF.getTarget().getFrameInfo().getStackAlignment(); |
| 203 | NumBytes = ((NumBytes+4)+Align-1)/Align*Align - 4; |
| 204 | } |
Chris Lattner | f158da2 | 2003-01-16 02:20:12 +0000 | [diff] [blame] | 205 | |
Chris Lattner | eafa423 | 2003-01-15 22:57:35 +0000 | [diff] [blame] | 206 | // Update frame info to pretend that this is part of the stack... |
| 207 | MFI->setStackSize(NumBytes); |
Misha Brukman | 2adb395 | 2002-12-04 23:57:03 +0000 | [diff] [blame] | 208 | |
Chris Lattner | c2b81f6 | 2003-10-14 19:09:05 +0000 | [diff] [blame] | 209 | if (NumBytes) { |
| 210 | // adjust stack pointer: ESP -= numbytes |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 211 | MI= BuildMI(X86::SUBri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(NumBytes); |
Chris Lattner | c2b81f6 | 2003-10-14 19:09:05 +0000 | [diff] [blame] | 212 | MBB.insert(MBBI, MI); |
| 213 | } |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 214 | } |
Misha Brukman | 2adb395 | 2002-12-04 23:57:03 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Chris Lattner | bb07ef9 | 2004-02-14 19:49:54 +0000 | [diff] [blame] | 217 | void X86RegisterInfo::emitEpilogue(MachineFunction &MF, |
| 218 | MachineBasicBlock &MBB) const { |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 219 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Alkis Evlogimenos | f81af21 | 2004-02-14 01:18:34 +0000 | [diff] [blame] | 220 | MachineBasicBlock::iterator MBBI = prior(MBB.end()); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 221 | MachineInstr *MI; |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 222 | assert(MBBI->getOpcode() == X86::RET && |
Chris Lattner | 198ab64 | 2002-12-15 20:06:35 +0000 | [diff] [blame] | 223 | "Can only insert epilog into returning blocks"); |
Misha Brukman | 2adb395 | 2002-12-04 23:57:03 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 225 | if (hasFP(MF)) { |
| 226 | // Get the offset of the stack slot for the EBP register... which is |
| 227 | // guaranteed to be the last slot by processFunctionBeforeFrameFinalized. |
Chris Lattner | eafa423 | 2003-01-15 22:57:35 +0000 | [diff] [blame] | 228 | int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1)+4; |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 229 | |
| 230 | // mov ESP, EBP |
| 231 | MI = BuildMI(X86::MOVrr32, 1,X86::ESP).addReg(X86::EBP); |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 232 | MBB.insert(MBBI, MI); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 233 | |
| 234 | // mov EBP, [ESP-<offset>] |
| 235 | MI = addRegOffset(BuildMI(X86::MOVmr32, 5, X86::EBP), X86::ESP, EBPOffset); |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 236 | MBB.insert(MBBI, MI); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 237 | } else { |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 238 | // Get the number of bytes allocated from the FrameInfo... |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 239 | unsigned NumBytes = MFI->getStackSize(); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 240 | |
| 241 | if (NumBytes) { // adjust stack pointer back: ESP += numbytes |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 242 | MI =BuildMI(X86::ADDri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(NumBytes); |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 243 | MBB.insert(MBBI, MI); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Chris Lattner | 7ad3e06 | 2003-08-03 15:48:14 +0000 | [diff] [blame] | 248 | #include "X86GenRegisterInfo.inc" |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 249 | |
| 250 | const TargetRegisterClass* |
| 251 | X86RegisterInfo::getRegClassForType(const Type* Ty) const { |
| 252 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | d264bec | 2003-01-13 00:50:33 +0000 | [diff] [blame] | 253 | case Type::LongTyID: |
| 254 | case Type::ULongTyID: assert(0 && "Long values can't fit in registers!"); |
| 255 | default: assert(0 && "Invalid type to getClass!"); |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 256 | case Type::BoolTyID: |
| 257 | case Type::SByteTyID: |
Chris Lattner | 6770aed | 2003-08-04 20:58:29 +0000 | [diff] [blame] | 258 | case Type::UByteTyID: return &R8Instance; |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 259 | case Type::ShortTyID: |
Chris Lattner | 6770aed | 2003-08-04 20:58:29 +0000 | [diff] [blame] | 260 | case Type::UShortTyID: return &R16Instance; |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 261 | case Type::IntTyID: |
| 262 | case Type::UIntTyID: |
Chris Lattner | 6770aed | 2003-08-04 20:58:29 +0000 | [diff] [blame] | 263 | case Type::PointerTyID: return &R32Instance; |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 264 | |
| 265 | case Type::FloatTyID: |
Chris Lattner | 6770aed | 2003-08-04 20:58:29 +0000 | [diff] [blame] | 266 | case Type::DoubleTyID: return &RFPInstance; |
Chris Lattner | 3c1c03d | 2002-12-28 20:32:28 +0000 | [diff] [blame] | 267 | } |
Misha Brukman | 2adb395 | 2002-12-04 23:57:03 +0000 | [diff] [blame] | 268 | } |