X86RegisterInfo.cpp revision 6e041c2015dc25140b20e09ed7adbc9b8d6dc62b
1//===- X86RegisterInfo.cpp - X86 Register Information -----------*- C++ -*-===// 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//===----------------------------------------------------------------------===// 9// 10// This file contains the X86 implementation of the MRegisterInfo class. This 11// file is responsible for the frame pointer elimination optimization on X86. 12// 13//===----------------------------------------------------------------------===// 14 15#include "X86.h" 16#include "X86RegisterInfo.h" 17#include "X86InstrBuilder.h" 18#include "X86MachineFunctionInfo.h" 19#include "X86Subtarget.h" 20#include "X86TargetMachine.h" 21#include "llvm/Constants.h" 22#include "llvm/Function.h" 23#include "llvm/Type.h" 24#include "llvm/CodeGen/ValueTypes.h" 25#include "llvm/CodeGen/MachineInstrBuilder.h" 26#include "llvm/CodeGen/MachineFunction.h" 27#include "llvm/CodeGen/MachineFrameInfo.h" 28#include "llvm/CodeGen/MachineLocation.h" 29#include "llvm/CodeGen/SSARegMap.h" 30#include "llvm/Target/TargetAsmInfo.h" 31#include "llvm/Target/TargetFrameInfo.h" 32#include "llvm/Target/TargetInstrInfo.h" 33#include "llvm/Target/TargetMachine.h" 34#include "llvm/Target/TargetOptions.h" 35#include "llvm/Support/CommandLine.h" 36#include "llvm/ADT/BitVector.h" 37#include "llvm/ADT/STLExtras.h" 38using namespace llvm; 39 40namespace { 41 cl::opt<bool> 42 NoFusing("disable-spill-fusing", 43 cl::desc("Disable fusing of spill code into instructions")); 44 cl::opt<bool> 45 PrintFailedFusing("print-failed-fuse-candidates", 46 cl::desc("Print instructions that the allocator wants to" 47 " fuse, but the X86 backend currently can't"), 48 cl::Hidden); 49} 50 51X86RegisterInfo::X86RegisterInfo(X86TargetMachine &tm, 52 const TargetInstrInfo &tii) 53 : X86GenRegisterInfo(X86::ADJCALLSTACKDOWN, X86::ADJCALLSTACKUP), 54 TM(tm), TII(tii) { 55 // Cache some information. 56 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>(); 57 Is64Bit = Subtarget->is64Bit(); 58 StackAlign = TM.getFrameInfo()->getStackAlignment(); 59 if (Is64Bit) { 60 SlotSize = 8; 61 StackPtr = X86::RSP; 62 FramePtr = X86::RBP; 63 } else { 64 SlotSize = 4; 65 StackPtr = X86::ESP; 66 FramePtr = X86::EBP; 67 } 68 69 SmallVector<unsigned,16> AmbEntries; 70 static const unsigned OpTbl2Addr[][2] = { 71 { X86::ADC32ri, X86::ADC32mi }, 72 { X86::ADC32ri8, X86::ADC32mi8 }, 73 { X86::ADC32rr, X86::ADC32mr }, 74 { X86::ADC64ri32, X86::ADC64mi32 }, 75 { X86::ADC64ri8, X86::ADC64mi8 }, 76 { X86::ADC64rr, X86::ADC64mr }, 77 { X86::ADD16ri, X86::ADD16mi }, 78 { X86::ADD16ri8, X86::ADD16mi8 }, 79 { X86::ADD16rr, X86::ADD16mr }, 80 { X86::ADD32ri, X86::ADD32mi }, 81 { X86::ADD32ri8, X86::ADD32mi8 }, 82 { X86::ADD32rr, X86::ADD32mr }, 83 { X86::ADD64ri32, X86::ADD64mi32 }, 84 { X86::ADD64ri8, X86::ADD64mi8 }, 85 { X86::ADD64rr, X86::ADD64mr }, 86 { X86::ADD8ri, X86::ADD8mi }, 87 { X86::ADD8rr, X86::ADD8mr }, 88 { X86::AND16ri, X86::AND16mi }, 89 { X86::AND16ri8, X86::AND16mi8 }, 90 { X86::AND16rr, X86::AND16mr }, 91 { X86::AND32ri, X86::AND32mi }, 92 { X86::AND32ri8, X86::AND32mi8 }, 93 { X86::AND32rr, X86::AND32mr }, 94 { X86::AND64ri32, X86::AND64mi32 }, 95 { X86::AND64ri8, X86::AND64mi8 }, 96 { X86::AND64rr, X86::AND64mr }, 97 { X86::AND8ri, X86::AND8mi }, 98 { X86::AND8rr, X86::AND8mr }, 99 { X86::DEC16r, X86::DEC16m }, 100 { X86::DEC32r, X86::DEC32m }, 101 { X86::DEC64_16r, X86::DEC64_16m }, 102 { X86::DEC64_32r, X86::DEC64_32m }, 103 { X86::DEC64r, X86::DEC64m }, 104 { X86::DEC8r, X86::DEC8m }, 105 { X86::INC16r, X86::INC16m }, 106 { X86::INC32r, X86::INC32m }, 107 { X86::INC64_16r, X86::INC64_16m }, 108 { X86::INC64_32r, X86::INC64_32m }, 109 { X86::INC64r, X86::INC64m }, 110 { X86::INC8r, X86::INC8m }, 111 { X86::NEG16r, X86::NEG16m }, 112 { X86::NEG32r, X86::NEG32m }, 113 { X86::NEG64r, X86::NEG64m }, 114 { X86::NEG8r, X86::NEG8m }, 115 { X86::NOT16r, X86::NOT16m }, 116 { X86::NOT32r, X86::NOT32m }, 117 { X86::NOT64r, X86::NOT64m }, 118 { X86::NOT8r, X86::NOT8m }, 119 { X86::OR16ri, X86::OR16mi }, 120 { X86::OR16ri8, X86::OR16mi8 }, 121 { X86::OR16rr, X86::OR16mr }, 122 { X86::OR32ri, X86::OR32mi }, 123 { X86::OR32ri8, X86::OR32mi8 }, 124 { X86::OR32rr, X86::OR32mr }, 125 { X86::OR64ri32, X86::OR64mi32 }, 126 { X86::OR64ri8, X86::OR64mi8 }, 127 { X86::OR64rr, X86::OR64mr }, 128 { X86::OR8ri, X86::OR8mi }, 129 { X86::OR8rr, X86::OR8mr }, 130 { X86::ROL16r1, X86::ROL16m1 }, 131 { X86::ROL16rCL, X86::ROL16mCL }, 132 { X86::ROL16ri, X86::ROL16mi }, 133 { X86::ROL32r1, X86::ROL32m1 }, 134 { X86::ROL32rCL, X86::ROL32mCL }, 135 { X86::ROL32ri, X86::ROL32mi }, 136 { X86::ROL64r1, X86::ROL64m1 }, 137 { X86::ROL64rCL, X86::ROL64mCL }, 138 { X86::ROL64ri, X86::ROL64mi }, 139 { X86::ROL8r1, X86::ROL8m1 }, 140 { X86::ROL8rCL, X86::ROL8mCL }, 141 { X86::ROL8ri, X86::ROL8mi }, 142 { X86::ROR16r1, X86::ROR16m1 }, 143 { X86::ROR16rCL, X86::ROR16mCL }, 144 { X86::ROR16ri, X86::ROR16mi }, 145 { X86::ROR32r1, X86::ROR32m1 }, 146 { X86::ROR32rCL, X86::ROR32mCL }, 147 { X86::ROR32ri, X86::ROR32mi }, 148 { X86::ROR64r1, X86::ROR64m1 }, 149 { X86::ROR64rCL, X86::ROR64mCL }, 150 { X86::ROR64ri, X86::ROR64mi }, 151 { X86::ROR8r1, X86::ROR8m1 }, 152 { X86::ROR8rCL, X86::ROR8mCL }, 153 { X86::ROR8ri, X86::ROR8mi }, 154 { X86::SAR16r1, X86::SAR16m1 }, 155 { X86::SAR16rCL, X86::SAR16mCL }, 156 { X86::SAR16ri, X86::SAR16mi }, 157 { X86::SAR32r1, X86::SAR32m1 }, 158 { X86::SAR32rCL, X86::SAR32mCL }, 159 { X86::SAR32ri, X86::SAR32mi }, 160 { X86::SAR64r1, X86::SAR64m1 }, 161 { X86::SAR64rCL, X86::SAR64mCL }, 162 { X86::SAR64ri, X86::SAR64mi }, 163 { X86::SAR8r1, X86::SAR8m1 }, 164 { X86::SAR8rCL, X86::SAR8mCL }, 165 { X86::SAR8ri, X86::SAR8mi }, 166 { X86::SBB32ri, X86::SBB32mi }, 167 { X86::SBB32ri8, X86::SBB32mi8 }, 168 { X86::SBB32rr, X86::SBB32mr }, 169 { X86::SBB64ri32, X86::SBB64mi32 }, 170 { X86::SBB64ri8, X86::SBB64mi8 }, 171 { X86::SBB64rr, X86::SBB64mr }, 172 { X86::SHL16r1, X86::SHL16m1 }, 173 { X86::SHL16rCL, X86::SHL16mCL }, 174 { X86::SHL16ri, X86::SHL16mi }, 175 { X86::SHL32r1, X86::SHL32m1 }, 176 { X86::SHL32rCL, X86::SHL32mCL }, 177 { X86::SHL32ri, X86::SHL32mi }, 178 { X86::SHL64r1, X86::SHL64m1 }, 179 { X86::SHL64rCL, X86::SHL64mCL }, 180 { X86::SHL64ri, X86::SHL64mi }, 181 { X86::SHL8r1, X86::SHL8m1 }, 182 { X86::SHL8rCL, X86::SHL8mCL }, 183 { X86::SHL8ri, X86::SHL8mi }, 184 { X86::SHLD16rrCL, X86::SHLD16mrCL }, 185 { X86::SHLD16rri8, X86::SHLD16mri8 }, 186 { X86::SHLD32rrCL, X86::SHLD32mrCL }, 187 { X86::SHLD32rri8, X86::SHLD32mri8 }, 188 { X86::SHLD64rrCL, X86::SHLD64mrCL }, 189 { X86::SHLD64rri8, X86::SHLD64mri8 }, 190 { X86::SHR16r1, X86::SHR16m1 }, 191 { X86::SHR16rCL, X86::SHR16mCL }, 192 { X86::SHR16ri, X86::SHR16mi }, 193 { X86::SHR32r1, X86::SHR32m1 }, 194 { X86::SHR32rCL, X86::SHR32mCL }, 195 { X86::SHR32ri, X86::SHR32mi }, 196 { X86::SHR64r1, X86::SHR64m1 }, 197 { X86::SHR64rCL, X86::SHR64mCL }, 198 { X86::SHR64ri, X86::SHR64mi }, 199 { X86::SHR8r1, X86::SHR8m1 }, 200 { X86::SHR8rCL, X86::SHR8mCL }, 201 { X86::SHR8ri, X86::SHR8mi }, 202 { X86::SHRD16rrCL, X86::SHRD16mrCL }, 203 { X86::SHRD16rri8, X86::SHRD16mri8 }, 204 { X86::SHRD32rrCL, X86::SHRD32mrCL }, 205 { X86::SHRD32rri8, X86::SHRD32mri8 }, 206 { X86::SHRD64rrCL, X86::SHRD64mrCL }, 207 { X86::SHRD64rri8, X86::SHRD64mri8 }, 208 { X86::SUB16ri, X86::SUB16mi }, 209 { X86::SUB16ri8, X86::SUB16mi8 }, 210 { X86::SUB16rr, X86::SUB16mr }, 211 { X86::SUB32ri, X86::SUB32mi }, 212 { X86::SUB32ri8, X86::SUB32mi8 }, 213 { X86::SUB32rr, X86::SUB32mr }, 214 { X86::SUB64ri32, X86::SUB64mi32 }, 215 { X86::SUB64ri8, X86::SUB64mi8 }, 216 { X86::SUB64rr, X86::SUB64mr }, 217 { X86::SUB8ri, X86::SUB8mi }, 218 { X86::SUB8rr, X86::SUB8mr }, 219 { X86::XOR16ri, X86::XOR16mi }, 220 { X86::XOR16ri8, X86::XOR16mi8 }, 221 { X86::XOR16rr, X86::XOR16mr }, 222 { X86::XOR32ri, X86::XOR32mi }, 223 { X86::XOR32ri8, X86::XOR32mi8 }, 224 { X86::XOR32rr, X86::XOR32mr }, 225 { X86::XOR64ri32, X86::XOR64mi32 }, 226 { X86::XOR64ri8, X86::XOR64mi8 }, 227 { X86::XOR64rr, X86::XOR64mr }, 228 { X86::XOR8ri, X86::XOR8mi }, 229 { X86::XOR8rr, X86::XOR8mr } 230 }; 231 232 for (unsigned i = 0, e = array_lengthof(OpTbl2Addr); i != e; ++i) { 233 unsigned RegOp = OpTbl2Addr[i][0]; 234 unsigned MemOp = OpTbl2Addr[i][1]; 235 if (!RegOp2MemOpTable2Addr.insert(std::make_pair((unsigned*)RegOp, MemOp))) 236 assert(false && "Duplicated entries?"); 237 unsigned AuxInfo = 0 | (1 << 4) | (1 << 5); // Index 0,folded load and store 238 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp, 239 std::make_pair(RegOp, AuxInfo)))) 240 AmbEntries.push_back(MemOp); 241 } 242 243 // If the third value is 1, then it's folding either a load or a store. 244 static const unsigned OpTbl0[][3] = { 245 { X86::CALL32r, X86::CALL32m, 1 }, 246 { X86::CALL64r, X86::CALL64m, 1 }, 247 { X86::CMP16ri, X86::CMP16mi, 1 }, 248 { X86::CMP16ri8, X86::CMP16mi8, 1 }, 249 { X86::CMP32ri, X86::CMP32mi, 1 }, 250 { X86::CMP32ri8, X86::CMP32mi8, 1 }, 251 { X86::CMP64ri32, X86::CMP64mi32, 1 }, 252 { X86::CMP64ri8, X86::CMP64mi8, 1 }, 253 { X86::CMP8ri, X86::CMP8mi, 1 }, 254 { X86::DIV16r, X86::DIV16m, 1 }, 255 { X86::DIV32r, X86::DIV32m, 1 }, 256 { X86::DIV64r, X86::DIV64m, 1 }, 257 { X86::DIV8r, X86::DIV8m, 1 }, 258 { X86::FsMOVAPDrr, X86::MOVSDmr, 0 }, 259 { X86::FsMOVAPSrr, X86::MOVSSmr, 0 }, 260 { X86::IDIV16r, X86::IDIV16m, 1 }, 261 { X86::IDIV32r, X86::IDIV32m, 1 }, 262 { X86::IDIV64r, X86::IDIV64m, 1 }, 263 { X86::IDIV8r, X86::IDIV8m, 1 }, 264 { X86::IMUL16r, X86::IMUL16m, 1 }, 265 { X86::IMUL32r, X86::IMUL32m, 1 }, 266 { X86::IMUL64r, X86::IMUL64m, 1 }, 267 { X86::IMUL8r, X86::IMUL8m, 1 }, 268 { X86::JMP32r, X86::JMP32m, 1 }, 269 { X86::JMP64r, X86::JMP64m, 1 }, 270 { X86::MOV16ri, X86::MOV16mi, 0 }, 271 { X86::MOV16rr, X86::MOV16mr, 0 }, 272 { X86::MOV16to16_, X86::MOV16_mr, 0 }, 273 { X86::MOV32ri, X86::MOV32mi, 0 }, 274 { X86::MOV32rr, X86::MOV32mr, 0 }, 275 { X86::MOV32to32_, X86::MOV32_mr, 0 }, 276 { X86::MOV64ri32, X86::MOV64mi32, 0 }, 277 { X86::MOV64rr, X86::MOV64mr, 0 }, 278 { X86::MOV8ri, X86::MOV8mi, 0 }, 279 { X86::MOV8rr, X86::MOV8mr, 0 }, 280 { X86::MOVAPDrr, X86::MOVAPDmr, 0 }, 281 { X86::MOVAPSrr, X86::MOVAPSmr, 0 }, 282 { X86::MOVPDI2DIrr, X86::MOVPDI2DImr, 0 }, 283 { X86::MOVPQIto64rr,X86::MOVPQIto64mr, 0 }, 284 { X86::MOVPS2SSrr, X86::MOVPS2SSmr, 0 }, 285 { X86::MOVSDrr, X86::MOVSDmr, 0 }, 286 { X86::MOVSDto64rr, X86::MOVSDto64mr, 0 }, 287 { X86::MOVSS2DIrr, X86::MOVSS2DImr, 0 }, 288 { X86::MOVSSrr, X86::MOVSSmr, 0 }, 289 { X86::MOVUPDrr, X86::MOVUPDmr, 0 }, 290 { X86::MOVUPSrr, X86::MOVUPSmr, 0 }, 291 { X86::MUL16r, X86::MUL16m, 1 }, 292 { X86::MUL32r, X86::MUL32m, 1 }, 293 { X86::MUL64r, X86::MUL64m, 1 }, 294 { X86::MUL8r, X86::MUL8m, 1 }, 295 { X86::SETAEr, X86::SETAEm, 0 }, 296 { X86::SETAr, X86::SETAm, 0 }, 297 { X86::SETBEr, X86::SETBEm, 0 }, 298 { X86::SETBr, X86::SETBm, 0 }, 299 { X86::SETEr, X86::SETEm, 0 }, 300 { X86::SETGEr, X86::SETGEm, 0 }, 301 { X86::SETGr, X86::SETGm, 0 }, 302 { X86::SETLEr, X86::SETLEm, 0 }, 303 { X86::SETLr, X86::SETLm, 0 }, 304 { X86::SETNEr, X86::SETNEm, 0 }, 305 { X86::SETNPr, X86::SETNPm, 0 }, 306 { X86::SETNSr, X86::SETNSm, 0 }, 307 { X86::SETPr, X86::SETPm, 0 }, 308 { X86::SETSr, X86::SETSm, 0 }, 309 { X86::TAILJMPr, X86::TAILJMPm, 1 }, 310 { X86::TEST16ri, X86::TEST16mi, 1 }, 311 { X86::TEST32ri, X86::TEST32mi, 1 }, 312 { X86::TEST64ri32, X86::TEST64mi32, 1 }, 313 { X86::TEST8ri, X86::TEST8mi, 1 }, 314 { X86::XCHG16rr, X86::XCHG16mr, 0 }, 315 { X86::XCHG32rr, X86::XCHG32mr, 0 }, 316 { X86::XCHG64rr, X86::XCHG64mr, 0 }, 317 { X86::XCHG8rr, X86::XCHG8mr, 0 } 318 }; 319 320 for (unsigned i = 0, e = array_lengthof(OpTbl0); i != e; ++i) { 321 unsigned RegOp = OpTbl0[i][0]; 322 unsigned MemOp = OpTbl0[i][1]; 323 if (!RegOp2MemOpTable0.insert(std::make_pair((unsigned*)RegOp, MemOp))) 324 assert(false && "Duplicated entries?"); 325 unsigned FoldedLoad = OpTbl0[i][2]; 326 // Index 0, folded load or store. 327 unsigned AuxInfo = 0 | (FoldedLoad << 4) | ((FoldedLoad^1) << 5); 328 if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr) 329 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp, 330 std::make_pair(RegOp, AuxInfo)))) 331 AmbEntries.push_back(MemOp); 332 } 333 334 static const unsigned OpTbl1[][2] = { 335 { X86::CMP16rr, X86::CMP16rm }, 336 { X86::CMP32rr, X86::CMP32rm }, 337 { X86::CMP64rr, X86::CMP64rm }, 338 { X86::CMP8rr, X86::CMP8rm }, 339 { X86::CVTSD2SSrr, X86::CVTSD2SSrm }, 340 { X86::CVTSI2SD64rr, X86::CVTSI2SD64rm }, 341 { X86::CVTSI2SDrr, X86::CVTSI2SDrm }, 342 { X86::CVTSI2SS64rr, X86::CVTSI2SS64rm }, 343 { X86::CVTSI2SSrr, X86::CVTSI2SSrm }, 344 { X86::CVTSS2SDrr, X86::CVTSS2SDrm }, 345 { X86::CVTTSD2SI64rr, X86::CVTTSD2SI64rm }, 346 { X86::CVTTSD2SIrr, X86::CVTTSD2SIrm }, 347 { X86::CVTTSS2SI64rr, X86::CVTTSS2SI64rm }, 348 { X86::CVTTSS2SIrr, X86::CVTTSS2SIrm }, 349 { X86::FsMOVAPDrr, X86::MOVSDrm }, 350 { X86::FsMOVAPSrr, X86::MOVSSrm }, 351 { X86::IMUL16rri, X86::IMUL16rmi }, 352 { X86::IMUL16rri8, X86::IMUL16rmi8 }, 353 { X86::IMUL32rri, X86::IMUL32rmi }, 354 { X86::IMUL32rri8, X86::IMUL32rmi8 }, 355 { X86::IMUL64rri32, X86::IMUL64rmi32 }, 356 { X86::IMUL64rri8, X86::IMUL64rmi8 }, 357 { X86::Int_CMPSDrr, X86::Int_CMPSDrm }, 358 { X86::Int_CMPSSrr, X86::Int_CMPSSrm }, 359 { X86::Int_COMISDrr, X86::Int_COMISDrm }, 360 { X86::Int_COMISSrr, X86::Int_COMISSrm }, 361 { X86::Int_CVTDQ2PDrr, X86::Int_CVTDQ2PDrm }, 362 { X86::Int_CVTDQ2PSrr, X86::Int_CVTDQ2PSrm }, 363 { X86::Int_CVTPD2DQrr, X86::Int_CVTPD2DQrm }, 364 { X86::Int_CVTPD2PSrr, X86::Int_CVTPD2PSrm }, 365 { X86::Int_CVTPS2DQrr, X86::Int_CVTPS2DQrm }, 366 { X86::Int_CVTPS2PDrr, X86::Int_CVTPS2PDrm }, 367 { X86::Int_CVTSD2SI64rr,X86::Int_CVTSD2SI64rm }, 368 { X86::Int_CVTSD2SIrr, X86::Int_CVTSD2SIrm }, 369 { X86::Int_CVTSD2SSrr, X86::Int_CVTSD2SSrm }, 370 { X86::Int_CVTSI2SD64rr,X86::Int_CVTSI2SD64rm }, 371 { X86::Int_CVTSI2SDrr, X86::Int_CVTSI2SDrm }, 372 { X86::Int_CVTSI2SS64rr,X86::Int_CVTSI2SS64rm }, 373 { X86::Int_CVTSI2SSrr, X86::Int_CVTSI2SSrm }, 374 { X86::Int_CVTSS2SDrr, X86::Int_CVTSS2SDrm }, 375 { X86::Int_CVTSS2SI64rr,X86::Int_CVTSS2SI64rm }, 376 { X86::Int_CVTSS2SIrr, X86::Int_CVTSS2SIrm }, 377 { X86::Int_CVTTPD2DQrr, X86::Int_CVTTPD2DQrm }, 378 { X86::Int_CVTTPS2DQrr, X86::Int_CVTTPS2DQrm }, 379 { X86::Int_CVTTSD2SI64rr,X86::Int_CVTTSD2SI64rm }, 380 { X86::Int_CVTTSD2SIrr, X86::Int_CVTTSD2SIrm }, 381 { X86::Int_CVTTSS2SI64rr,X86::Int_CVTTSS2SI64rm }, 382 { X86::Int_CVTTSS2SIrr, X86::Int_CVTTSS2SIrm }, 383 { X86::Int_UCOMISDrr, X86::Int_UCOMISDrm }, 384 { X86::Int_UCOMISSrr, X86::Int_UCOMISSrm }, 385 { X86::MOV16rr, X86::MOV16rm }, 386 { X86::MOV16to16_, X86::MOV16_rm }, 387 { X86::MOV32rr, X86::MOV32rm }, 388 { X86::MOV32to32_, X86::MOV32_rm }, 389 { X86::MOV64rr, X86::MOV64rm }, 390 { X86::MOV64toPQIrr, X86::MOV64toPQIrm }, 391 { X86::MOV64toSDrr, X86::MOV64toSDrm }, 392 { X86::MOV8rr, X86::MOV8rm }, 393 { X86::MOVAPDrr, X86::MOVAPDrm }, 394 { X86::MOVAPSrr, X86::MOVAPSrm }, 395 { X86::MOVDDUPrr, X86::MOVDDUPrm }, 396 { X86::MOVDI2PDIrr, X86::MOVDI2PDIrm }, 397 { X86::MOVDI2SSrr, X86::MOVDI2SSrm }, 398 { X86::MOVSD2PDrr, X86::MOVSD2PDrm }, 399 { X86::MOVSDrr, X86::MOVSDrm }, 400 { X86::MOVSHDUPrr, X86::MOVSHDUPrm }, 401 { X86::MOVSLDUPrr, X86::MOVSLDUPrm }, 402 { X86::MOVSS2PSrr, X86::MOVSS2PSrm }, 403 { X86::MOVSSrr, X86::MOVSSrm }, 404 { X86::MOVSX16rr8, X86::MOVSX16rm8 }, 405 { X86::MOVSX32rr16, X86::MOVSX32rm16 }, 406 { X86::MOVSX32rr8, X86::MOVSX32rm8 }, 407 { X86::MOVSX64rr16, X86::MOVSX64rm16 }, 408 { X86::MOVSX64rr32, X86::MOVSX64rm32 }, 409 { X86::MOVSX64rr8, X86::MOVSX64rm8 }, 410 { X86::MOVUPDrr, X86::MOVUPDrm }, 411 { X86::MOVUPSrr, X86::MOVUPSrm }, 412 { X86::MOVZX16rr8, X86::MOVZX16rm8 }, 413 { X86::MOVZX32rr16, X86::MOVZX32rm16 }, 414 { X86::MOVZX32rr8, X86::MOVZX32rm8 }, 415 { X86::MOVZX64rr16, X86::MOVZX64rm16 }, 416 { X86::MOVZX64rr8, X86::MOVZX64rm8 }, 417 { X86::PSHUFDri, X86::PSHUFDmi }, 418 { X86::PSHUFHWri, X86::PSHUFHWmi }, 419 { X86::PSHUFLWri, X86::PSHUFLWmi }, 420 { X86::PsMOVZX64rr32, X86::PsMOVZX64rm32 }, 421 { X86::RCPPSr, X86::RCPPSm }, 422 { X86::RCPPSr_Int, X86::RCPPSm_Int }, 423 { X86::RSQRTPSr, X86::RSQRTPSm }, 424 { X86::RSQRTPSr_Int, X86::RSQRTPSm_Int }, 425 { X86::RSQRTSSr, X86::RSQRTSSm }, 426 { X86::RSQRTSSr_Int, X86::RSQRTSSm_Int }, 427 { X86::SQRTPDr, X86::SQRTPDm }, 428 { X86::SQRTPDr_Int, X86::SQRTPDm_Int }, 429 { X86::SQRTPSr, X86::SQRTPSm }, 430 { X86::SQRTPSr_Int, X86::SQRTPSm_Int }, 431 { X86::SQRTSDr, X86::SQRTSDm }, 432 { X86::SQRTSDr_Int, X86::SQRTSDm_Int }, 433 { X86::SQRTSSr, X86::SQRTSSm }, 434 { X86::SQRTSSr_Int, X86::SQRTSSm_Int }, 435 { X86::TEST16rr, X86::TEST16rm }, 436 { X86::TEST32rr, X86::TEST32rm }, 437 { X86::TEST64rr, X86::TEST64rm }, 438 { X86::TEST8rr, X86::TEST8rm }, 439 // FIXME: TEST*rr EAX,EAX ---> CMP [mem], 0 440 { X86::UCOMISDrr, X86::UCOMISDrm }, 441 { X86::UCOMISSrr, X86::UCOMISSrm }, 442 { X86::XCHG16rr, X86::XCHG16rm }, 443 { X86::XCHG32rr, X86::XCHG32rm }, 444 { X86::XCHG64rr, X86::XCHG64rm }, 445 { X86::XCHG8rr, X86::XCHG8rm } 446 }; 447 448 for (unsigned i = 0, e = array_lengthof(OpTbl1); i != e; ++i) { 449 unsigned RegOp = OpTbl1[i][0]; 450 unsigned MemOp = OpTbl1[i][1]; 451 if (!RegOp2MemOpTable1.insert(std::make_pair((unsigned*)RegOp, MemOp))) 452 assert(false && "Duplicated entries?"); 453 unsigned AuxInfo = 1 | (1 << 4); // Index 1, folded load 454 if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr) 455 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp, 456 std::make_pair(RegOp, AuxInfo)))) 457 AmbEntries.push_back(MemOp); 458 } 459 460 static const unsigned OpTbl2[][2] = { 461 { X86::ADC32rr, X86::ADC32rm }, 462 { X86::ADC64rr, X86::ADC64rm }, 463 { X86::ADD16rr, X86::ADD16rm }, 464 { X86::ADD32rr, X86::ADD32rm }, 465 { X86::ADD64rr, X86::ADD64rm }, 466 { X86::ADD8rr, X86::ADD8rm }, 467 { X86::ADDPDrr, X86::ADDPDrm }, 468 { X86::ADDPSrr, X86::ADDPSrm }, 469 { X86::ADDSDrr, X86::ADDSDrm }, 470 { X86::ADDSSrr, X86::ADDSSrm }, 471 { X86::ADDSUBPDrr, X86::ADDSUBPDrm }, 472 { X86::ADDSUBPSrr, X86::ADDSUBPSrm }, 473 { X86::AND16rr, X86::AND16rm }, 474 { X86::AND32rr, X86::AND32rm }, 475 { X86::AND64rr, X86::AND64rm }, 476 { X86::AND8rr, X86::AND8rm }, 477 { X86::ANDNPDrr, X86::ANDNPDrm }, 478 { X86::ANDNPSrr, X86::ANDNPSrm }, 479 { X86::ANDPDrr, X86::ANDPDrm }, 480 { X86::ANDPSrr, X86::ANDPSrm }, 481 { X86::CMOVA16rr, X86::CMOVA16rm }, 482 { X86::CMOVA32rr, X86::CMOVA32rm }, 483 { X86::CMOVA64rr, X86::CMOVA64rm }, 484 { X86::CMOVAE16rr, X86::CMOVAE16rm }, 485 { X86::CMOVAE32rr, X86::CMOVAE32rm }, 486 { X86::CMOVAE64rr, X86::CMOVAE64rm }, 487 { X86::CMOVB16rr, X86::CMOVB16rm }, 488 { X86::CMOVB32rr, X86::CMOVB32rm }, 489 { X86::CMOVB64rr, X86::CMOVB64rm }, 490 { X86::CMOVBE16rr, X86::CMOVBE16rm }, 491 { X86::CMOVBE32rr, X86::CMOVBE32rm }, 492 { X86::CMOVBE64rr, X86::CMOVBE64rm }, 493 { X86::CMOVE16rr, X86::CMOVE16rm }, 494 { X86::CMOVE32rr, X86::CMOVE32rm }, 495 { X86::CMOVE64rr, X86::CMOVE64rm }, 496 { X86::CMOVG16rr, X86::CMOVG16rm }, 497 { X86::CMOVG32rr, X86::CMOVG32rm }, 498 { X86::CMOVG64rr, X86::CMOVG64rm }, 499 { X86::CMOVGE16rr, X86::CMOVGE16rm }, 500 { X86::CMOVGE32rr, X86::CMOVGE32rm }, 501 { X86::CMOVGE64rr, X86::CMOVGE64rm }, 502 { X86::CMOVL16rr, X86::CMOVL16rm }, 503 { X86::CMOVL32rr, X86::CMOVL32rm }, 504 { X86::CMOVL64rr, X86::CMOVL64rm }, 505 { X86::CMOVLE16rr, X86::CMOVLE16rm }, 506 { X86::CMOVLE32rr, X86::CMOVLE32rm }, 507 { X86::CMOVLE64rr, X86::CMOVLE64rm }, 508 { X86::CMOVNE16rr, X86::CMOVNE16rm }, 509 { X86::CMOVNE32rr, X86::CMOVNE32rm }, 510 { X86::CMOVNE64rr, X86::CMOVNE64rm }, 511 { X86::CMOVNP16rr, X86::CMOVNP16rm }, 512 { X86::CMOVNP32rr, X86::CMOVNP32rm }, 513 { X86::CMOVNP64rr, X86::CMOVNP64rm }, 514 { X86::CMOVNS16rr, X86::CMOVNS16rm }, 515 { X86::CMOVNS32rr, X86::CMOVNS32rm }, 516 { X86::CMOVNS64rr, X86::CMOVNS64rm }, 517 { X86::CMOVP16rr, X86::CMOVP16rm }, 518 { X86::CMOVP32rr, X86::CMOVP32rm }, 519 { X86::CMOVP64rr, X86::CMOVP64rm }, 520 { X86::CMOVS16rr, X86::CMOVS16rm }, 521 { X86::CMOVS32rr, X86::CMOVS32rm }, 522 { X86::CMOVS64rr, X86::CMOVS64rm }, 523 { X86::CMPPDrri, X86::CMPPDrmi }, 524 { X86::CMPPSrri, X86::CMPPSrmi }, 525 { X86::CMPSDrr, X86::CMPSDrm }, 526 { X86::CMPSSrr, X86::CMPSSrm }, 527 { X86::DIVPDrr, X86::DIVPDrm }, 528 { X86::DIVPSrr, X86::DIVPSrm }, 529 { X86::DIVSDrr, X86::DIVSDrm }, 530 { X86::DIVSSrr, X86::DIVSSrm }, 531 { X86::HADDPDrr, X86::HADDPDrm }, 532 { X86::HADDPSrr, X86::HADDPSrm }, 533 { X86::HSUBPDrr, X86::HSUBPDrm }, 534 { X86::HSUBPSrr, X86::HSUBPSrm }, 535 { X86::IMUL16rr, X86::IMUL16rm }, 536 { X86::IMUL32rr, X86::IMUL32rm }, 537 { X86::IMUL64rr, X86::IMUL64rm }, 538 { X86::MAXPDrr, X86::MAXPDrm }, 539 { X86::MAXPDrr_Int, X86::MAXPDrm_Int }, 540 { X86::MAXPSrr, X86::MAXPSrm }, 541 { X86::MAXPSrr_Int, X86::MAXPSrm_Int }, 542 { X86::MAXSDrr, X86::MAXSDrm }, 543 { X86::MAXSDrr_Int, X86::MAXSDrm_Int }, 544 { X86::MAXSSrr, X86::MAXSSrm }, 545 { X86::MAXSSrr_Int, X86::MAXSSrm_Int }, 546 { X86::MINPDrr, X86::MINPDrm }, 547 { X86::MINPDrr_Int, X86::MINPDrm_Int }, 548 { X86::MINPSrr, X86::MINPSrm }, 549 { X86::MINPSrr_Int, X86::MINPSrm_Int }, 550 { X86::MINSDrr, X86::MINSDrm }, 551 { X86::MINSDrr_Int, X86::MINSDrm_Int }, 552 { X86::MINSSrr, X86::MINSSrm }, 553 { X86::MINSSrr_Int, X86::MINSSrm_Int }, 554 { X86::MULPDrr, X86::MULPDrm }, 555 { X86::MULPSrr, X86::MULPSrm }, 556 { X86::MULSDrr, X86::MULSDrm }, 557 { X86::MULSSrr, X86::MULSSrm }, 558 { X86::OR16rr, X86::OR16rm }, 559 { X86::OR32rr, X86::OR32rm }, 560 { X86::OR64rr, X86::OR64rm }, 561 { X86::OR8rr, X86::OR8rm }, 562 { X86::ORPDrr, X86::ORPDrm }, 563 { X86::ORPSrr, X86::ORPSrm }, 564 { X86::PACKSSDWrr, X86::PACKSSDWrm }, 565 { X86::PACKSSWBrr, X86::PACKSSWBrm }, 566 { X86::PACKUSWBrr, X86::PACKUSWBrm }, 567 { X86::PADDBrr, X86::PADDBrm }, 568 { X86::PADDDrr, X86::PADDDrm }, 569 { X86::PADDQrr, X86::PADDQrm }, 570 { X86::PADDSBrr, X86::PADDSBrm }, 571 { X86::PADDSWrr, X86::PADDSWrm }, 572 { X86::PADDWrr, X86::PADDWrm }, 573 { X86::PANDNrr, X86::PANDNrm }, 574 { X86::PANDrr, X86::PANDrm }, 575 { X86::PAVGBrr, X86::PAVGBrm }, 576 { X86::PAVGWrr, X86::PAVGWrm }, 577 { X86::PCMPEQBrr, X86::PCMPEQBrm }, 578 { X86::PCMPEQDrr, X86::PCMPEQDrm }, 579 { X86::PCMPEQWrr, X86::PCMPEQWrm }, 580 { X86::PCMPGTBrr, X86::PCMPGTBrm }, 581 { X86::PCMPGTDrr, X86::PCMPGTDrm }, 582 { X86::PCMPGTWrr, X86::PCMPGTWrm }, 583 { X86::PINSRWrri, X86::PINSRWrmi }, 584 { X86::PMADDWDrr, X86::PMADDWDrm }, 585 { X86::PMAXSWrr, X86::PMAXSWrm }, 586 { X86::PMAXUBrr, X86::PMAXUBrm }, 587 { X86::PMINSWrr, X86::PMINSWrm }, 588 { X86::PMINUBrr, X86::PMINUBrm }, 589 { X86::PMULHUWrr, X86::PMULHUWrm }, 590 { X86::PMULHWrr, X86::PMULHWrm }, 591 { X86::PMULLWrr, X86::PMULLWrm }, 592 { X86::PMULUDQrr, X86::PMULUDQrm }, 593 { X86::PORrr, X86::PORrm }, 594 { X86::PSADBWrr, X86::PSADBWrm }, 595 { X86::PSLLDrr, X86::PSLLDrm }, 596 { X86::PSLLQrr, X86::PSLLQrm }, 597 { X86::PSLLWrr, X86::PSLLWrm }, 598 { X86::PSRADrr, X86::PSRADrm }, 599 { X86::PSRAWrr, X86::PSRAWrm }, 600 { X86::PSRLDrr, X86::PSRLDrm }, 601 { X86::PSRLQrr, X86::PSRLQrm }, 602 { X86::PSRLWrr, X86::PSRLWrm }, 603 { X86::PSUBBrr, X86::PSUBBrm }, 604 { X86::PSUBDrr, X86::PSUBDrm }, 605 { X86::PSUBSBrr, X86::PSUBSBrm }, 606 { X86::PSUBSWrr, X86::PSUBSWrm }, 607 { X86::PSUBWrr, X86::PSUBWrm }, 608 { X86::PUNPCKHBWrr, X86::PUNPCKHBWrm }, 609 { X86::PUNPCKHDQrr, X86::PUNPCKHDQrm }, 610 { X86::PUNPCKHQDQrr, X86::PUNPCKHQDQrm }, 611 { X86::PUNPCKHWDrr, X86::PUNPCKHWDrm }, 612 { X86::PUNPCKLBWrr, X86::PUNPCKLBWrm }, 613 { X86::PUNPCKLDQrr, X86::PUNPCKLDQrm }, 614 { X86::PUNPCKLQDQrr, X86::PUNPCKLQDQrm }, 615 { X86::PUNPCKLWDrr, X86::PUNPCKLWDrm }, 616 { X86::PXORrr, X86::PXORrm }, 617 { X86::SBB32rr, X86::SBB32rm }, 618 { X86::SBB64rr, X86::SBB64rm }, 619 { X86::SHUFPDrri, X86::SHUFPDrmi }, 620 { X86::SHUFPSrri, X86::SHUFPSrmi }, 621 { X86::SUB16rr, X86::SUB16rm }, 622 { X86::SUB32rr, X86::SUB32rm }, 623 { X86::SUB64rr, X86::SUB64rm }, 624 { X86::SUB8rr, X86::SUB8rm }, 625 { X86::SUBPDrr, X86::SUBPDrm }, 626 { X86::SUBPSrr, X86::SUBPSrm }, 627 { X86::SUBSDrr, X86::SUBSDrm }, 628 { X86::SUBSSrr, X86::SUBSSrm }, 629 // FIXME: TEST*rr -> swapped operand of TEST*mr. 630 { X86::UNPCKHPDrr, X86::UNPCKHPDrm }, 631 { X86::UNPCKHPSrr, X86::UNPCKHPSrm }, 632 { X86::UNPCKLPDrr, X86::UNPCKLPDrm }, 633 { X86::UNPCKLPSrr, X86::UNPCKLPSrm }, 634 { X86::XOR16rr, X86::XOR16rm }, 635 { X86::XOR32rr, X86::XOR32rm }, 636 { X86::XOR64rr, X86::XOR64rm }, 637 { X86::XOR8rr, X86::XOR8rm }, 638 { X86::XORPDrr, X86::XORPDrm }, 639 { X86::XORPSrr, X86::XORPSrm } 640 }; 641 642 for (unsigned i = 0, e = array_lengthof(OpTbl2); i != e; ++i) { 643 unsigned RegOp = OpTbl2[i][0]; 644 unsigned MemOp = OpTbl2[i][1]; 645 if (!RegOp2MemOpTable2.insert(std::make_pair((unsigned*)RegOp, MemOp))) 646 assert(false && "Duplicated entries?"); 647 unsigned AuxInfo = 2 | (1 << 4); // Index 1, folded load 648 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp, 649 std::make_pair(RegOp, AuxInfo)))) 650 AmbEntries.push_back(MemOp); 651 } 652 653 // Remove ambiguous entries. 654 assert(AmbEntries.empty() && "Duplicated entries in unfolding maps?"); 655} 656 657// getDwarfRegNum - This function maps LLVM register identifiers to the 658// Dwarf specific numbering, used in debug info and exception tables. 659 660int X86RegisterInfo::getDwarfRegNum(unsigned RegNo, bool isEH) const { 661 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>(); 662 unsigned Flavour = DWARFFlavour::X86_64; 663 if (!Subtarget->is64Bit()) { 664 if (Subtarget->isTargetDarwin()) { 665 Flavour = DWARFFlavour::X86_32_Darwin; 666 } else if (Subtarget->isTargetCygMing()) { 667 // Unsupported by now, just quick fallback 668 Flavour = DWARFFlavour::X86_32_ELF; 669 } else { 670 Flavour = DWARFFlavour::X86_32_ELF; 671 } 672 } 673 674 return X86GenRegisterInfo::getDwarfRegNumFull(RegNo, Flavour); 675} 676 677// getX86RegNum - This function maps LLVM register identifiers to their X86 678// specific numbering, which is used in various places encoding instructions. 679// 680unsigned X86RegisterInfo::getX86RegNum(unsigned RegNo) { 681 switch(RegNo) { 682 case X86::RAX: case X86::EAX: case X86::AX: case X86::AL: return N86::EAX; 683 case X86::RCX: case X86::ECX: case X86::CX: case X86::CL: return N86::ECX; 684 case X86::RDX: case X86::EDX: case X86::DX: case X86::DL: return N86::EDX; 685 case X86::RBX: case X86::EBX: case X86::BX: case X86::BL: return N86::EBX; 686 case X86::RSP: case X86::ESP: case X86::SP: case X86::SPL: case X86::AH: 687 return N86::ESP; 688 case X86::RBP: case X86::EBP: case X86::BP: case X86::BPL: case X86::CH: 689 return N86::EBP; 690 case X86::RSI: case X86::ESI: case X86::SI: case X86::SIL: case X86::DH: 691 return N86::ESI; 692 case X86::RDI: case X86::EDI: case X86::DI: case X86::DIL: case X86::BH: 693 return N86::EDI; 694 695 case X86::R8: case X86::R8D: case X86::R8W: case X86::R8B: 696 return N86::EAX; 697 case X86::R9: case X86::R9D: case X86::R9W: case X86::R9B: 698 return N86::ECX; 699 case X86::R10: case X86::R10D: case X86::R10W: case X86::R10B: 700 return N86::EDX; 701 case X86::R11: case X86::R11D: case X86::R11W: case X86::R11B: 702 return N86::EBX; 703 case X86::R12: case X86::R12D: case X86::R12W: case X86::R12B: 704 return N86::ESP; 705 case X86::R13: case X86::R13D: case X86::R13W: case X86::R13B: 706 return N86::EBP; 707 case X86::R14: case X86::R14D: case X86::R14W: case X86::R14B: 708 return N86::ESI; 709 case X86::R15: case X86::R15D: case X86::R15W: case X86::R15B: 710 return N86::EDI; 711 712 case X86::ST0: case X86::ST1: case X86::ST2: case X86::ST3: 713 case X86::ST4: case X86::ST5: case X86::ST6: case X86::ST7: 714 return RegNo-X86::ST0; 715 716 case X86::XMM0: case X86::XMM8: case X86::MM0: 717 return 0; 718 case X86::XMM1: case X86::XMM9: case X86::MM1: 719 return 1; 720 case X86::XMM2: case X86::XMM10: case X86::MM2: 721 return 2; 722 case X86::XMM3: case X86::XMM11: case X86::MM3: 723 return 3; 724 case X86::XMM4: case X86::XMM12: case X86::MM4: 725 return 4; 726 case X86::XMM5: case X86::XMM13: case X86::MM5: 727 return 5; 728 case X86::XMM6: case X86::XMM14: case X86::MM6: 729 return 6; 730 case X86::XMM7: case X86::XMM15: case X86::MM7: 731 return 7; 732 733 default: 734 assert(isVirtualRegister(RegNo) && "Unknown physical register!"); 735 assert(0 && "Register allocator hasn't allocated reg correctly yet!"); 736 return 0; 737 } 738} 739 740bool X86RegisterInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 741 MachineBasicBlock::iterator MI, 742 const std::vector<CalleeSavedInfo> &CSI) const { 743 if (CSI.empty()) 744 return false; 745 746 MachineFunction &MF = *MBB.getParent(); 747 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 748 X86FI->setCalleeSavedFrameSize(CSI.size() * SlotSize); 749 unsigned Opc = Is64Bit ? X86::PUSH64r : X86::PUSH32r; 750 for (unsigned i = CSI.size(); i != 0; --i) { 751 unsigned Reg = CSI[i-1].getReg(); 752 // Add the callee-saved register as live-in. It's killed at the spill. 753 MBB.addLiveIn(Reg); 754 BuildMI(MBB, MI, TII.get(Opc)).addReg(Reg); 755 } 756 return true; 757} 758 759bool X86RegisterInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 760 MachineBasicBlock::iterator MI, 761 const std::vector<CalleeSavedInfo> &CSI) const { 762 if (CSI.empty()) 763 return false; 764 765 unsigned Opc = Is64Bit ? X86::POP64r : X86::POP32r; 766 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 767 unsigned Reg = CSI[i].getReg(); 768 BuildMI(MBB, MI, TII.get(Opc), Reg); 769 } 770 return true; 771} 772 773static const MachineInstrBuilder &X86InstrAddOperand(MachineInstrBuilder &MIB, 774 MachineOperand &MO) { 775 if (MO.isRegister()) 776 MIB = MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit(), 777 false, false, MO.getSubReg()); 778 else if (MO.isImmediate()) 779 MIB = MIB.addImm(MO.getImm()); 780 else if (MO.isFrameIndex()) 781 MIB = MIB.addFrameIndex(MO.getFrameIndex()); 782 else if (MO.isGlobalAddress()) 783 MIB = MIB.addGlobalAddress(MO.getGlobal(), MO.getOffset()); 784 else if (MO.isConstantPoolIndex()) 785 MIB = MIB.addConstantPoolIndex(MO.getConstantPoolIndex(), MO.getOffset()); 786 else if (MO.isJumpTableIndex()) 787 MIB = MIB.addJumpTableIndex(MO.getJumpTableIndex()); 788 else if (MO.isExternalSymbol()) 789 MIB = MIB.addExternalSymbol(MO.getSymbolName()); 790 else 791 assert(0 && "Unknown operand for X86InstrAddOperand!"); 792 793 return MIB; 794} 795 796static unsigned getStoreRegOpcode(const TargetRegisterClass *RC, 797 unsigned StackAlign) { 798 unsigned Opc = 0; 799 if (RC == &X86::GR64RegClass) { 800 Opc = X86::MOV64mr; 801 } else if (RC == &X86::GR32RegClass) { 802 Opc = X86::MOV32mr; 803 } else if (RC == &X86::GR16RegClass) { 804 Opc = X86::MOV16mr; 805 } else if (RC == &X86::GR8RegClass) { 806 Opc = X86::MOV8mr; 807 } else if (RC == &X86::GR32_RegClass) { 808 Opc = X86::MOV32_mr; 809 } else if (RC == &X86::GR16_RegClass) { 810 Opc = X86::MOV16_mr; 811 } else if (RC == &X86::RFP80RegClass) { 812 Opc = X86::ST_FpP80m; // pops 813 } else if (RC == &X86::RFP64RegClass) { 814 Opc = X86::ST_Fp64m; 815 } else if (RC == &X86::RFP32RegClass) { 816 Opc = X86::ST_Fp32m; 817 } else if (RC == &X86::FR32RegClass) { 818 Opc = X86::MOVSSmr; 819 } else if (RC == &X86::FR64RegClass) { 820 Opc = X86::MOVSDmr; 821 } else if (RC == &X86::VR128RegClass) { 822 // FIXME: Use movaps once we are capable of selectively 823 // aligning functions that spill SSE registers on 16-byte boundaries. 824 Opc = StackAlign >= 16 ? X86::MOVAPSmr : X86::MOVUPSmr; 825 } else if (RC == &X86::VR64RegClass) { 826 Opc = X86::MMX_MOVQ64mr; 827 } else { 828 assert(0 && "Unknown regclass"); 829 abort(); 830 } 831 832 return Opc; 833} 834 835void X86RegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 836 MachineBasicBlock::iterator MI, 837 unsigned SrcReg, bool isKill, int FrameIdx, 838 const TargetRegisterClass *RC) const { 839 unsigned Opc = getStoreRegOpcode(RC, StackAlign); 840 addFrameReference(BuildMI(MBB, MI, TII.get(Opc)), FrameIdx) 841 .addReg(SrcReg, false, false, isKill); 842} 843 844void X86RegisterInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg, 845 bool isKill, 846 SmallVectorImpl<MachineOperand> &Addr, 847 const TargetRegisterClass *RC, 848 SmallVectorImpl<MachineInstr*> &NewMIs) const { 849 unsigned Opc = getStoreRegOpcode(RC, StackAlign); 850 MachineInstrBuilder MIB = BuildMI(TII.get(Opc)); 851 for (unsigned i = 0, e = Addr.size(); i != e; ++i) 852 MIB = X86InstrAddOperand(MIB, Addr[i]); 853 MIB.addReg(SrcReg, false, false, isKill); 854 NewMIs.push_back(MIB); 855} 856 857static unsigned getLoadRegOpcode(const TargetRegisterClass *RC, 858 unsigned StackAlign) { 859 unsigned Opc = 0; 860 if (RC == &X86::GR64RegClass) { 861 Opc = X86::MOV64rm; 862 } else if (RC == &X86::GR32RegClass) { 863 Opc = X86::MOV32rm; 864 } else if (RC == &X86::GR16RegClass) { 865 Opc = X86::MOV16rm; 866 } else if (RC == &X86::GR8RegClass) { 867 Opc = X86::MOV8rm; 868 } else if (RC == &X86::GR32_RegClass) { 869 Opc = X86::MOV32_rm; 870 } else if (RC == &X86::GR16_RegClass) { 871 Opc = X86::MOV16_rm; 872 } else if (RC == &X86::RFP80RegClass) { 873 Opc = X86::LD_Fp80m; 874 } else if (RC == &X86::RFP64RegClass) { 875 Opc = X86::LD_Fp64m; 876 } else if (RC == &X86::RFP32RegClass) { 877 Opc = X86::LD_Fp32m; 878 } else if (RC == &X86::FR32RegClass) { 879 Opc = X86::MOVSSrm; 880 } else if (RC == &X86::FR64RegClass) { 881 Opc = X86::MOVSDrm; 882 } else if (RC == &X86::VR128RegClass) { 883 // FIXME: Use movaps once we are capable of selectively 884 // aligning functions that spill SSE registers on 16-byte boundaries. 885 Opc = StackAlign >= 16 ? X86::MOVAPSrm : X86::MOVUPSrm; 886 } else if (RC == &X86::VR64RegClass) { 887 Opc = X86::MMX_MOVQ64rm; 888 } else { 889 assert(0 && "Unknown regclass"); 890 abort(); 891 } 892 893 return Opc; 894} 895 896void X86RegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 897 MachineBasicBlock::iterator MI, 898 unsigned DestReg, int FrameIdx, 899 const TargetRegisterClass *RC) const{ 900 unsigned Opc = getLoadRegOpcode(RC, StackAlign); 901 addFrameReference(BuildMI(MBB, MI, TII.get(Opc), DestReg), FrameIdx); 902} 903 904void X86RegisterInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg, 905 SmallVectorImpl<MachineOperand> &Addr, 906 const TargetRegisterClass *RC, 907 SmallVectorImpl<MachineInstr*> &NewMIs) const { 908 unsigned Opc = getLoadRegOpcode(RC, StackAlign); 909 MachineInstrBuilder MIB = BuildMI(TII.get(Opc), DestReg); 910 for (unsigned i = 0, e = Addr.size(); i != e; ++i) 911 MIB = X86InstrAddOperand(MIB, Addr[i]); 912 NewMIs.push_back(MIB); 913} 914 915void X86RegisterInfo::copyRegToReg(MachineBasicBlock &MBB, 916 MachineBasicBlock::iterator MI, 917 unsigned DestReg, unsigned SrcReg, 918 const TargetRegisterClass *DestRC, 919 const TargetRegisterClass *SrcRC) const { 920 if (DestRC != SrcRC) { 921 // Moving EFLAGS to / from another register requires a push and a pop. 922 if (SrcRC == &X86::CCRRegClass) { 923 assert(SrcReg == X86::EFLAGS); 924 if (DestRC == &X86::GR64RegClass) { 925 BuildMI(MBB, MI, TII.get(X86::PUSHFQ)); 926 BuildMI(MBB, MI, TII.get(X86::POP64r), DestReg); 927 return; 928 } else if (DestRC == &X86::GR32RegClass) { 929 BuildMI(MBB, MI, TII.get(X86::PUSHFD)); 930 BuildMI(MBB, MI, TII.get(X86::POP32r), DestReg); 931 return; 932 } 933 } else if (DestRC == &X86::CCRRegClass) { 934 assert(DestReg == X86::EFLAGS); 935 if (SrcRC == &X86::GR64RegClass) { 936 BuildMI(MBB, MI, TII.get(X86::PUSH64r)).addReg(SrcReg); 937 BuildMI(MBB, MI, TII.get(X86::POPFQ)); 938 return; 939 } else if (SrcRC == &X86::GR32RegClass) { 940 BuildMI(MBB, MI, TII.get(X86::PUSH32r)).addReg(SrcReg); 941 BuildMI(MBB, MI, TII.get(X86::POPFD)); 942 return; 943 } 944 } 945 cerr << "Not yet supported!"; 946 abort(); 947 } 948 949 unsigned Opc; 950 if (DestRC == &X86::GR64RegClass) { 951 Opc = X86::MOV64rr; 952 } else if (DestRC == &X86::GR32RegClass) { 953 Opc = X86::MOV32rr; 954 } else if (DestRC == &X86::GR16RegClass) { 955 Opc = X86::MOV16rr; 956 } else if (DestRC == &X86::GR8RegClass) { 957 Opc = X86::MOV8rr; 958 } else if (DestRC == &X86::GR32_RegClass) { 959 Opc = X86::MOV32_rr; 960 } else if (DestRC == &X86::GR16_RegClass) { 961 Opc = X86::MOV16_rr; 962 } else if (DestRC == &X86::RFP32RegClass) { 963 Opc = X86::MOV_Fp3232; 964 } else if (DestRC == &X86::RFP64RegClass || DestRC == &X86::RSTRegClass) { 965 Opc = X86::MOV_Fp6464; 966 } else if (DestRC == &X86::RFP80RegClass) { 967 Opc = X86::MOV_Fp8080; 968 } else if (DestRC == &X86::FR32RegClass) { 969 Opc = X86::FsMOVAPSrr; 970 } else if (DestRC == &X86::FR64RegClass) { 971 Opc = X86::FsMOVAPDrr; 972 } else if (DestRC == &X86::VR128RegClass) { 973 Opc = X86::MOVAPSrr; 974 } else if (DestRC == &X86::VR64RegClass) { 975 Opc = X86::MMX_MOVQ64rr; 976 } else { 977 assert(0 && "Unknown regclass"); 978 abort(); 979 } 980 BuildMI(MBB, MI, TII.get(Opc), DestReg).addReg(SrcReg); 981} 982 983const TargetRegisterClass * 984X86RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const { 985 if (RC == &X86::CCRRegClass) 986 if (Is64Bit) 987 return &X86::GR64RegClass; 988 else 989 return &X86::GR32RegClass; 990 return NULL; 991} 992 993void X86RegisterInfo::reMaterialize(MachineBasicBlock &MBB, 994 MachineBasicBlock::iterator I, 995 unsigned DestReg, 996 const MachineInstr *Orig) const { 997 // MOV32r0 etc. are implemented with xor which clobbers condition code. 998 // Re-materialize them as movri instructions to avoid side effects. 999 switch (Orig->getOpcode()) { 1000 case X86::MOV8r0: 1001 BuildMI(MBB, I, TII.get(X86::MOV8ri), DestReg).addImm(0); 1002 break; 1003 case X86::MOV16r0: 1004 BuildMI(MBB, I, TII.get(X86::MOV16ri), DestReg).addImm(0); 1005 break; 1006 case X86::MOV32r0: 1007 BuildMI(MBB, I, TII.get(X86::MOV32ri), DestReg).addImm(0); 1008 break; 1009 case X86::MOV64r0: 1010 BuildMI(MBB, I, TII.get(X86::MOV64ri32), DestReg).addImm(0); 1011 break; 1012 default: { 1013 MachineInstr *MI = Orig->clone(); 1014 MI->getOperand(0).setReg(DestReg); 1015 MBB.insert(I, MI); 1016 break; 1017 } 1018 } 1019} 1020 1021static MachineInstr *FuseTwoAddrInst(unsigned Opcode, 1022 SmallVector<MachineOperand,4> &MOs, 1023 MachineInstr *MI, const TargetInstrInfo &TII) { 1024 // Create the base instruction with the memory operand as the first part. 1025 MachineInstr *NewMI = new MachineInstr(TII.get(Opcode), true); 1026 MachineInstrBuilder MIB(NewMI); 1027 unsigned NumAddrOps = MOs.size(); 1028 for (unsigned i = 0; i != NumAddrOps; ++i) 1029 MIB = X86InstrAddOperand(MIB, MOs[i]); 1030 if (NumAddrOps < 4) // FrameIndex only 1031 MIB.addImm(1).addReg(0).addImm(0); 1032 1033 // Loop over the rest of the ri operands, converting them over. 1034 unsigned NumOps = TII.getNumOperands(MI->getOpcode())-2; 1035 for (unsigned i = 0; i != NumOps; ++i) { 1036 MachineOperand &MO = MI->getOperand(i+2); 1037 MIB = X86InstrAddOperand(MIB, MO); 1038 } 1039 for (unsigned i = NumOps+2, e = MI->getNumOperands(); i != e; ++i) { 1040 MachineOperand &MO = MI->getOperand(i); 1041 MIB = X86InstrAddOperand(MIB, MO); 1042 } 1043 return MIB; 1044} 1045 1046static MachineInstr *FuseInst(unsigned Opcode, unsigned OpNo, 1047 SmallVector<MachineOperand,4> &MOs, 1048 MachineInstr *MI, const TargetInstrInfo &TII) { 1049 MachineInstr *NewMI = new MachineInstr(TII.get(Opcode), true); 1050 MachineInstrBuilder MIB(NewMI); 1051 1052 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1053 MachineOperand &MO = MI->getOperand(i); 1054 if (i == OpNo) { 1055 assert(MO.isRegister() && "Expected to fold into reg operand!"); 1056 unsigned NumAddrOps = MOs.size(); 1057 for (unsigned i = 0; i != NumAddrOps; ++i) 1058 MIB = X86InstrAddOperand(MIB, MOs[i]); 1059 if (NumAddrOps < 4) // FrameIndex only 1060 MIB.addImm(1).addReg(0).addImm(0); 1061 } else { 1062 MIB = X86InstrAddOperand(MIB, MO); 1063 } 1064 } 1065 return MIB; 1066} 1067 1068static MachineInstr *MakeM0Inst(const TargetInstrInfo &TII, unsigned Opcode, 1069 SmallVector<MachineOperand,4> &MOs, 1070 MachineInstr *MI) { 1071 MachineInstrBuilder MIB = BuildMI(TII.get(Opcode)); 1072 1073 unsigned NumAddrOps = MOs.size(); 1074 for (unsigned i = 0; i != NumAddrOps; ++i) 1075 MIB = X86InstrAddOperand(MIB, MOs[i]); 1076 if (NumAddrOps < 4) // FrameIndex only 1077 MIB.addImm(1).addReg(0).addImm(0); 1078 return MIB.addImm(0); 1079} 1080 1081MachineInstr* 1082X86RegisterInfo::foldMemoryOperand(MachineInstr *MI, unsigned i, 1083 SmallVector<MachineOperand,4> &MOs) const { 1084 const DenseMap<unsigned*, unsigned> *OpcodeTablePtr = NULL; 1085 bool isTwoAddrFold = false; 1086 unsigned NumOps = TII.getNumOperands(MI->getOpcode()); 1087 bool isTwoAddr = NumOps > 1 && 1088 MI->getInstrDescriptor()->getOperandConstraint(1, TOI::TIED_TO) != -1; 1089 1090 MachineInstr *NewMI = NULL; 1091 // Folding a memory location into the two-address part of a two-address 1092 // instruction is different than folding it other places. It requires 1093 // replacing the *two* registers with the memory location. 1094 if (isTwoAddr && NumOps >= 2 && i < 2 && 1095 MI->getOperand(0).isRegister() && 1096 MI->getOperand(1).isRegister() && 1097 MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) { 1098 OpcodeTablePtr = &RegOp2MemOpTable2Addr; 1099 isTwoAddrFold = true; 1100 } else if (i == 0) { // If operand 0 1101 if (MI->getOpcode() == X86::MOV16r0) 1102 NewMI = MakeM0Inst(TII, X86::MOV16mi, MOs, MI); 1103 else if (MI->getOpcode() == X86::MOV32r0) 1104 NewMI = MakeM0Inst(TII, X86::MOV32mi, MOs, MI); 1105 else if (MI->getOpcode() == X86::MOV64r0) 1106 NewMI = MakeM0Inst(TII, X86::MOV64mi32, MOs, MI); 1107 else if (MI->getOpcode() == X86::MOV8r0) 1108 NewMI = MakeM0Inst(TII, X86::MOV8mi, MOs, MI); 1109 if (NewMI) { 1110 NewMI->copyKillDeadInfo(MI); 1111 return NewMI; 1112 } 1113 1114 OpcodeTablePtr = &RegOp2MemOpTable0; 1115 } else if (i == 1) { 1116 OpcodeTablePtr = &RegOp2MemOpTable1; 1117 } else if (i == 2) { 1118 OpcodeTablePtr = &RegOp2MemOpTable2; 1119 } 1120 1121 // If table selected... 1122 if (OpcodeTablePtr) { 1123 // Find the Opcode to fuse 1124 DenseMap<unsigned*, unsigned>::iterator I = 1125 OpcodeTablePtr->find((unsigned*)MI->getOpcode()); 1126 if (I != OpcodeTablePtr->end()) { 1127 if (isTwoAddrFold) 1128 NewMI = FuseTwoAddrInst(I->second, MOs, MI, TII); 1129 else 1130 NewMI = FuseInst(I->second, i, MOs, MI, TII); 1131 NewMI->copyKillDeadInfo(MI); 1132 return NewMI; 1133 } 1134 } 1135 1136 // No fusion 1137 if (PrintFailedFusing) 1138 cerr << "We failed to fuse (" 1139 << ((i == 1) ? "r" : "s") << "): " << *MI; 1140 return NULL; 1141} 1142 1143 1144MachineInstr* X86RegisterInfo::foldMemoryOperand(MachineInstr *MI, 1145 SmallVectorImpl<unsigned> &Ops, 1146 int FrameIndex) const { 1147 // Check switch flag 1148 if (NoFusing) return NULL; 1149 1150 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) { 1151 unsigned NewOpc = 0; 1152 switch (MI->getOpcode()) { 1153 default: return NULL; 1154 case X86::TEST8rr: NewOpc = X86::CMP8ri; break; 1155 case X86::TEST16rr: NewOpc = X86::CMP16ri; break; 1156 case X86::TEST32rr: NewOpc = X86::CMP32ri; break; 1157 case X86::TEST64rr: NewOpc = X86::CMP64ri32; break; 1158 } 1159 // Change to CMPXXri r, 0 first. 1160 MI->setInstrDescriptor(TII.get(NewOpc)); 1161 MI->getOperand(1).ChangeToImmediate(0); 1162 } else if (Ops.size() != 1) 1163 return NULL; 1164 1165 SmallVector<MachineOperand,4> MOs; 1166 MOs.push_back(MachineOperand::CreateFrameIndex(FrameIndex)); 1167 return foldMemoryOperand(MI, Ops[0], MOs); 1168} 1169 1170MachineInstr* X86RegisterInfo::foldMemoryOperand(MachineInstr *MI, 1171 SmallVectorImpl<unsigned> &Ops, 1172 MachineInstr *LoadMI) const { 1173 // Check switch flag 1174 if (NoFusing) return NULL; 1175 1176 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) { 1177 unsigned NewOpc = 0; 1178 switch (MI->getOpcode()) { 1179 default: return NULL; 1180 case X86::TEST8rr: NewOpc = X86::CMP8ri; break; 1181 case X86::TEST16rr: NewOpc = X86::CMP16ri; break; 1182 case X86::TEST32rr: NewOpc = X86::CMP32ri; break; 1183 case X86::TEST64rr: NewOpc = X86::CMP64ri32; break; 1184 } 1185 // Change to CMPXXri r, 0 first. 1186 MI->setInstrDescriptor(TII.get(NewOpc)); 1187 MI->getOperand(1).ChangeToImmediate(0); 1188 } else if (Ops.size() != 1) 1189 return NULL; 1190 1191 SmallVector<MachineOperand,4> MOs; 1192 unsigned NumOps = TII.getNumOperands(LoadMI->getOpcode()); 1193 for (unsigned i = NumOps - 4; i != NumOps; ++i) 1194 MOs.push_back(LoadMI->getOperand(i)); 1195 return foldMemoryOperand(MI, Ops[0], MOs); 1196} 1197 1198 1199bool X86RegisterInfo::canFoldMemoryOperand(MachineInstr *MI, 1200 SmallVectorImpl<unsigned> &Ops) const { 1201 // Check switch flag 1202 if (NoFusing) return 0; 1203 1204 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) { 1205 switch (MI->getOpcode()) { 1206 default: return false; 1207 case X86::TEST8rr: 1208 case X86::TEST16rr: 1209 case X86::TEST32rr: 1210 case X86::TEST64rr: 1211 return true; 1212 } 1213 } 1214 1215 if (Ops.size() != 1) 1216 return false; 1217 1218 unsigned OpNum = Ops[0]; 1219 unsigned Opc = MI->getOpcode(); 1220 unsigned NumOps = TII.getNumOperands(Opc); 1221 bool isTwoAddr = NumOps > 1 && 1222 TII.getOperandConstraint(Opc, 1, TOI::TIED_TO) != -1; 1223 1224 // Folding a memory location into the two-address part of a two-address 1225 // instruction is different than folding it other places. It requires 1226 // replacing the *two* registers with the memory location. 1227 const DenseMap<unsigned*, unsigned> *OpcodeTablePtr = NULL; 1228 if (isTwoAddr && NumOps >= 2 && OpNum < 2) { 1229 OpcodeTablePtr = &RegOp2MemOpTable2Addr; 1230 } else if (OpNum == 0) { // If operand 0 1231 switch (Opc) { 1232 case X86::MOV16r0: 1233 case X86::MOV32r0: 1234 case X86::MOV64r0: 1235 case X86::MOV8r0: 1236 return true; 1237 default: break; 1238 } 1239 OpcodeTablePtr = &RegOp2MemOpTable0; 1240 } else if (OpNum == 1) { 1241 OpcodeTablePtr = &RegOp2MemOpTable1; 1242 } else if (OpNum == 2) { 1243 OpcodeTablePtr = &RegOp2MemOpTable2; 1244 } 1245 1246 if (OpcodeTablePtr) { 1247 // Find the Opcode to fuse 1248 DenseMap<unsigned*, unsigned>::iterator I = 1249 OpcodeTablePtr->find((unsigned*)Opc); 1250 if (I != OpcodeTablePtr->end()) 1251 return true; 1252 } 1253 return false; 1254} 1255 1256bool X86RegisterInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI, 1257 unsigned Reg, bool UnfoldLoad, bool UnfoldStore, 1258 SmallVectorImpl<MachineInstr*> &NewMIs) const { 1259 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I = 1260 MemOp2RegOpTable.find((unsigned*)MI->getOpcode()); 1261 if (I == MemOp2RegOpTable.end()) 1262 return false; 1263 unsigned Opc = I->second.first; 1264 unsigned Index = I->second.second & 0xf; 1265 bool FoldedLoad = I->second.second & (1 << 4); 1266 bool FoldedStore = I->second.second & (1 << 5); 1267 if (UnfoldLoad && !FoldedLoad) 1268 return false; 1269 UnfoldLoad &= FoldedLoad; 1270 if (UnfoldStore && !FoldedStore) 1271 return false; 1272 UnfoldStore &= FoldedStore; 1273 1274 const TargetInstrDescriptor &TID = TII.get(Opc); 1275 const TargetOperandInfo &TOI = TID.OpInfo[Index]; 1276 const TargetRegisterClass *RC = (TOI.Flags & M_LOOK_UP_PTR_REG_CLASS) 1277 ? TII.getPointerRegClass() : getRegClass(TOI.RegClass); 1278 SmallVector<MachineOperand,4> AddrOps; 1279 SmallVector<MachineOperand,2> BeforeOps; 1280 SmallVector<MachineOperand,2> AfterOps; 1281 SmallVector<MachineOperand,4> ImpOps; 1282 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1283 MachineOperand &Op = MI->getOperand(i); 1284 if (i >= Index && i < Index+4) 1285 AddrOps.push_back(Op); 1286 else if (Op.isRegister() && Op.isImplicit()) 1287 ImpOps.push_back(Op); 1288 else if (i < Index) 1289 BeforeOps.push_back(Op); 1290 else if (i > Index) 1291 AfterOps.push_back(Op); 1292 } 1293 1294 // Emit the load instruction. 1295 if (UnfoldLoad) { 1296 loadRegFromAddr(MF, Reg, AddrOps, RC, NewMIs); 1297 if (UnfoldStore) { 1298 // Address operands cannot be marked isKill. 1299 for (unsigned i = 1; i != 5; ++i) { 1300 MachineOperand &MO = NewMIs[0]->getOperand(i); 1301 if (MO.isRegister()) 1302 MO.unsetIsKill(); 1303 } 1304 } 1305 } 1306 1307 // Emit the data processing instruction. 1308 MachineInstr *DataMI = new MachineInstr(TID, true); 1309 MachineInstrBuilder MIB(DataMI); 1310 1311 if (FoldedStore) 1312 MIB.addReg(Reg, true); 1313 for (unsigned i = 0, e = BeforeOps.size(); i != e; ++i) 1314 MIB = X86InstrAddOperand(MIB, BeforeOps[i]); 1315 if (FoldedLoad) 1316 MIB.addReg(Reg); 1317 for (unsigned i = 0, e = AfterOps.size(); i != e; ++i) 1318 MIB = X86InstrAddOperand(MIB, AfterOps[i]); 1319 for (unsigned i = 0, e = ImpOps.size(); i != e; ++i) { 1320 MachineOperand &MO = ImpOps[i]; 1321 MIB.addReg(MO.getReg(), MO.isDef(), true, MO.isKill(), MO.isDead()); 1322 } 1323 // Change CMP32ri r, 0 back to TEST32rr r, r, etc. 1324 unsigned NewOpc = 0; 1325 switch (DataMI->getOpcode()) { 1326 default: break; 1327 case X86::CMP64ri32: 1328 case X86::CMP32ri: 1329 case X86::CMP16ri: 1330 case X86::CMP8ri: { 1331 MachineOperand &MO0 = DataMI->getOperand(0); 1332 MachineOperand &MO1 = DataMI->getOperand(1); 1333 if (MO1.getImm() == 0) { 1334 switch (DataMI->getOpcode()) { 1335 default: break; 1336 case X86::CMP64ri32: NewOpc = X86::TEST64rr; break; 1337 case X86::CMP32ri: NewOpc = X86::TEST32rr; break; 1338 case X86::CMP16ri: NewOpc = X86::TEST16rr; break; 1339 case X86::CMP8ri: NewOpc = X86::TEST8rr; break; 1340 } 1341 DataMI->setInstrDescriptor(TII.get(NewOpc)); 1342 MO1.ChangeToRegister(MO0.getReg(), false); 1343 } 1344 } 1345 } 1346 NewMIs.push_back(DataMI); 1347 1348 // Emit the store instruction. 1349 if (UnfoldStore) { 1350 const TargetOperandInfo &DstTOI = TID.OpInfo[0]; 1351 const TargetRegisterClass *DstRC = (DstTOI.Flags & M_LOOK_UP_PTR_REG_CLASS) 1352 ? TII.getPointerRegClass() : getRegClass(DstTOI.RegClass); 1353 storeRegToAddr(MF, Reg, true, AddrOps, DstRC, NewMIs); 1354 } 1355 1356 return true; 1357} 1358 1359 1360bool 1361X86RegisterInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N, 1362 SmallVectorImpl<SDNode*> &NewNodes) const { 1363 if (!N->isTargetOpcode()) 1364 return false; 1365 1366 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I = 1367 MemOp2RegOpTable.find((unsigned*)N->getTargetOpcode()); 1368 if (I == MemOp2RegOpTable.end()) 1369 return false; 1370 unsigned Opc = I->second.first; 1371 unsigned Index = I->second.second & 0xf; 1372 bool FoldedLoad = I->second.second & (1 << 4); 1373 bool FoldedStore = I->second.second & (1 << 5); 1374 const TargetInstrDescriptor &TID = TII.get(Opc); 1375 const TargetOperandInfo &TOI = TID.OpInfo[Index]; 1376 const TargetRegisterClass *RC = (TOI.Flags & M_LOOK_UP_PTR_REG_CLASS) 1377 ? TII.getPointerRegClass() : getRegClass(TOI.RegClass); 1378 std::vector<SDOperand> AddrOps; 1379 std::vector<SDOperand> BeforeOps; 1380 std::vector<SDOperand> AfterOps; 1381 unsigned NumOps = N->getNumOperands(); 1382 for (unsigned i = 0; i != NumOps-1; ++i) { 1383 SDOperand Op = N->getOperand(i); 1384 if (i >= Index && i < Index+4) 1385 AddrOps.push_back(Op); 1386 else if (i < Index) 1387 BeforeOps.push_back(Op); 1388 else if (i > Index) 1389 AfterOps.push_back(Op); 1390 } 1391 SDOperand Chain = N->getOperand(NumOps-1); 1392 AddrOps.push_back(Chain); 1393 1394 // Emit the load instruction. 1395 SDNode *Load = 0; 1396 if (FoldedLoad) { 1397 MVT::ValueType VT = *RC->vt_begin(); 1398 Load = DAG.getTargetNode(getLoadRegOpcode(RC, StackAlign), VT, MVT::Other, 1399 &AddrOps[0], AddrOps.size()); 1400 NewNodes.push_back(Load); 1401 } 1402 1403 // Emit the data processing instruction. 1404 std::vector<MVT::ValueType> VTs; 1405 const TargetRegisterClass *DstRC = 0; 1406 if (TID.numDefs > 0) { 1407 const TargetOperandInfo &DstTOI = TID.OpInfo[0]; 1408 DstRC = (DstTOI.Flags & M_LOOK_UP_PTR_REG_CLASS) 1409 ? TII.getPointerRegClass() : getRegClass(DstTOI.RegClass); 1410 VTs.push_back(*DstRC->vt_begin()); 1411 } 1412 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) { 1413 MVT::ValueType VT = N->getValueType(i); 1414 if (VT != MVT::Other && i >= TID.numDefs) 1415 VTs.push_back(VT); 1416 } 1417 if (Load) 1418 BeforeOps.push_back(SDOperand(Load, 0)); 1419 std::copy(AfterOps.begin(), AfterOps.end(), std::back_inserter(BeforeOps)); 1420 SDNode *NewNode= DAG.getTargetNode(Opc, VTs, &BeforeOps[0], BeforeOps.size()); 1421 NewNodes.push_back(NewNode); 1422 1423 // Emit the store instruction. 1424 if (FoldedStore) { 1425 AddrOps.pop_back(); 1426 AddrOps.push_back(SDOperand(NewNode, 0)); 1427 AddrOps.push_back(Chain); 1428 SDNode *Store = DAG.getTargetNode(getStoreRegOpcode(DstRC, StackAlign), 1429 MVT::Other, &AddrOps[0], AddrOps.size()); 1430 NewNodes.push_back(Store); 1431 } 1432 1433 return true; 1434} 1435 1436unsigned X86RegisterInfo::getOpcodeAfterMemoryUnfold(unsigned Opc, 1437 bool UnfoldLoad, bool UnfoldStore) const { 1438 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I = 1439 MemOp2RegOpTable.find((unsigned*)Opc); 1440 if (I == MemOp2RegOpTable.end()) 1441 return 0; 1442 bool FoldedLoad = I->second.second & (1 << 4); 1443 bool FoldedStore = I->second.second & (1 << 5); 1444 if (UnfoldLoad && !FoldedLoad) 1445 return 0; 1446 if (UnfoldStore && !FoldedStore) 1447 return 0; 1448 return I->second.first; 1449} 1450 1451const unsigned * 1452X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 1453 static const unsigned CalleeSavedRegs32Bit[] = { 1454 X86::ESI, X86::EDI, X86::EBX, X86::EBP, 0 1455 }; 1456 1457 static const unsigned CalleeSavedRegs32EHRet[] = { 1458 X86::EAX, X86::EDX, X86::ESI, X86::EDI, X86::EBX, X86::EBP, 0 1459 }; 1460 1461 static const unsigned CalleeSavedRegs64Bit[] = { 1462 X86::RBX, X86::R12, X86::R13, X86::R14, X86::R15, X86::RBP, 0 1463 }; 1464 1465 if (Is64Bit) 1466 return CalleeSavedRegs64Bit; 1467 else { 1468 if (MF) { 1469 MachineFrameInfo *MFI = MF->getFrameInfo(); 1470 MachineModuleInfo *MMI = MFI->getMachineModuleInfo(); 1471 if (MMI && MMI->callsEHReturn()) 1472 return CalleeSavedRegs32EHRet; 1473 } 1474 return CalleeSavedRegs32Bit; 1475 } 1476} 1477 1478const TargetRegisterClass* const* 1479X86RegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const { 1480 static const TargetRegisterClass * const CalleeSavedRegClasses32Bit[] = { 1481 &X86::GR32RegClass, &X86::GR32RegClass, 1482 &X86::GR32RegClass, &X86::GR32RegClass, 0 1483 }; 1484 static const TargetRegisterClass * const CalleeSavedRegClasses32EHRet[] = { 1485 &X86::GR32RegClass, &X86::GR32RegClass, 1486 &X86::GR32RegClass, &X86::GR32RegClass, 1487 &X86::GR32RegClass, &X86::GR32RegClass, 0 1488 }; 1489 static const TargetRegisterClass * const CalleeSavedRegClasses64Bit[] = { 1490 &X86::GR64RegClass, &X86::GR64RegClass, 1491 &X86::GR64RegClass, &X86::GR64RegClass, 1492 &X86::GR64RegClass, &X86::GR64RegClass, 0 1493 }; 1494 1495 if (Is64Bit) 1496 return CalleeSavedRegClasses64Bit; 1497 else { 1498 if (MF) { 1499 MachineFrameInfo *MFI = MF->getFrameInfo(); 1500 MachineModuleInfo *MMI = MFI->getMachineModuleInfo(); 1501 if (MMI && MMI->callsEHReturn()) 1502 return CalleeSavedRegClasses32EHRet; 1503 } 1504 return CalleeSavedRegClasses32Bit; 1505 } 1506 1507} 1508 1509BitVector X86RegisterInfo::getReservedRegs(const MachineFunction &MF) const { 1510 BitVector Reserved(getNumRegs()); 1511 Reserved.set(X86::RSP); 1512 Reserved.set(X86::ESP); 1513 Reserved.set(X86::SP); 1514 Reserved.set(X86::SPL); 1515 if (hasFP(MF)) { 1516 Reserved.set(X86::RBP); 1517 Reserved.set(X86::EBP); 1518 Reserved.set(X86::BP); 1519 Reserved.set(X86::BPL); 1520 } 1521 return Reserved; 1522} 1523 1524//===----------------------------------------------------------------------===// 1525// Stack Frame Processing methods 1526//===----------------------------------------------------------------------===// 1527 1528// hasFP - Return true if the specified function should have a dedicated frame 1529// pointer register. This is true if the function has variable sized allocas or 1530// if frame pointer elimination is disabled. 1531// 1532bool X86RegisterInfo::hasFP(const MachineFunction &MF) const { 1533 MachineFrameInfo *MFI = MF.getFrameInfo(); 1534 MachineModuleInfo *MMI = MFI->getMachineModuleInfo(); 1535 1536 return (NoFramePointerElim || 1537 MFI->hasVarSizedObjects() || 1538 MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() || 1539 (MMI && MMI->callsUnwindInit())); 1540} 1541 1542bool X86RegisterInfo::hasReservedCallFrame(MachineFunction &MF) const { 1543 return !MF.getFrameInfo()->hasVarSizedObjects(); 1544} 1545 1546void X86RegisterInfo:: 1547eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 1548 MachineBasicBlock::iterator I) const { 1549 if (!hasReservedCallFrame(MF)) { 1550 // If the stack pointer can be changed after prologue, turn the 1551 // adjcallstackup instruction into a 'sub ESP, <amt>' and the 1552 // adjcallstackdown instruction into 'add ESP, <amt>' 1553 // TODO: consider using push / pop instead of sub + store / add 1554 MachineInstr *Old = I; 1555 uint64_t Amount = Old->getOperand(0).getImm(); 1556 if (Amount != 0) { 1557 // We need to keep the stack aligned properly. To do this, we round the 1558 // amount of space needed for the outgoing arguments up to the next 1559 // alignment boundary. 1560 Amount = (Amount+StackAlign-1)/StackAlign*StackAlign; 1561 1562 MachineInstr *New = 0; 1563 if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) { 1564 New=BuildMI(TII.get(Is64Bit ? X86::SUB64ri32 : X86::SUB32ri), StackPtr) 1565 .addReg(StackPtr).addImm(Amount); 1566 } else { 1567 assert(Old->getOpcode() == X86::ADJCALLSTACKUP); 1568 // factor out the amount the callee already popped. 1569 uint64_t CalleeAmt = Old->getOperand(1).getImm(); 1570 Amount -= CalleeAmt; 1571 if (Amount) { 1572 unsigned Opc = (Amount < 128) ? 1573 (Is64Bit ? X86::ADD64ri8 : X86::ADD32ri8) : 1574 (Is64Bit ? X86::ADD64ri32 : X86::ADD32ri); 1575 New = BuildMI(TII.get(Opc), StackPtr).addReg(StackPtr).addImm(Amount); 1576 } 1577 } 1578 1579 // Replace the pseudo instruction with a new instruction... 1580 if (New) MBB.insert(I, New); 1581 } 1582 } else if (I->getOpcode() == X86::ADJCALLSTACKUP) { 1583 // If we are performing frame pointer elimination and if the callee pops 1584 // something off the stack pointer, add it back. We do this until we have 1585 // more advanced stack pointer tracking ability. 1586 if (uint64_t CalleeAmt = I->getOperand(1).getImm()) { 1587 unsigned Opc = (CalleeAmt < 128) ? 1588 (Is64Bit ? X86::SUB64ri8 : X86::SUB32ri8) : 1589 (Is64Bit ? X86::SUB64ri32 : X86::SUB32ri); 1590 MachineInstr *New = 1591 BuildMI(TII.get(Opc), StackPtr).addReg(StackPtr).addImm(CalleeAmt); 1592 MBB.insert(I, New); 1593 } 1594 } 1595 1596 MBB.erase(I); 1597} 1598 1599void X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 1600 int SPAdj, RegScavenger *RS) const{ 1601 assert(SPAdj == 0 && "Unexpected"); 1602 1603 unsigned i = 0; 1604 MachineInstr &MI = *II; 1605 MachineFunction &MF = *MI.getParent()->getParent(); 1606 while (!MI.getOperand(i).isFrameIndex()) { 1607 ++i; 1608 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 1609 } 1610 1611 int FrameIndex = MI.getOperand(i).getFrameIndex(); 1612 // This must be part of a four operand memory reference. Replace the 1613 // FrameIndex with base register with EBP. Add an offset to the offset. 1614 MI.getOperand(i).ChangeToRegister(hasFP(MF) ? FramePtr : StackPtr, false); 1615 1616 // Now add the frame object offset to the offset from EBP. 1617 int64_t Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) + 1618 MI.getOperand(i+3).getImm()+SlotSize; 1619 1620 if (!hasFP(MF)) 1621 Offset += MF.getFrameInfo()->getStackSize(); 1622 else { 1623 Offset += SlotSize; // Skip the saved EBP 1624 // Skip the RETADDR move area 1625 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 1626 int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); 1627 if (TailCallReturnAddrDelta < 0) Offset -= TailCallReturnAddrDelta; 1628 } 1629 1630 MI.getOperand(i+3).ChangeToImmediate(Offset); 1631} 1632 1633void 1634X86RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF) const{ 1635 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 1636 int32_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); 1637 if (TailCallReturnAddrDelta < 0) { 1638 // create RETURNADDR area 1639 // arg 1640 // arg 1641 // RETADDR 1642 // { ... 1643 // RETADDR area 1644 // ... 1645 // } 1646 // [EBP] 1647 MF.getFrameInfo()-> 1648 CreateFixedObject(-TailCallReturnAddrDelta, 1649 (-1*SlotSize)+TailCallReturnAddrDelta); 1650 } 1651 if (hasFP(MF)) { 1652 assert((TailCallReturnAddrDelta <= 0) && 1653 "The Delta should always be zero or negative"); 1654 // Create a frame entry for the EBP register that must be saved. 1655 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, 1656 (int)SlotSize * -2+ 1657 TailCallReturnAddrDelta); 1658 assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() && 1659 "Slot for EBP register must be last in order to be found!"); 1660 } 1661} 1662 1663/// emitSPUpdate - Emit a series of instructions to increment / decrement the 1664/// stack pointer by a constant value. 1665static 1666void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, 1667 unsigned StackPtr, int64_t NumBytes, bool Is64Bit, 1668 const TargetInstrInfo &TII) { 1669 bool isSub = NumBytes < 0; 1670 uint64_t Offset = isSub ? -NumBytes : NumBytes; 1671 unsigned Opc = isSub 1672 ? ((Offset < 128) ? 1673 (Is64Bit ? X86::SUB64ri8 : X86::SUB32ri8) : 1674 (Is64Bit ? X86::SUB64ri32 : X86::SUB32ri)) 1675 : ((Offset < 128) ? 1676 (Is64Bit ? X86::ADD64ri8 : X86::ADD32ri8) : 1677 (Is64Bit ? X86::ADD64ri32 : X86::ADD32ri)); 1678 uint64_t Chunk = (1LL << 31) - 1; 1679 1680 while (Offset) { 1681 uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset; 1682 BuildMI(MBB, MBBI, TII.get(Opc), StackPtr).addReg(StackPtr).addImm(ThisVal); 1683 Offset -= ThisVal; 1684 } 1685} 1686 1687// mergeSPUpdatesUp - Merge two stack-manipulating instructions upper iterator. 1688static 1689void mergeSPUpdatesUp(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, 1690 unsigned StackPtr, uint64_t *NumBytes = NULL) { 1691 if (MBBI == MBB.begin()) return; 1692 1693 MachineBasicBlock::iterator PI = prior(MBBI); 1694 unsigned Opc = PI->getOpcode(); 1695 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 || 1696 Opc == X86::ADD32ri || Opc == X86::ADD32ri8) && 1697 PI->getOperand(0).getReg() == StackPtr) { 1698 if (NumBytes) 1699 *NumBytes += PI->getOperand(2).getImm(); 1700 MBB.erase(PI); 1701 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 || 1702 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) && 1703 PI->getOperand(0).getReg() == StackPtr) { 1704 if (NumBytes) 1705 *NumBytes -= PI->getOperand(2).getImm(); 1706 MBB.erase(PI); 1707 } 1708} 1709 1710// mergeSPUpdatesUp - Merge two stack-manipulating instructions lower iterator. 1711static 1712void mergeSPUpdatesDown(MachineBasicBlock &MBB, 1713 MachineBasicBlock::iterator &MBBI, 1714 unsigned StackPtr, uint64_t *NumBytes = NULL) { 1715 return; 1716 1717 if (MBBI == MBB.end()) return; 1718 1719 MachineBasicBlock::iterator NI = next(MBBI); 1720 if (NI == MBB.end()) return; 1721 1722 unsigned Opc = NI->getOpcode(); 1723 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 || 1724 Opc == X86::ADD32ri || Opc == X86::ADD32ri8) && 1725 NI->getOperand(0).getReg() == StackPtr) { 1726 if (NumBytes) 1727 *NumBytes -= NI->getOperand(2).getImm(); 1728 MBB.erase(NI); 1729 MBBI = NI; 1730 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 || 1731 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) && 1732 NI->getOperand(0).getReg() == StackPtr) { 1733 if (NumBytes) 1734 *NumBytes += NI->getOperand(2).getImm(); 1735 MBB.erase(NI); 1736 MBBI = NI; 1737 } 1738} 1739 1740/// mergeSPUpdates - Checks the instruction before/after the passed 1741/// instruction. If it is an ADD/SUB instruction it is deleted 1742/// argument and the stack adjustment is returned as a positive value for ADD 1743/// and a negative for SUB. 1744static int mergeSPUpdates(MachineBasicBlock &MBB, 1745 MachineBasicBlock::iterator &MBBI, 1746 unsigned StackPtr, 1747 bool doMergeWithPrevious) { 1748 1749 if ((doMergeWithPrevious && MBBI == MBB.begin()) || 1750 (!doMergeWithPrevious && MBBI == MBB.end())) 1751 return 0; 1752 1753 int Offset = 0; 1754 1755 MachineBasicBlock::iterator PI = doMergeWithPrevious ? prior(MBBI) : MBBI; 1756 MachineBasicBlock::iterator NI = doMergeWithPrevious ? 0 : next(MBBI); 1757 unsigned Opc = PI->getOpcode(); 1758 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 || 1759 Opc == X86::ADD32ri || Opc == X86::ADD32ri8) && 1760 PI->getOperand(0).getReg() == StackPtr){ 1761 Offset += PI->getOperand(2).getImm(); 1762 MBB.erase(PI); 1763 if (!doMergeWithPrevious) MBBI = NI; 1764 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 || 1765 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) && 1766 PI->getOperand(0).getReg() == StackPtr) { 1767 Offset -= PI->getOperand(2).getImm(); 1768 MBB.erase(PI); 1769 if (!doMergeWithPrevious) MBBI = NI; 1770 } 1771 1772 return Offset; 1773} 1774 1775void X86RegisterInfo::emitPrologue(MachineFunction &MF) const { 1776 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB 1777 MachineFrameInfo *MFI = MF.getFrameInfo(); 1778 const Function* Fn = MF.getFunction(); 1779 const X86Subtarget* Subtarget = &MF.getTarget().getSubtarget<X86Subtarget>(); 1780 MachineModuleInfo *MMI = MFI->getMachineModuleInfo(); 1781 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 1782 MachineBasicBlock::iterator MBBI = MBB.begin(); 1783 1784 // Prepare for frame info. 1785 unsigned FrameLabelId = 0; 1786 1787 // Get the number of bytes to allocate from the FrameInfo. 1788 uint64_t StackSize = MFI->getStackSize(); 1789 // Add RETADDR move area to callee saved frame size. 1790 int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); 1791 if (TailCallReturnAddrDelta < 0) 1792 X86FI->setCalleeSavedFrameSize( 1793 X86FI->getCalleeSavedFrameSize() +(-TailCallReturnAddrDelta)); 1794 uint64_t NumBytes = StackSize - X86FI->getCalleeSavedFrameSize(); 1795 1796 // Insert stack pointer adjustment for later moving of return addr. Only 1797 // applies to tail call optimized functions where the callee argument stack 1798 // size is bigger than the callers. 1799 if (TailCallReturnAddrDelta < 0) { 1800 BuildMI(MBB, MBBI, TII.get(Is64Bit? X86::SUB64ri32 : X86::SUB32ri), 1801 StackPtr).addReg(StackPtr).addImm(-TailCallReturnAddrDelta); 1802 } 1803 1804 if (hasFP(MF)) { 1805 // Get the offset of the stack slot for the EBP register... which is 1806 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized. 1807 // Update the frame offset adjustment. 1808 MFI->setOffsetAdjustment(SlotSize-NumBytes); 1809 1810 // Save EBP into the appropriate stack slot... 1811 BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::PUSH64r : X86::PUSH32r)) 1812 .addReg(FramePtr); 1813 NumBytes -= SlotSize; 1814 1815 if (MMI && MMI->needsFrameInfo()) { 1816 // Mark effective beginning of when frame pointer becomes valid. 1817 FrameLabelId = MMI->NextLabelID(); 1818 BuildMI(MBB, MBBI, TII.get(X86::LABEL)).addImm(FrameLabelId); 1819 } 1820 1821 // Update EBP with the new base value... 1822 BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr), FramePtr) 1823 .addReg(StackPtr); 1824 } 1825 1826 unsigned ReadyLabelId = 0; 1827 if (MMI && MMI->needsFrameInfo()) { 1828 // Mark effective beginning of when frame pointer is ready. 1829 ReadyLabelId = MMI->NextLabelID(); 1830 BuildMI(MBB, MBBI, TII.get(X86::LABEL)).addImm(ReadyLabelId); 1831 } 1832 1833 // Skip the callee-saved push instructions. 1834 while (MBBI != MBB.end() && 1835 (MBBI->getOpcode() == X86::PUSH32r || 1836 MBBI->getOpcode() == X86::PUSH64r)) 1837 ++MBBI; 1838 1839 if (NumBytes) { // adjust stack pointer: ESP -= numbytes 1840 if (NumBytes >= 4096 && Subtarget->isTargetCygMing()) { 1841 // Check, whether EAX is livein for this function 1842 bool isEAXAlive = false; 1843 for (MachineFunction::livein_iterator II = MF.livein_begin(), 1844 EE = MF.livein_end(); (II != EE) && !isEAXAlive; ++II) { 1845 unsigned Reg = II->first; 1846 isEAXAlive = (Reg == X86::EAX || Reg == X86::AX || 1847 Reg == X86::AH || Reg == X86::AL); 1848 } 1849 1850 // Function prologue calls _alloca to probe the stack when allocating 1851 // more than 4k bytes in one go. Touching the stack at 4K increments is 1852 // necessary to ensure that the guard pages used by the OS virtual memory 1853 // manager are allocated in correct sequence. 1854 if (!isEAXAlive) { 1855 BuildMI(MBB, MBBI, TII.get(X86::MOV32ri), X86::EAX).addImm(NumBytes); 1856 BuildMI(MBB, MBBI, TII.get(X86::CALLpcrel32)) 1857 .addExternalSymbol("_alloca"); 1858 } else { 1859 // Save EAX 1860 BuildMI(MBB, MBBI, TII.get(X86::PUSH32r), X86::EAX); 1861 // Allocate NumBytes-4 bytes on stack. We'll also use 4 already 1862 // allocated bytes for EAX. 1863 BuildMI(MBB, MBBI, TII.get(X86::MOV32ri), X86::EAX).addImm(NumBytes-4); 1864 BuildMI(MBB, MBBI, TII.get(X86::CALLpcrel32)) 1865 .addExternalSymbol("_alloca"); 1866 // Restore EAX 1867 MachineInstr *MI = addRegOffset(BuildMI(TII.get(X86::MOV32rm),X86::EAX), 1868 StackPtr, NumBytes-4); 1869 MBB.insert(MBBI, MI); 1870 } 1871 } else { 1872 // If there is an SUB32ri of ESP immediately before this instruction, 1873 // merge the two. This can be the case when tail call elimination is 1874 // enabled and the callee has more arguments then the caller. 1875 NumBytes -= mergeSPUpdates(MBB, MBBI, StackPtr, true); 1876 // If there is an ADD32ri or SUB32ri of ESP immediately after this 1877 // instruction, merge the two instructions. 1878 mergeSPUpdatesDown(MBB, MBBI, StackPtr, &NumBytes); 1879 1880 if (NumBytes) 1881 emitSPUpdate(MBB, MBBI, StackPtr, -(int64_t)NumBytes, Is64Bit, TII); 1882 } 1883 } 1884 1885 if (MMI && MMI->needsFrameInfo()) { 1886 std::vector<MachineMove> &Moves = MMI->getFrameMoves(); 1887 const TargetData *TD = MF.getTarget().getTargetData(); 1888 1889 // Calculate amount of bytes used for return address storing 1890 int stackGrowth = 1891 (MF.getTarget().getFrameInfo()->getStackGrowthDirection() == 1892 TargetFrameInfo::StackGrowsUp ? 1893 TD->getPointerSize() : -TD->getPointerSize()); 1894 1895 if (StackSize) { 1896 // Show update of SP. 1897 if (hasFP(MF)) { 1898 // Adjust SP 1899 MachineLocation SPDst(MachineLocation::VirtualFP); 1900 MachineLocation SPSrc(MachineLocation::VirtualFP, 2*stackGrowth); 1901 Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc)); 1902 } else { 1903 MachineLocation SPDst(MachineLocation::VirtualFP); 1904 MachineLocation SPSrc(MachineLocation::VirtualFP, -StackSize+stackGrowth); 1905 Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc)); 1906 } 1907 } else { 1908 //FIXME: Verify & implement for FP 1909 MachineLocation SPDst(StackPtr); 1910 MachineLocation SPSrc(StackPtr, stackGrowth); 1911 Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc)); 1912 } 1913 1914 // Add callee saved registers to move list. 1915 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 1916 1917 // FIXME: This is dirty hack. The code itself is pretty mess right now. 1918 // It should be rewritten from scratch and generalized sometimes. 1919 1920 // Determine maximum offset (minumum due to stack growth) 1921 int64_t MaxOffset = 0; 1922 for (unsigned I = 0, E = CSI.size(); I!=E; ++I) 1923 MaxOffset = std::min(MaxOffset, 1924 MFI->getObjectOffset(CSI[I].getFrameIdx())); 1925 1926 // Calculate offsets 1927 int64_t saveAreaOffset = (hasFP(MF) ? 3 : 2)*stackGrowth; 1928 for (unsigned I = 0, E = CSI.size(); I!=E; ++I) { 1929 int64_t Offset = MFI->getObjectOffset(CSI[I].getFrameIdx()); 1930 unsigned Reg = CSI[I].getReg(); 1931 Offset = (MaxOffset-Offset+saveAreaOffset); 1932 MachineLocation CSDst(MachineLocation::VirtualFP, Offset); 1933 MachineLocation CSSrc(Reg); 1934 Moves.push_back(MachineMove(FrameLabelId, CSDst, CSSrc)); 1935 } 1936 1937 if (hasFP(MF)) { 1938 // Save FP 1939 MachineLocation FPDst(MachineLocation::VirtualFP, 2*stackGrowth); 1940 MachineLocation FPSrc(FramePtr); 1941 Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc)); 1942 } 1943 1944 MachineLocation FPDst(hasFP(MF) ? FramePtr : StackPtr); 1945 MachineLocation FPSrc(MachineLocation::VirtualFP); 1946 Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc)); 1947 } 1948 1949 // If it's main() on Cygwin\Mingw32 we should align stack as well 1950 if (Fn->hasExternalLinkage() && Fn->getName() == "main" && 1951 Subtarget->isTargetCygMing()) { 1952 BuildMI(MBB, MBBI, TII.get(X86::AND32ri), X86::ESP) 1953 .addReg(X86::ESP).addImm(-StackAlign); 1954 1955 // Probe the stack 1956 BuildMI(MBB, MBBI, TII.get(X86::MOV32ri), X86::EAX).addImm(StackAlign); 1957 BuildMI(MBB, MBBI, TII.get(X86::CALLpcrel32)).addExternalSymbol("_alloca"); 1958 } 1959} 1960 1961void X86RegisterInfo::emitEpilogue(MachineFunction &MF, 1962 MachineBasicBlock &MBB) const { 1963 const MachineFrameInfo *MFI = MF.getFrameInfo(); 1964 const Function* Fn = MF.getFunction(); 1965 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 1966 const X86Subtarget* Subtarget = &MF.getTarget().getSubtarget<X86Subtarget>(); 1967 MachineBasicBlock::iterator MBBI = prior(MBB.end()); 1968 unsigned RetOpcode = MBBI->getOpcode(); 1969 1970 switch (RetOpcode) { 1971 case X86::RET: 1972 case X86::RETI: 1973 case X86::TCRETURNdi: 1974 case X86::TCRETURNri: 1975 case X86::TCRETURNri64: 1976 case X86::TCRETURNdi64: 1977 case X86::EH_RETURN: 1978 case X86::TAILJMPd: 1979 case X86::TAILJMPr: 1980 case X86::TAILJMPm: break; // These are ok 1981 default: 1982 assert(0 && "Can only insert epilog into returning blocks"); 1983 } 1984 1985 // Get the number of bytes to allocate from the FrameInfo 1986 uint64_t StackSize = MFI->getStackSize(); 1987 unsigned CSSize = X86FI->getCalleeSavedFrameSize(); 1988 uint64_t NumBytes = StackSize - CSSize; 1989 1990 if (hasFP(MF)) { 1991 // pop EBP. 1992 BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), FramePtr); 1993 NumBytes -= SlotSize; 1994 } 1995 1996 // Skip the callee-saved pop instructions. 1997 while (MBBI != MBB.begin()) { 1998 MachineBasicBlock::iterator PI = prior(MBBI); 1999 unsigned Opc = PI->getOpcode(); 2000 if (Opc != X86::POP32r && Opc != X86::POP64r && !TII.isTerminatorInstr(Opc)) 2001 break; 2002 --MBBI; 2003 } 2004 2005 // If there is an ADD32ri or SUB32ri of ESP immediately before this 2006 // instruction, merge the two instructions. 2007 if (NumBytes || MFI->hasVarSizedObjects()) 2008 mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes); 2009 2010 // If dynamic alloca is used, then reset esp to point to the last callee-saved 2011 // slot before popping them off! Also, if it's main() on Cygwin/Mingw32 we 2012 // aligned stack in the prologue, - revert stack changes back. Note: we're 2013 // assuming, that frame pointer was forced for main() 2014 if (MFI->hasVarSizedObjects() || 2015 (Fn->hasExternalLinkage() && Fn->getName() == "main" && 2016 Subtarget->isTargetCygMing())) { 2017 unsigned Opc = Is64Bit ? X86::LEA64r : X86::LEA32r; 2018 if (CSSize) { 2019 MachineInstr *MI = addRegOffset(BuildMI(TII.get(Opc), StackPtr), 2020 FramePtr, -CSSize); 2021 MBB.insert(MBBI, MI); 2022 } else 2023 BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr),StackPtr). 2024 addReg(FramePtr); 2025 2026 NumBytes = 0; 2027 } 2028 2029 // adjust stack pointer back: ESP += numbytes 2030 if (NumBytes) 2031 emitSPUpdate(MBB, MBBI, StackPtr, NumBytes, Is64Bit, TII); 2032 2033 // We're returning from function via eh_return. 2034 if (RetOpcode == X86::EH_RETURN) { 2035 MBBI = prior(MBB.end()); 2036 MachineOperand &DestAddr = MBBI->getOperand(0); 2037 assert(DestAddr.isRegister() && "Offset should be in register!"); 2038 BuildMI(MBB, MBBI, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr),StackPtr). 2039 addReg(DestAddr.getReg()); 2040 // Tail call return: adjust the stack pointer and jump to callee 2041 } else if (RetOpcode == X86::TCRETURNri || RetOpcode == X86::TCRETURNdi || 2042 RetOpcode== X86::TCRETURNri64 || RetOpcode == X86::TCRETURNdi64) { 2043 MBBI = prior(MBB.end()); 2044 MachineOperand &JumpTarget = MBBI->getOperand(0); 2045 MachineOperand &StackAdjust = MBBI->getOperand(1); 2046 assert( StackAdjust.isImmediate() && "Expecting immediate value."); 2047 2048 // Adjust stack pointer. 2049 int StackAdj = StackAdjust.getImm(); 2050 int MaxTCDelta = X86FI->getTCReturnAddrDelta(); 2051 int Offset = 0; 2052 assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive"); 2053 // Incoporate the retaddr area. 2054 Offset = StackAdj-MaxTCDelta; 2055 assert(Offset >= 0 && "Offset should never be negative"); 2056 if (Offset) { 2057 // Check for possible merge with preceeding ADD instruction. 2058 Offset += mergeSPUpdates(MBB, MBBI, StackPtr, true); 2059 emitSPUpdate(MBB, MBBI, StackPtr, Offset, Is64Bit, TII); 2060 } 2061 // Jump to label or value in register. 2062 if (RetOpcode == X86::TCRETURNdi|| RetOpcode == X86::TCRETURNdi64) 2063 BuildMI(MBB, MBBI, TII.get(X86::TAILJMPd)). 2064 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); 2065 else if (RetOpcode== X86::TCRETURNri64) { 2066 BuildMI(MBB, MBBI, TII.get(X86::TAILJMPr64), JumpTarget.getReg()); 2067 } else 2068 BuildMI(MBB, MBBI, TII.get(X86::TAILJMPr), JumpTarget.getReg()); 2069 // Delete the pseudo instruction TCRETURN. 2070 MBB.erase(MBBI); 2071 } else if ((RetOpcode == X86::RET || RetOpcode == X86::RETI) && 2072 (X86FI->getTCReturnAddrDelta() < 0)) { 2073 // Add the return addr area delta back since we are not tail calling. 2074 int delta = -1*X86FI->getTCReturnAddrDelta(); 2075 MBBI = prior(MBB.end()); 2076 // Check for possible merge with preceeding ADD instruction. 2077 delta += mergeSPUpdates(MBB, MBBI, StackPtr, true); 2078 emitSPUpdate(MBB, MBBI, StackPtr, delta, Is64Bit, TII); 2079 } 2080} 2081 2082unsigned X86RegisterInfo::getRARegister() const { 2083 if (Is64Bit) 2084 return X86::RIP; // Should have dwarf #16 2085 else 2086 return X86::EIP; // Should have dwarf #8 2087} 2088 2089unsigned X86RegisterInfo::getFrameRegister(MachineFunction &MF) const { 2090 return hasFP(MF) ? FramePtr : StackPtr; 2091} 2092 2093void X86RegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves) 2094 const { 2095 // Calculate amount of bytes used for return address storing 2096 int stackGrowth = (Is64Bit ? -8 : -4); 2097 2098 // Initial state of the frame pointer is esp+4. 2099 MachineLocation Dst(MachineLocation::VirtualFP); 2100 MachineLocation Src(StackPtr, stackGrowth); 2101 Moves.push_back(MachineMove(0, Dst, Src)); 2102 2103 // Add return address to move list 2104 MachineLocation CSDst(StackPtr, stackGrowth); 2105 MachineLocation CSSrc(getRARegister()); 2106 Moves.push_back(MachineMove(0, CSDst, CSSrc)); 2107} 2108 2109unsigned X86RegisterInfo::getEHExceptionRegister() const { 2110 assert(0 && "What is the exception register"); 2111 return 0; 2112} 2113 2114unsigned X86RegisterInfo::getEHHandlerRegister() const { 2115 assert(0 && "What is the exception handler register"); 2116 return 0; 2117} 2118 2119namespace llvm { 2120unsigned getX86SubSuperRegister(unsigned Reg, MVT::ValueType VT, bool High) { 2121 switch (VT) { 2122 default: return Reg; 2123 case MVT::i8: 2124 if (High) { 2125 switch (Reg) { 2126 default: return 0; 2127 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX: 2128 return X86::AH; 2129 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX: 2130 return X86::DH; 2131 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX: 2132 return X86::CH; 2133 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX: 2134 return X86::BH; 2135 } 2136 } else { 2137 switch (Reg) { 2138 default: return 0; 2139 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX: 2140 return X86::AL; 2141 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX: 2142 return X86::DL; 2143 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX: 2144 return X86::CL; 2145 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX: 2146 return X86::BL; 2147 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI: 2148 return X86::SIL; 2149 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI: 2150 return X86::DIL; 2151 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP: 2152 return X86::BPL; 2153 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP: 2154 return X86::SPL; 2155 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8: 2156 return X86::R8B; 2157 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9: 2158 return X86::R9B; 2159 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10: 2160 return X86::R10B; 2161 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11: 2162 return X86::R11B; 2163 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12: 2164 return X86::R12B; 2165 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13: 2166 return X86::R13B; 2167 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14: 2168 return X86::R14B; 2169 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15: 2170 return X86::R15B; 2171 } 2172 } 2173 case MVT::i16: 2174 switch (Reg) { 2175 default: return Reg; 2176 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX: 2177 return X86::AX; 2178 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX: 2179 return X86::DX; 2180 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX: 2181 return X86::CX; 2182 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX: 2183 return X86::BX; 2184 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI: 2185 return X86::SI; 2186 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI: 2187 return X86::DI; 2188 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP: 2189 return X86::BP; 2190 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP: 2191 return X86::SP; 2192 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8: 2193 return X86::R8W; 2194 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9: 2195 return X86::R9W; 2196 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10: 2197 return X86::R10W; 2198 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11: 2199 return X86::R11W; 2200 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12: 2201 return X86::R12W; 2202 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13: 2203 return X86::R13W; 2204 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14: 2205 return X86::R14W; 2206 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15: 2207 return X86::R15W; 2208 } 2209 case MVT::i32: 2210 switch (Reg) { 2211 default: return Reg; 2212 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX: 2213 return X86::EAX; 2214 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX: 2215 return X86::EDX; 2216 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX: 2217 return X86::ECX; 2218 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX: 2219 return X86::EBX; 2220 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI: 2221 return X86::ESI; 2222 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI: 2223 return X86::EDI; 2224 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP: 2225 return X86::EBP; 2226 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP: 2227 return X86::ESP; 2228 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8: 2229 return X86::R8D; 2230 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9: 2231 return X86::R9D; 2232 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10: 2233 return X86::R10D; 2234 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11: 2235 return X86::R11D; 2236 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12: 2237 return X86::R12D; 2238 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13: 2239 return X86::R13D; 2240 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14: 2241 return X86::R14D; 2242 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15: 2243 return X86::R15D; 2244 } 2245 case MVT::i64: 2246 switch (Reg) { 2247 default: return Reg; 2248 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX: 2249 return X86::RAX; 2250 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX: 2251 return X86::RDX; 2252 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX: 2253 return X86::RCX; 2254 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX: 2255 return X86::RBX; 2256 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI: 2257 return X86::RSI; 2258 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI: 2259 return X86::RDI; 2260 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP: 2261 return X86::RBP; 2262 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP: 2263 return X86::RSP; 2264 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8: 2265 return X86::R8; 2266 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9: 2267 return X86::R9; 2268 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10: 2269 return X86::R10; 2270 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11: 2271 return X86::R11; 2272 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12: 2273 return X86::R12; 2274 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13: 2275 return X86::R13; 2276 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14: 2277 return X86::R14; 2278 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15: 2279 return X86::R15; 2280 } 2281 } 2282 2283 return Reg; 2284} 2285} 2286 2287#include "X86GenRegisterInfo.inc" 2288 2289