X86RegisterInfo.cpp revision cc0d2f586f87a6aef77514ee75dab67cbfc235cb
1dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//===- X86RegisterInfo.cpp - X86 Register Information -----------*- C++ -*-===// 2dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// 3dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// The LLVM Compiler Infrastructure 4dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// 5dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// This file was developed by the LLVM research group and is distributed under 6dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// the University of Illinois Open Source License. See LICENSE.TXT for details. 7dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// 8dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//===----------------------------------------------------------------------===// 9dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// 10dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// This file contains the X86 implementation of the MRegisterInfo class. This 11dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// file is responsible for the frame pointer elimination optimization on X86. 12dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// 13dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//===----------------------------------------------------------------------===// 14dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 15dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "X86.h" 16dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "X86RegisterInfo.h" 17dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "X86InstrBuilder.h" 18dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/Constants.h" 19dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/Type.h" 20c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include "llvm/CodeGen/ValueTypes.h" 21c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include "llvm/CodeGen/MachineInstrBuilder.h" 22c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include "llvm/CodeGen/MachineFunction.h" 23c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include "llvm/CodeGen/MachineFrameInfo.h" 24c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include "llvm/Target/TargetMachine.h" 25c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include "llvm/Target/TargetFrameInfo.h" 26dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "Support/CommandLine.h" 27dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "Support/STLExtras.h" 28dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesusing namespace llvm; 29dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 30dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesnamespace { 31dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines cl::opt<bool> 32dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines NoFPElim("disable-fp-elim", 33dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines cl::desc("Disable frame pointer elimination optimization")); 34c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines cl::opt<bool> 35c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines NoFusing("disable-spill-fusing", 36dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines cl::desc("Disable fusing of spill code into instructions")); 37dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines cl::opt<bool> 38dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines PrintFailedFusing("print-failed-fuse-candidates", 39dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines cl::desc("Print instructions that the allocator wants to" 40dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines " fuse, but the X86 backend currently can't"), 41dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines cl::Hidden); 42dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines} 43dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 44c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen HinesX86RegisterInfo::X86RegisterInfo() 45c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines : X86GenRegisterInfo(X86::ADJCALLSTACKDOWN, X86::ADJCALLSTACKUP) {} 46dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 47cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarstatic unsigned getIdx(const TargetRegisterClass *RC) { 48dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines switch (RC->getSize()) { 49dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines default: assert(0 && "Invalid data size!"); 50dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines case 1: return 0; 51dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines case 2: return 1; 52 case 4: return 2; 53 case 10: return 3; 54 } 55} 56 57int X86RegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 58 MachineBasicBlock::iterator MI, 59 unsigned SrcReg, int FrameIdx, 60 const TargetRegisterClass *RC) const { 61 static const unsigned Opcode[] = 62 { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, X86::FSTPr80 }; 63 MachineInstr *I = addFrameReference(BuildMI(Opcode[getIdx(RC)], 5), 64 FrameIdx).addReg(SrcReg); 65 MBB.insert(MI, I); 66 return 1; 67} 68 69int X86RegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 70 MachineBasicBlock::iterator MI, 71 unsigned DestReg, int FrameIdx, 72 const TargetRegisterClass *RC) const{ 73 static const unsigned Opcode[] = 74 { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32, X86::FLDr80 }; 75 unsigned OC = Opcode[getIdx(RC)]; 76 MBB.insert(MI, addFrameReference(BuildMI(OC, 4, DestReg), FrameIdx)); 77 return 1; 78} 79 80int X86RegisterInfo::copyRegToReg(MachineBasicBlock &MBB, 81 MachineBasicBlock::iterator MI, 82 unsigned DestReg, unsigned SrcReg, 83 const TargetRegisterClass *RC) const { 84 static const unsigned Opcode[] = 85 { X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV }; 86 MBB.insert(MI, BuildMI(Opcode[getIdx(RC)],1,DestReg).addReg(SrcReg)); 87 return 1; 88} 89 90static MachineInstr *MakeMInst(unsigned Opcode, unsigned FrameIndex, 91 MachineInstr *MI) { 92 return addFrameReference(BuildMI(Opcode, 4), FrameIndex); 93} 94 95static MachineInstr *MakeMRInst(unsigned Opcode, unsigned FrameIndex, 96 MachineInstr *MI) { 97 return addFrameReference(BuildMI(Opcode, 5), FrameIndex) 98 .addReg(MI->getOperand(1).getReg()); 99} 100 101static MachineInstr *MakeMIInst(unsigned Opcode, unsigned FrameIndex, 102 MachineInstr *MI) { 103 if (MI->getOperand(1).isImmediate()) 104 return addFrameReference(BuildMI(Opcode, 5), FrameIndex) 105 .addZImm(MI->getOperand(1).getImmedValue()); 106 else if (MI->getOperand(1).isGlobalAddress()) 107 return addFrameReference(BuildMI(Opcode, 5), FrameIndex) 108 .addGlobalAddress(MI->getOperand(1).getGlobal()); 109 assert(0 && "Unknown operand for MakeMI!"); 110 return 0; 111} 112 113static MachineInstr *MakeRMInst(unsigned Opcode, unsigned FrameIndex, 114 MachineInstr *MI) { 115 return addFrameReference(BuildMI(Opcode, 5, MI->getOperand(0).getReg()), 116 FrameIndex); 117} 118 119static MachineInstr *MakeRMIInst(unsigned Opcode, unsigned FrameIndex, 120 MachineInstr *MI) { 121 return addFrameReference(BuildMI(Opcode, 5, MI->getOperand(0).getReg()), 122 FrameIndex).addZImm(MI->getOperand(2).getImmedValue()); 123} 124 125 126bool X86RegisterInfo::foldMemoryOperand(MachineBasicBlock::iterator &MI, 127 unsigned i, int FrameIndex) const { 128 if (NoFusing) return false; 129 130 /// FIXME: This should obviously be autogenerated by tablegen when patterns 131 /// are available! 132 MachineBasicBlock& MBB = *MI->getParent(); 133 MachineInstr* NI = 0; 134 if (i == 0) { 135 switch(MI->getOpcode()) { 136 case X86::XCHGrr8: NI = MakeMRInst(X86::XCHGmr8 ,FrameIndex, MI); break; 137 case X86::XCHGrr16:NI = MakeMRInst(X86::XCHGmr16,FrameIndex, MI); break; 138 case X86::XCHGrr32:NI = MakeMRInst(X86::XCHGmr32,FrameIndex, MI); break; 139 case X86::MOVrr8: NI = MakeMRInst(X86::MOVmr8 , FrameIndex, MI); break; 140 case X86::MOVrr16: NI = MakeMRInst(X86::MOVmr16, FrameIndex, MI); break; 141 case X86::MOVrr32: NI = MakeMRInst(X86::MOVmr32, FrameIndex, MI); break; 142 case X86::MOVri8: NI = MakeMIInst(X86::MOVmi8 , FrameIndex, MI); break; 143 case X86::MOVri16: NI = MakeMIInst(X86::MOVmi16, FrameIndex, MI); break; 144 case X86::MOVri32: NI = MakeMIInst(X86::MOVmi32, FrameIndex, MI); break; 145 case X86::MULr8: NI = MakeMInst( X86::MULm8 , FrameIndex, MI); break; 146 case X86::MULr16: NI = MakeMInst( X86::MULm16, FrameIndex, MI); break; 147 case X86::MULr32: NI = MakeMInst( X86::MULm32, FrameIndex, MI); break; 148 case X86::DIVr8: NI = MakeMInst( X86::DIVm8 , FrameIndex, MI); break; 149 case X86::DIVr16: NI = MakeMInst( X86::DIVm16, FrameIndex, MI); break; 150 case X86::DIVr32: NI = MakeMInst( X86::DIVm32, FrameIndex, MI); break; 151 case X86::IDIVr8: NI = MakeMInst( X86::IDIVm8 , FrameIndex, MI); break; 152 case X86::IDIVr16: NI = MakeMInst( X86::IDIVm16, FrameIndex, MI); break; 153 case X86::IDIVr32: NI = MakeMInst( X86::IDIVm32, FrameIndex, MI); break; 154 case X86::NEGr8: NI = MakeMInst( X86::NEGm8 , FrameIndex, MI); break; 155 case X86::NEGr16: NI = MakeMInst( X86::NEGm16, FrameIndex, MI); break; 156 case X86::NEGr32: NI = MakeMInst( X86::NEGm32, FrameIndex, MI); break; 157 case X86::NOTr8: NI = MakeMInst( X86::NOTm8 , FrameIndex, MI); break; 158 case X86::NOTr16: NI = MakeMInst( X86::NOTm16, FrameIndex, MI); break; 159 case X86::NOTr32: NI = MakeMInst( X86::NOTm32, FrameIndex, MI); break; 160 case X86::INCr8: NI = MakeMInst( X86::INCm8 , FrameIndex, MI); break; 161 case X86::INCr16: NI = MakeMInst( X86::INCm16, FrameIndex, MI); break; 162 case X86::INCr32: NI = MakeMInst( X86::INCm32, FrameIndex, MI); break; 163 case X86::DECr8: NI = MakeMInst( X86::DECm8 , FrameIndex, MI); break; 164 case X86::DECr16: NI = MakeMInst( X86::DECm16, FrameIndex, MI); break; 165 case X86::DECr32: NI = MakeMInst( X86::DECm32, FrameIndex, MI); break; 166 case X86::ADDrr8: NI = MakeMRInst(X86::ADDmr8 , FrameIndex, MI); break; 167 case X86::ADDrr16: NI = MakeMRInst(X86::ADDmr16, FrameIndex, MI); break; 168 case X86::ADDrr32: NI = MakeMRInst(X86::ADDmr32, FrameIndex, MI); break; 169 case X86::ADCrr32: NI = MakeMRInst(X86::ADCmr32, FrameIndex, MI); break; 170 case X86::ADDri8: NI = MakeMIInst(X86::ADDmi8 , FrameIndex, MI); break; 171 case X86::ADDri16: NI = MakeMIInst(X86::ADDmi16, FrameIndex, MI); break; 172 case X86::ADDri32: NI = MakeMIInst(X86::ADDmi32, FrameIndex, MI); break; 173 case X86::SUBrr8: NI = MakeMRInst(X86::SUBmr8 , FrameIndex, MI); break; 174 case X86::SUBrr16: NI = MakeMRInst(X86::SUBmr16, FrameIndex, MI); break; 175 case X86::SUBrr32: NI = MakeMRInst(X86::SUBmr32, FrameIndex, MI); break; 176 case X86::SBBrr32: NI = MakeMRInst(X86::SBBmr32, FrameIndex, MI); break; 177 case X86::SUBri8: NI = MakeMIInst(X86::SUBmi8 , FrameIndex, MI); break; 178 case X86::SUBri16: NI = MakeMIInst(X86::SUBmi16, FrameIndex, MI); break; 179 case X86::SUBri32: NI = MakeMIInst(X86::SUBmi32, FrameIndex, MI); break; 180 case X86::ANDrr8: NI = MakeMRInst(X86::ANDmr8 , FrameIndex, MI); break; 181 case X86::ANDrr16: NI = MakeMRInst(X86::ANDmr16, FrameIndex, MI); break; 182 case X86::ANDrr32: NI = MakeMRInst(X86::ANDmr32, FrameIndex, MI); break; 183 case X86::ANDri8: NI = MakeMIInst(X86::ANDmi8 , FrameIndex, MI); break; 184 case X86::ANDri16: NI = MakeMIInst(X86::ANDmi16, FrameIndex, MI); break; 185 case X86::ANDri32: NI = MakeMIInst(X86::ANDmi32, FrameIndex, MI); break; 186 case X86::ORrr8: NI = MakeMRInst(X86::ORmr8 , FrameIndex, MI); break; 187 case X86::ORrr16: NI = MakeMRInst(X86::ORmr16, FrameIndex, MI); break; 188 case X86::ORrr32: NI = MakeMRInst(X86::ORmr32, FrameIndex, MI); break; 189 case X86::ORri8: NI = MakeMIInst(X86::ORmi8 , FrameIndex, MI); break; 190 case X86::ORri16: NI = MakeMIInst(X86::ORmi16, FrameIndex, MI); break; 191 case X86::ORri32: NI = MakeMIInst(X86::ORmi32, FrameIndex, MI); break; 192 case X86::XORrr8: NI = MakeMRInst(X86::XORmr8 , FrameIndex, MI); break; 193 case X86::XORrr16: NI = MakeMRInst(X86::XORmr16, FrameIndex, MI); break; 194 case X86::XORrr32: NI = MakeMRInst(X86::XORmr32, FrameIndex, MI); break; 195 case X86::XORri8: NI = MakeMIInst(X86::XORmi8 , FrameIndex, MI); break; 196 case X86::XORri16: NI = MakeMIInst(X86::XORmi16, FrameIndex, MI); break; 197 case X86::XORri32: NI = MakeMIInst(X86::XORmi32, FrameIndex, MI); break; 198 case X86::TESTrr8: NI = MakeMRInst(X86::TESTmr8 ,FrameIndex, MI); break; 199 case X86::TESTrr16:NI = MakeMRInst(X86::TESTmr16,FrameIndex, MI); break; 200 case X86::TESTrr32:NI = MakeMRInst(X86::TESTmr32,FrameIndex, MI); break; 201 case X86::TESTri8: NI = MakeMIInst(X86::TESTmi8 ,FrameIndex, MI); break; 202 case X86::TESTri16:NI = MakeMIInst(X86::TESTmi16,FrameIndex, MI); break; 203 case X86::TESTri32:NI = MakeMIInst(X86::TESTmi32,FrameIndex, MI); break; 204 case X86::CMPrr8: NI = MakeMRInst(X86::CMPmr8 , FrameIndex, MI); break; 205 case X86::CMPrr16: NI = MakeMRInst(X86::CMPmr16, FrameIndex, MI); break; 206 case X86::CMPrr32: NI = MakeMRInst(X86::CMPmr32, FrameIndex, MI); break; 207 case X86::CMPri8: NI = MakeMIInst(X86::CMPmi8 , FrameIndex, MI); break; 208 case X86::CMPri16: NI = MakeMIInst(X86::CMPmi16, FrameIndex, MI); break; 209 case X86::CMPri32: NI = MakeMIInst(X86::CMPmi32, FrameIndex, MI); break; 210 default: break; // Cannot fold 211 } 212 } else if (i == 1) { 213 switch(MI->getOpcode()) { 214 case X86::XCHGrr8: NI = MakeRMInst(X86::XCHGrm8 ,FrameIndex, MI); break; 215 case X86::XCHGrr16:NI = MakeRMInst(X86::XCHGrm16,FrameIndex, MI); break; 216 case X86::XCHGrr32:NI = MakeRMInst(X86::XCHGrm32,FrameIndex, MI); break; 217 case X86::MOVrr8: NI = MakeRMInst(X86::MOVrm8 , FrameIndex, MI); break; 218 case X86::MOVrr16: NI = MakeRMInst(X86::MOVrm16, FrameIndex, MI); break; 219 case X86::MOVrr32: NI = MakeRMInst(X86::MOVrm32, FrameIndex, MI); break; 220 case X86::ADDrr8: NI = MakeRMInst(X86::ADDrm8 , FrameIndex, MI); break; 221 case X86::ADDrr16: NI = MakeRMInst(X86::ADDrm16, FrameIndex, MI); break; 222 case X86::ADDrr32: NI = MakeRMInst(X86::ADDrm32, FrameIndex, MI); break; 223 case X86::ADCrr32: NI = MakeRMInst(X86::ADCrm32, FrameIndex, MI); break; 224 case X86::SUBrr8: NI = MakeRMInst(X86::SUBrm8 , FrameIndex, MI); break; 225 case X86::SUBrr16: NI = MakeRMInst(X86::SUBrm16, FrameIndex, MI); break; 226 case X86::SUBrr32: NI = MakeRMInst(X86::SUBrm32, FrameIndex, MI); break; 227 case X86::SBBrr32: NI = MakeRMInst(X86::SBBrm32, FrameIndex, MI); break; 228 case X86::ANDrr8: NI = MakeRMInst(X86::ANDrm8 , FrameIndex, MI); break; 229 case X86::ANDrr16: NI = MakeRMInst(X86::ANDrm16, FrameIndex, MI); break; 230 case X86::ANDrr32: NI = MakeRMInst(X86::ANDrm32, FrameIndex, MI); break; 231 case X86::ORrr8: NI = MakeRMInst(X86::ORrm8 , FrameIndex, MI); break; 232 case X86::ORrr16: NI = MakeRMInst(X86::ORrm16, FrameIndex, MI); break; 233 case X86::ORrr32: NI = MakeRMInst(X86::ORrm32, FrameIndex, MI); break; 234 case X86::XORrr8: NI = MakeRMInst(X86::XORrm8 , FrameIndex, MI); break; 235 case X86::XORrr16: NI = MakeRMInst(X86::XORrm16, FrameIndex, MI); break; 236 case X86::XORrr32: NI = MakeRMInst(X86::XORrm32, FrameIndex, MI); break; 237 case X86::TESTrr8: NI = MakeRMInst(X86::TESTrm8 ,FrameIndex, MI); break; 238 case X86::TESTrr16:NI = MakeRMInst(X86::TESTrm16,FrameIndex, MI); break; 239 case X86::TESTrr32:NI = MakeRMInst(X86::TESTrm32,FrameIndex, MI); break; 240 case X86::IMULrr16:NI = MakeRMInst(X86::IMULrm16,FrameIndex, MI); break; 241 case X86::IMULrr32:NI = MakeRMInst(X86::IMULrm32,FrameIndex, MI); break; 242 case X86::IMULrri16: NI = MakeRMIInst(X86::IMULrmi16, FrameIndex, MI);break; 243 case X86::IMULrri32: NI = MakeRMIInst(X86::IMULrmi32, FrameIndex, MI);break; 244 case X86::CMPrr8: NI = MakeRMInst(X86::CMPrm8 , FrameIndex, MI); break; 245 case X86::CMPrr16: NI = MakeRMInst(X86::CMPrm16, FrameIndex, MI); break; 246 case X86::CMPrr32: NI = MakeRMInst(X86::CMPrm32, FrameIndex, MI); break; 247 case X86::MOVSXr16r8: NI = MakeRMInst(X86::MOVSXr16m8 , FrameIndex, MI); break; 248 case X86::MOVSXr32r8: NI = MakeRMInst(X86::MOVSXr32m8, FrameIndex, MI); break; 249 case X86::MOVSXr32r16:NI = MakeRMInst(X86::MOVSXr32m16, FrameIndex, MI); break; 250 case X86::MOVZXr16r8: NI = MakeRMInst(X86::MOVZXr16m8 , FrameIndex, MI); break; 251 case X86::MOVZXr32r8: NI = MakeRMInst(X86::MOVZXr32m8, FrameIndex, MI); break; 252 case X86::MOVZXr32r16:NI = MakeRMInst(X86::MOVZXr32m16, FrameIndex, MI); break; 253 default: break; 254 } 255 } 256 if (NI) { 257 MI = MBB.insert(MBB.erase(MI), NI); 258 return true; 259 } else { 260 if (PrintFailedFusing) 261 std::cerr << "We failed to fuse: " << *MI; 262 return false; 263 } 264} 265 266//===----------------------------------------------------------------------===// 267// Stack Frame Processing methods 268//===----------------------------------------------------------------------===// 269 270// hasFP - Return true if the specified function should have a dedicated frame 271// pointer register. This is true if the function has variable sized allocas or 272// if frame pointer elimination is disabled. 273// 274static bool hasFP(MachineFunction &MF) { 275 return NoFPElim || MF.getFrameInfo()->hasVarSizedObjects(); 276} 277 278void X86RegisterInfo:: 279eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 280 MachineBasicBlock::iterator I) const { 281 if (hasFP(MF)) { 282 // If we have a frame pointer, turn the adjcallstackup instruction into a 283 // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP, 284 // <amt>' 285 MachineInstr *Old = I; 286 unsigned Amount = Old->getOperand(0).getImmedValue(); 287 if (Amount != 0) { 288 // We need to keep the stack aligned properly. To do this, we round the 289 // amount of space needed for the outgoing arguments up to the next 290 // alignment boundary. 291 unsigned Align = MF.getTarget().getFrameInfo().getStackAlignment(); 292 Amount = (Amount+Align-1)/Align*Align; 293 294 MachineInstr *New; 295 if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) { 296 New=BuildMI(X86::SUBri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(Amount); 297 } else { 298 assert(Old->getOpcode() == X86::ADJCALLSTACKUP); 299 New=BuildMI(X86::ADDri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(Amount); 300 } 301 302 // Replace the pseudo instruction with a new instruction... 303 MBB.insert(I, New); 304 } 305 } 306 307 MBB.erase(I); 308} 309 310void X86RegisterInfo::eliminateFrameIndex(MachineFunction &MF, 311 MachineBasicBlock::iterator II) const { 312 unsigned i = 0; 313 MachineInstr &MI = *II; 314 while (!MI.getOperand(i).isFrameIndex()) { 315 ++i; 316 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 317 } 318 319 int FrameIndex = MI.getOperand(i).getFrameIndex(); 320 321 // This must be part of a four operand memory reference. Replace the 322 // FrameIndex with base register with EBP. Add add an offset to the offset. 323 MI.SetMachineOperandReg(i, hasFP(MF) ? X86::EBP : X86::ESP); 324 325 // Now add the frame object offset to the offset from EBP. 326 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) + 327 MI.getOperand(i+3).getImmedValue()+4; 328 329 if (!hasFP(MF)) 330 Offset += MF.getFrameInfo()->getStackSize(); 331 else 332 Offset += 4; // Skip the saved EBP 333 334 MI.SetMachineOperandConst(i+3, MachineOperand::MO_SignExtendedImmed, Offset); 335} 336 337void 338X86RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF) const{ 339 if (hasFP(MF)) { 340 // Create a frame entry for the EBP register that must be saved. 341 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, -8); 342 assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() && 343 "Slot for EBP register must be last in order to be found!"); 344 } 345} 346 347void X86RegisterInfo::emitPrologue(MachineFunction &MF) const { 348 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB 349 MachineBasicBlock::iterator MBBI = MBB.begin(); 350 MachineFrameInfo *MFI = MF.getFrameInfo(); 351 MachineInstr *MI; 352 353 // Get the number of bytes to allocate from the FrameInfo 354 unsigned NumBytes = MFI->getStackSize(); 355 if (hasFP(MF)) { 356 // Get the offset of the stack slot for the EBP register... which is 357 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized. 358 int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexBegin())+4; 359 360 if (NumBytes) { // adjust stack pointer: ESP -= numbytes 361 MI= BuildMI(X86::SUBri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(NumBytes); 362 MBB.insert(MBBI, MI); 363 } 364 365 // Save EBP into the appropriate stack slot... 366 MI = addRegOffset(BuildMI(X86::MOVmr32, 5), // mov [ESP-<offset>], EBP 367 X86::ESP, EBPOffset+NumBytes).addReg(X86::EBP); 368 MBB.insert(MBBI, MI); 369 370 // Update EBP with the new base value... 371 if (NumBytes == 4) // mov EBP, ESP 372 MI = BuildMI(X86::MOVrr32, 2, X86::EBP).addReg(X86::ESP); 373 else // lea EBP, [ESP+StackSize] 374 MI = addRegOffset(BuildMI(X86::LEAr32, 5, X86::EBP), X86::ESP,NumBytes-4); 375 376 MBB.insert(MBBI, MI); 377 378 } else { 379 if (MFI->hasCalls()) { 380 // When we have no frame pointer, we reserve argument space for call sites 381 // in the function immediately on entry to the current function. This 382 // eliminates the need for add/sub ESP brackets around call sites. 383 // 384 NumBytes += MFI->getMaxCallFrameSize(); 385 386 // Round the size to a multiple of the alignment (don't forget the 4 byte 387 // offset though). 388 unsigned Align = MF.getTarget().getFrameInfo().getStackAlignment(); 389 NumBytes = ((NumBytes+4)+Align-1)/Align*Align - 4; 390 } 391 392 // Update frame info to pretend that this is part of the stack... 393 MFI->setStackSize(NumBytes); 394 395 if (NumBytes) { 396 // adjust stack pointer: ESP -= numbytes 397 MI= BuildMI(X86::SUBri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(NumBytes); 398 MBB.insert(MBBI, MI); 399 } 400 } 401} 402 403void X86RegisterInfo::emitEpilogue(MachineFunction &MF, 404 MachineBasicBlock &MBB) const { 405 const MachineFrameInfo *MFI = MF.getFrameInfo(); 406 MachineBasicBlock::iterator MBBI = prior(MBB.end()); 407 MachineInstr *MI; 408 assert(MBBI->getOpcode() == X86::RET && 409 "Can only insert epilog into returning blocks"); 410 411 if (hasFP(MF)) { 412 // Get the offset of the stack slot for the EBP register... which is 413 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized. 414 int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1)+4; 415 416 // mov ESP, EBP 417 MI = BuildMI(X86::MOVrr32, 1,X86::ESP).addReg(X86::EBP); 418 MBB.insert(MBBI, MI); 419 420 // pop EBP 421 MI = BuildMI(X86::POPr32, 0, X86::EBP); 422 MBB.insert(MBBI, MI); 423 } else { 424 // Get the number of bytes allocated from the FrameInfo... 425 unsigned NumBytes = MFI->getStackSize(); 426 427 if (NumBytes) { // adjust stack pointer back: ESP += numbytes 428 MI =BuildMI(X86::ADDri32, 1, X86::ESP, MOTy::UseAndDef).addZImm(NumBytes); 429 MBB.insert(MBBI, MI); 430 } 431 } 432} 433 434#include "X86GenRegisterInfo.inc" 435 436const TargetRegisterClass* 437X86RegisterInfo::getRegClassForType(const Type* Ty) const { 438 switch (Ty->getPrimitiveID()) { 439 case Type::LongTyID: 440 case Type::ULongTyID: assert(0 && "Long values can't fit in registers!"); 441 default: assert(0 && "Invalid type to getClass!"); 442 case Type::BoolTyID: 443 case Type::SByteTyID: 444 case Type::UByteTyID: return &R8Instance; 445 case Type::ShortTyID: 446 case Type::UShortTyID: return &R16Instance; 447 case Type::IntTyID: 448 case Type::UIntTyID: 449 case Type::PointerTyID: return &R32Instance; 450 451 case Type::FloatTyID: 452 case Type::DoubleTyID: return &RFPInstance; 453 } 454} 455