1 //===-- X86RegisterInfo.cpp - X86 Register Information --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the X86 implementation of the TargetRegisterInfo class. 11 // This file is responsible for the frame pointer elimination optimization 12 // on X86. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "X86RegisterInfo.h" 17 #include "X86FrameLowering.h" 18 #include "X86InstrBuilder.h" 19 #include "X86MachineFunctionInfo.h" 20 #include "X86Subtarget.h" 21 #include "X86TargetMachine.h" 22 #include "llvm/ADT/BitVector.h" 23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/CodeGen/MachineFrameInfo.h" 25 #include "llvm/CodeGen/MachineFunction.h" 26 #include "llvm/CodeGen/MachineFunctionPass.h" 27 #include "llvm/CodeGen/MachineInstrBuilder.h" 28 #include "llvm/CodeGen/MachineModuleInfo.h" 29 #include "llvm/CodeGen/MachineRegisterInfo.h" 30 #include "llvm/IR/Constants.h" 31 #include "llvm/IR/Function.h" 32 #include "llvm/IR/Type.h" 33 #include "llvm/MC/MCAsmInfo.h" 34 #include "llvm/Support/CommandLine.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Target/TargetFrameLowering.h" 37 #include "llvm/Target/TargetInstrInfo.h" 38 #include "llvm/Target/TargetMachine.h" 39 #include "llvm/Target/TargetOptions.h" 40 41 using namespace llvm; 42 43 #define GET_REGINFO_TARGET_DESC 44 #include "X86GenRegisterInfo.inc" 45 46 static cl::opt<bool> 47 EnableBasePointer("x86-use-base-pointer", cl::Hidden, cl::init(true), 48 cl::desc("Enable use of a base pointer for complex stack frames")); 49 50 X86RegisterInfo::X86RegisterInfo(const Triple &TT) 51 : X86GenRegisterInfo((TT.isArch64Bit() ? X86::RIP : X86::EIP), 52 X86_MC::getDwarfRegFlavour(TT, false), 53 X86_MC::getDwarfRegFlavour(TT, true), 54 (TT.isArch64Bit() ? X86::RIP : X86::EIP)) { 55 X86_MC::initLLVMToSEHAndCVRegMapping(this); 56 57 // Cache some information. 58 Is64Bit = TT.isArch64Bit(); 59 IsWin64 = Is64Bit && TT.isOSWindows(); 60 61 // Use a callee-saved register as the base pointer. These registers must 62 // not conflict with any ABI requirements. For example, in 32-bit mode PIC 63 // requires GOT in the EBX register before function calls via PLT GOT pointer. 64 if (Is64Bit) { 65 SlotSize = 8; 66 // This matches the simplified 32-bit pointer code in the data layout 67 // computation. 68 // FIXME: Should use the data layout? 69 bool Use64BitReg = TT.getEnvironment() != Triple::GNUX32; 70 StackPtr = Use64BitReg ? X86::RSP : X86::ESP; 71 FramePtr = Use64BitReg ? X86::RBP : X86::EBP; 72 BasePtr = Use64BitReg ? X86::RBX : X86::EBX; 73 } else { 74 SlotSize = 4; 75 StackPtr = X86::ESP; 76 FramePtr = X86::EBP; 77 BasePtr = X86::ESI; 78 } 79 } 80 81 bool 82 X86RegisterInfo::trackLivenessAfterRegAlloc(const MachineFunction &MF) const { 83 // ExeDepsFixer and PostRAScheduler require liveness. 84 return true; 85 } 86 87 int 88 X86RegisterInfo::getSEHRegNum(unsigned i) const { 89 return getEncodingValue(i); 90 } 91 92 const TargetRegisterClass * 93 X86RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC, 94 unsigned Idx) const { 95 // The sub_8bit sub-register index is more constrained in 32-bit mode. 96 // It behaves just like the sub_8bit_hi index. 97 if (!Is64Bit && Idx == X86::sub_8bit) 98 Idx = X86::sub_8bit_hi; 99 100 // Forward to TableGen's default version. 101 return X86GenRegisterInfo::getSubClassWithSubReg(RC, Idx); 102 } 103 104 const TargetRegisterClass * 105 X86RegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A, 106 const TargetRegisterClass *B, 107 unsigned SubIdx) const { 108 // The sub_8bit sub-register index is more constrained in 32-bit mode. 109 if (!Is64Bit && SubIdx == X86::sub_8bit) { 110 A = X86GenRegisterInfo::getSubClassWithSubReg(A, X86::sub_8bit_hi); 111 if (!A) 112 return nullptr; 113 } 114 return X86GenRegisterInfo::getMatchingSuperRegClass(A, B, SubIdx); 115 } 116 117 const TargetRegisterClass * 118 X86RegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC, 119 const MachineFunction &MF) const { 120 // Don't allow super-classes of GR8_NOREX. This class is only used after 121 // extracting sub_8bit_hi sub-registers. The H sub-registers cannot be copied 122 // to the full GR8 register class in 64-bit mode, so we cannot allow the 123 // reigster class inflation. 124 // 125 // The GR8_NOREX class is always used in a way that won't be constrained to a 126 // sub-class, so sub-classes like GR8_ABCD_L are allowed to expand to the 127 // full GR8 class. 128 if (RC == &X86::GR8_NOREXRegClass) 129 return RC; 130 131 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>(); 132 133 const TargetRegisterClass *Super = RC; 134 TargetRegisterClass::sc_iterator I = RC->getSuperClasses(); 135 do { 136 switch (Super->getID()) { 137 case X86::FR32RegClassID: 138 case X86::FR64RegClassID: 139 // If AVX-512 isn't supported we should only inflate to these classes. 140 if (!Subtarget.hasAVX512() && Super->getSize() == RC->getSize()) 141 return Super; 142 break; 143 case X86::VR128RegClassID: 144 case X86::VR256RegClassID: 145 // If VLX isn't supported we should only inflate to these classes. 146 if (!Subtarget.hasVLX() && Super->getSize() == RC->getSize()) 147 return Super; 148 break; 149 case X86::VR128XRegClassID: 150 case X86::VR256XRegClassID: 151 // If VLX isn't support we shouldn't inflate to these classes. 152 if (Subtarget.hasVLX() && Super->getSize() == RC->getSize()) 153 return Super; 154 break; 155 case X86::FR32XRegClassID: 156 case X86::FR64XRegClassID: 157 // If AVX-512 isn't support we shouldn't inflate to these classes. 158 if (Subtarget.hasAVX512() && Super->getSize() == RC->getSize()) 159 return Super; 160 break; 161 case X86::GR8RegClassID: 162 case X86::GR16RegClassID: 163 case X86::GR32RegClassID: 164 case X86::GR64RegClassID: 165 case X86::RFP32RegClassID: 166 case X86::RFP64RegClassID: 167 case X86::RFP80RegClassID: 168 case X86::VR512RegClassID: 169 // Don't return a super-class that would shrink the spill size. 170 // That can happen with the vector and float classes. 171 if (Super->getSize() == RC->getSize()) 172 return Super; 173 } 174 Super = *I++; 175 } while (Super); 176 return RC; 177 } 178 179 const TargetRegisterClass * 180 X86RegisterInfo::getPointerRegClass(const MachineFunction &MF, 181 unsigned Kind) const { 182 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>(); 183 switch (Kind) { 184 default: llvm_unreachable("Unexpected Kind in getPointerRegClass!"); 185 case 0: // Normal GPRs. 186 if (Subtarget.isTarget64BitLP64()) 187 return &X86::GR64RegClass; 188 // If the target is 64bit but we have been told to use 32bit addresses, 189 // we can still use 64-bit register as long as we know the high bits 190 // are zeros. 191 // Reflect that in the returned register class. 192 if (Is64Bit) { 193 // When the target also allows 64-bit frame pointer and we do have a 194 // frame, this is fine to use it for the address accesses as well. 195 const X86FrameLowering *TFI = getFrameLowering(MF); 196 return TFI->hasFP(MF) && TFI->Uses64BitFramePtr 197 ? &X86::LOW32_ADDR_ACCESS_RBPRegClass 198 : &X86::LOW32_ADDR_ACCESSRegClass; 199 } 200 return &X86::GR32RegClass; 201 case 1: // Normal GPRs except the stack pointer (for encoding reasons). 202 if (Subtarget.isTarget64BitLP64()) 203 return &X86::GR64_NOSPRegClass; 204 // NOSP does not contain RIP, so no special case here. 205 return &X86::GR32_NOSPRegClass; 206 case 2: // NOREX GPRs. 207 if (Subtarget.isTarget64BitLP64()) 208 return &X86::GR64_NOREXRegClass; 209 return &X86::GR32_NOREXRegClass; 210 case 3: // NOREX GPRs except the stack pointer (for encoding reasons). 211 if (Subtarget.isTarget64BitLP64()) 212 return &X86::GR64_NOREX_NOSPRegClass; 213 // NOSP does not contain RIP, so no special case here. 214 return &X86::GR32_NOREX_NOSPRegClass; 215 case 4: // Available for tailcall (not callee-saved GPRs). 216 return getGPRsForTailCall(MF); 217 } 218 } 219 220 const TargetRegisterClass * 221 X86RegisterInfo::getGPRsForTailCall(const MachineFunction &MF) const { 222 const Function *F = MF.getFunction(); 223 if (IsWin64 || (F && F->getCallingConv() == CallingConv::X86_64_Win64)) 224 return &X86::GR64_TCW64RegClass; 225 else if (Is64Bit) 226 return &X86::GR64_TCRegClass; 227 228 bool hasHipeCC = (F ? F->getCallingConv() == CallingConv::HiPE : false); 229 if (hasHipeCC) 230 return &X86::GR32RegClass; 231 return &X86::GR32_TCRegClass; 232 } 233 234 const TargetRegisterClass * 235 X86RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const { 236 if (RC == &X86::CCRRegClass) { 237 if (Is64Bit) 238 return &X86::GR64RegClass; 239 else 240 return &X86::GR32RegClass; 241 } 242 return RC; 243 } 244 245 unsigned 246 X86RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 247 MachineFunction &MF) const { 248 const X86FrameLowering *TFI = getFrameLowering(MF); 249 250 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0; 251 switch (RC->getID()) { 252 default: 253 return 0; 254 case X86::GR32RegClassID: 255 return 4 - FPDiff; 256 case X86::GR64RegClassID: 257 return 12 - FPDiff; 258 case X86::VR128RegClassID: 259 return Is64Bit ? 10 : 4; 260 case X86::VR64RegClassID: 261 return 4; 262 } 263 } 264 265 const MCPhysReg * 266 X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 267 const X86Subtarget &Subtarget = MF->getSubtarget<X86Subtarget>(); 268 bool HasSSE = Subtarget.hasSSE1(); 269 bool HasAVX = Subtarget.hasAVX(); 270 bool HasAVX512 = Subtarget.hasAVX512(); 271 bool CallsEHReturn = MF->getMMI().callsEHReturn(); 272 273 assert(MF && "MachineFunction required"); 274 switch (MF->getFunction()->getCallingConv()) { 275 case CallingConv::GHC: 276 case CallingConv::HiPE: 277 return CSR_NoRegs_SaveList; 278 case CallingConv::AnyReg: 279 if (HasAVX) 280 return CSR_64_AllRegs_AVX_SaveList; 281 return CSR_64_AllRegs_SaveList; 282 case CallingConv::PreserveMost: 283 return CSR_64_RT_MostRegs_SaveList; 284 case CallingConv::PreserveAll: 285 if (HasAVX) 286 return CSR_64_RT_AllRegs_AVX_SaveList; 287 return CSR_64_RT_AllRegs_SaveList; 288 case CallingConv::CXX_FAST_TLS: 289 if (Is64Bit) 290 return MF->getInfo<X86MachineFunctionInfo>()->isSplitCSR() ? 291 CSR_64_CXX_TLS_Darwin_PE_SaveList : CSR_64_TLS_Darwin_SaveList; 292 break; 293 case CallingConv::Intel_OCL_BI: { 294 if (HasAVX512 && IsWin64) 295 return CSR_Win64_Intel_OCL_BI_AVX512_SaveList; 296 if (HasAVX512 && Is64Bit) 297 return CSR_64_Intel_OCL_BI_AVX512_SaveList; 298 if (HasAVX && IsWin64) 299 return CSR_Win64_Intel_OCL_BI_AVX_SaveList; 300 if (HasAVX && Is64Bit) 301 return CSR_64_Intel_OCL_BI_AVX_SaveList; 302 if (!HasAVX && !IsWin64 && Is64Bit) 303 return CSR_64_Intel_OCL_BI_SaveList; 304 break; 305 } 306 case CallingConv::HHVM: 307 return CSR_64_HHVM_SaveList; 308 case CallingConv::X86_RegCall: 309 if (Is64Bit) { 310 if (IsWin64) { 311 return (HasSSE ? CSR_Win64_RegCall_SaveList : 312 CSR_Win64_RegCall_NoSSE_SaveList); 313 } else { 314 return (HasSSE ? CSR_SysV64_RegCall_SaveList : 315 CSR_SysV64_RegCall_NoSSE_SaveList); 316 } 317 } else { 318 return (HasSSE ? CSR_32_RegCall_SaveList : 319 CSR_32_RegCall_NoSSE_SaveList); 320 } 321 case CallingConv::Cold: 322 if (Is64Bit) 323 return CSR_64_MostRegs_SaveList; 324 break; 325 case CallingConv::X86_64_Win64: 326 if (!HasSSE) 327 return CSR_Win64_NoSSE_SaveList; 328 return CSR_Win64_SaveList; 329 case CallingConv::X86_64_SysV: 330 if (CallsEHReturn) 331 return CSR_64EHRet_SaveList; 332 return CSR_64_SaveList; 333 case CallingConv::X86_INTR: 334 if (Is64Bit) { 335 if (HasAVX512) 336 return CSR_64_AllRegs_AVX512_SaveList; 337 if (HasAVX) 338 return CSR_64_AllRegs_AVX_SaveList; 339 return CSR_64_AllRegs_SaveList; 340 } else { 341 if (HasAVX512) 342 return CSR_32_AllRegs_AVX512_SaveList; 343 if (HasAVX) 344 return CSR_32_AllRegs_AVX_SaveList; 345 if (HasSSE) 346 return CSR_32_AllRegs_SSE_SaveList; 347 return CSR_32_AllRegs_SaveList; 348 } 349 default: 350 break; 351 } 352 353 if (Is64Bit) { 354 if (IsWin64) { 355 if (!HasSSE) 356 return CSR_Win64_NoSSE_SaveList; 357 return CSR_Win64_SaveList; 358 } 359 if (CallsEHReturn) 360 return CSR_64EHRet_SaveList; 361 if (Subtarget.getTargetLowering()->supportSwiftError() && 362 MF->getFunction()->getAttributes().hasAttrSomewhere( 363 Attribute::SwiftError)) 364 return CSR_64_SwiftError_SaveList; 365 return CSR_64_SaveList; 366 } 367 if (CallsEHReturn) 368 return CSR_32EHRet_SaveList; 369 return CSR_32_SaveList; 370 } 371 372 const MCPhysReg *X86RegisterInfo::getCalleeSavedRegsViaCopy( 373 const MachineFunction *MF) const { 374 assert(MF && "Invalid MachineFunction pointer."); 375 if (MF->getFunction()->getCallingConv() == CallingConv::CXX_FAST_TLS && 376 MF->getInfo<X86MachineFunctionInfo>()->isSplitCSR()) 377 return CSR_64_CXX_TLS_Darwin_ViaCopy_SaveList; 378 return nullptr; 379 } 380 381 const uint32_t * 382 X86RegisterInfo::getCallPreservedMask(const MachineFunction &MF, 383 CallingConv::ID CC) const { 384 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>(); 385 bool HasSSE = Subtarget.hasSSE1(); 386 bool HasAVX = Subtarget.hasAVX(); 387 bool HasAVX512 = Subtarget.hasAVX512(); 388 389 switch (CC) { 390 case CallingConv::GHC: 391 case CallingConv::HiPE: 392 return CSR_NoRegs_RegMask; 393 case CallingConv::AnyReg: 394 if (HasAVX) 395 return CSR_64_AllRegs_AVX_RegMask; 396 return CSR_64_AllRegs_RegMask; 397 case CallingConv::PreserveMost: 398 return CSR_64_RT_MostRegs_RegMask; 399 case CallingConv::PreserveAll: 400 if (HasAVX) 401 return CSR_64_RT_AllRegs_AVX_RegMask; 402 return CSR_64_RT_AllRegs_RegMask; 403 case CallingConv::CXX_FAST_TLS: 404 if (Is64Bit) 405 return CSR_64_TLS_Darwin_RegMask; 406 break; 407 case CallingConv::Intel_OCL_BI: { 408 if (HasAVX512 && IsWin64) 409 return CSR_Win64_Intel_OCL_BI_AVX512_RegMask; 410 if (HasAVX512 && Is64Bit) 411 return CSR_64_Intel_OCL_BI_AVX512_RegMask; 412 if (HasAVX && IsWin64) 413 return CSR_Win64_Intel_OCL_BI_AVX_RegMask; 414 if (HasAVX && Is64Bit) 415 return CSR_64_Intel_OCL_BI_AVX_RegMask; 416 if (!HasAVX && !IsWin64 && Is64Bit) 417 return CSR_64_Intel_OCL_BI_RegMask; 418 break; 419 } 420 case CallingConv::HHVM: 421 return CSR_64_HHVM_RegMask; 422 case CallingConv::X86_RegCall: 423 if (Is64Bit) { 424 if (IsWin64) { 425 return (HasSSE ? CSR_Win64_RegCall_RegMask : 426 CSR_Win64_RegCall_NoSSE_RegMask); 427 } else { 428 return (HasSSE ? CSR_SysV64_RegCall_RegMask : 429 CSR_SysV64_RegCall_NoSSE_RegMask); 430 } 431 } else { 432 return (HasSSE ? CSR_32_RegCall_RegMask : 433 CSR_32_RegCall_NoSSE_RegMask); 434 } 435 case CallingConv::Cold: 436 if (Is64Bit) 437 return CSR_64_MostRegs_RegMask; 438 break; 439 case CallingConv::X86_64_Win64: 440 return CSR_Win64_RegMask; 441 case CallingConv::X86_64_SysV: 442 return CSR_64_RegMask; 443 case CallingConv::X86_INTR: 444 if (Is64Bit) { 445 if (HasAVX512) 446 return CSR_64_AllRegs_AVX512_RegMask; 447 if (HasAVX) 448 return CSR_64_AllRegs_AVX_RegMask; 449 return CSR_64_AllRegs_RegMask; 450 } else { 451 if (HasAVX512) 452 return CSR_32_AllRegs_AVX512_RegMask; 453 if (HasAVX) 454 return CSR_32_AllRegs_AVX_RegMask; 455 if (HasSSE) 456 return CSR_32_AllRegs_SSE_RegMask; 457 return CSR_32_AllRegs_RegMask; 458 } 459 default: 460 break; 461 } 462 463 // Unlike getCalleeSavedRegs(), we don't have MMI so we can't check 464 // callsEHReturn(). 465 if (Is64Bit) { 466 if (IsWin64) 467 return CSR_Win64_RegMask; 468 if (Subtarget.getTargetLowering()->supportSwiftError() && 469 MF.getFunction()->getAttributes().hasAttrSomewhere( 470 Attribute::SwiftError)) 471 return CSR_64_SwiftError_RegMask; 472 return CSR_64_RegMask; 473 } 474 return CSR_32_RegMask; 475 } 476 477 const uint32_t* 478 X86RegisterInfo::getNoPreservedMask() const { 479 return CSR_NoRegs_RegMask; 480 } 481 482 const uint32_t *X86RegisterInfo::getDarwinTLSCallPreservedMask() const { 483 return CSR_64_TLS_Darwin_RegMask; 484 } 485 486 BitVector X86RegisterInfo::getReservedRegs(const MachineFunction &MF) const { 487 BitVector Reserved(getNumRegs()); 488 const X86FrameLowering *TFI = getFrameLowering(MF); 489 490 // Set the stack-pointer register and its aliases as reserved. 491 for (MCSubRegIterator I(X86::RSP, this, /*IncludeSelf=*/true); I.isValid(); 492 ++I) 493 Reserved.set(*I); 494 495 // Set the instruction pointer register and its aliases as reserved. 496 for (MCSubRegIterator I(X86::RIP, this, /*IncludeSelf=*/true); I.isValid(); 497 ++I) 498 Reserved.set(*I); 499 500 // Set the frame-pointer register and its aliases as reserved if needed. 501 if (TFI->hasFP(MF)) { 502 for (MCSubRegIterator I(X86::RBP, this, /*IncludeSelf=*/true); I.isValid(); 503 ++I) 504 Reserved.set(*I); 505 } 506 507 // Set the base-pointer register and its aliases as reserved if needed. 508 if (hasBasePointer(MF)) { 509 CallingConv::ID CC = MF.getFunction()->getCallingConv(); 510 const uint32_t *RegMask = getCallPreservedMask(MF, CC); 511 if (MachineOperand::clobbersPhysReg(RegMask, getBaseRegister())) 512 report_fatal_error( 513 "Stack realignment in presence of dynamic allocas is not supported with" 514 "this calling convention."); 515 516 unsigned BasePtr = getX86SubSuperRegister(getBaseRegister(), 64); 517 for (MCSubRegIterator I(BasePtr, this, /*IncludeSelf=*/true); 518 I.isValid(); ++I) 519 Reserved.set(*I); 520 } 521 522 // Mark the segment registers as reserved. 523 Reserved.set(X86::CS); 524 Reserved.set(X86::SS); 525 Reserved.set(X86::DS); 526 Reserved.set(X86::ES); 527 Reserved.set(X86::FS); 528 Reserved.set(X86::GS); 529 530 // Mark the floating point stack registers as reserved. 531 for (unsigned n = 0; n != 8; ++n) 532 Reserved.set(X86::ST0 + n); 533 534 // Reserve the registers that only exist in 64-bit mode. 535 if (!Is64Bit) { 536 // These 8-bit registers are part of the x86-64 extension even though their 537 // super-registers are old 32-bits. 538 Reserved.set(X86::SIL); 539 Reserved.set(X86::DIL); 540 Reserved.set(X86::BPL); 541 Reserved.set(X86::SPL); 542 543 for (unsigned n = 0; n != 8; ++n) { 544 // R8, R9, ... 545 for (MCRegAliasIterator AI(X86::R8 + n, this, true); AI.isValid(); ++AI) 546 Reserved.set(*AI); 547 548 // XMM8, XMM9, ... 549 for (MCRegAliasIterator AI(X86::XMM8 + n, this, true); AI.isValid(); ++AI) 550 Reserved.set(*AI); 551 } 552 } 553 if (!Is64Bit || !MF.getSubtarget<X86Subtarget>().hasAVX512()) { 554 for (unsigned n = 16; n != 32; ++n) { 555 for (MCRegAliasIterator AI(X86::XMM0 + n, this, true); AI.isValid(); ++AI) 556 Reserved.set(*AI); 557 } 558 } 559 560 return Reserved; 561 } 562 563 void X86RegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const { 564 // Check if the EFLAGS register is marked as live-out. This shouldn't happen, 565 // because the calling convention defines the EFLAGS register as NOT 566 // preserved. 567 // 568 // Unfortunatelly the EFLAGS show up as live-out after branch folding. Adding 569 // an assert to track this and clear the register afterwards to avoid 570 // unnecessary crashes during release builds. 571 assert(!(Mask[X86::EFLAGS / 32] & (1U << (X86::EFLAGS % 32))) && 572 "EFLAGS are not live-out from a patchpoint."); 573 574 // Also clean other registers that don't need preserving (IP). 575 for (auto Reg : {X86::EFLAGS, X86::RIP, X86::EIP, X86::IP}) 576 Mask[Reg / 32] &= ~(1U << (Reg % 32)); 577 } 578 579 //===----------------------------------------------------------------------===// 580 // Stack Frame Processing methods 581 //===----------------------------------------------------------------------===// 582 583 static bool CantUseSP(const MachineFrameInfo &MFI) { 584 return MFI.hasVarSizedObjects() || MFI.hasOpaqueSPAdjustment(); 585 } 586 587 bool X86RegisterInfo::hasBasePointer(const MachineFunction &MF) const { 588 const MachineFrameInfo &MFI = MF.getFrameInfo(); 589 590 if (!EnableBasePointer) 591 return false; 592 593 // When we need stack realignment, we can't address the stack from the frame 594 // pointer. When we have dynamic allocas or stack-adjusting inline asm, we 595 // can't address variables from the stack pointer. MS inline asm can 596 // reference locals while also adjusting the stack pointer. When we can't 597 // use both the SP and the FP, we need a separate base pointer register. 598 bool CantUseFP = needsStackRealignment(MF); 599 return CantUseFP && CantUseSP(MFI); 600 } 601 602 bool X86RegisterInfo::canRealignStack(const MachineFunction &MF) const { 603 if (!TargetRegisterInfo::canRealignStack(MF)) 604 return false; 605 606 const MachineFrameInfo &MFI = MF.getFrameInfo(); 607 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 608 609 // Stack realignment requires a frame pointer. If we already started 610 // register allocation with frame pointer elimination, it is too late now. 611 if (!MRI->canReserveReg(FramePtr)) 612 return false; 613 614 // If a base pointer is necessary. Check that it isn't too late to reserve 615 // it. 616 if (CantUseSP(MFI)) 617 return MRI->canReserveReg(BasePtr); 618 return true; 619 } 620 621 bool X86RegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 622 unsigned Reg, int &FrameIdx) const { 623 // Since X86 defines assignCalleeSavedSpillSlots which always return true 624 // this function neither used nor tested. 625 llvm_unreachable("Unused function on X86. Otherwise need a test case."); 626 } 627 628 // tryOptimizeLEAtoMOV - helper function that tries to replace a LEA instruction 629 // of the form 'lea (%esp), %ebx' --> 'mov %esp, %ebx'. 630 // TODO: In this case we should be really trying first to entirely eliminate 631 // this instruction which is a plain copy. 632 static bool tryOptimizeLEAtoMOV(MachineBasicBlock::iterator II) { 633 MachineInstr &MI = *II; 634 unsigned Opc = II->getOpcode(); 635 // Check if this is a LEA of the form 'lea (%esp), %ebx' 636 if ((Opc != X86::LEA32r && Opc != X86::LEA64r && Opc != X86::LEA64_32r) || 637 MI.getOperand(2).getImm() != 1 || 638 MI.getOperand(3).getReg() != X86::NoRegister || 639 MI.getOperand(4).getImm() != 0 || 640 MI.getOperand(5).getReg() != X86::NoRegister) 641 return false; 642 unsigned BasePtr = MI.getOperand(1).getReg(); 643 // In X32 mode, ensure the base-pointer is a 32-bit operand, so the LEA will 644 // be replaced with a 32-bit operand MOV which will zero extend the upper 645 // 32-bits of the super register. 646 if (Opc == X86::LEA64_32r) 647 BasePtr = getX86SubSuperRegister(BasePtr, 32); 648 unsigned NewDestReg = MI.getOperand(0).getReg(); 649 const X86InstrInfo *TII = 650 MI.getParent()->getParent()->getSubtarget<X86Subtarget>().getInstrInfo(); 651 TII->copyPhysReg(*MI.getParent(), II, MI.getDebugLoc(), NewDestReg, BasePtr, 652 MI.getOperand(1).isKill()); 653 MI.eraseFromParent(); 654 return true; 655 } 656 657 void 658 X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 659 int SPAdj, unsigned FIOperandNum, 660 RegScavenger *RS) const { 661 MachineInstr &MI = *II; 662 MachineFunction &MF = *MI.getParent()->getParent(); 663 const X86FrameLowering *TFI = getFrameLowering(MF); 664 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 665 unsigned BasePtr; 666 667 unsigned Opc = MI.getOpcode(); 668 bool AfterFPPop = Opc == X86::TAILJMPm64 || Opc == X86::TAILJMPm || 669 Opc == X86::TCRETURNmi || Opc == X86::TCRETURNmi64; 670 671 if (hasBasePointer(MF)) 672 BasePtr = (FrameIndex < 0 ? FramePtr : getBaseRegister()); 673 else if (needsStackRealignment(MF)) 674 BasePtr = (FrameIndex < 0 ? FramePtr : StackPtr); 675 else if (AfterFPPop) 676 BasePtr = StackPtr; 677 else 678 BasePtr = (TFI->hasFP(MF) ? FramePtr : StackPtr); 679 680 // LOCAL_ESCAPE uses a single offset, with no register. It only works in the 681 // simple FP case, and doesn't work with stack realignment. On 32-bit, the 682 // offset is from the traditional base pointer location. On 64-bit, the 683 // offset is from the SP at the end of the prologue, not the FP location. This 684 // matches the behavior of llvm.frameaddress. 685 unsigned IgnoredFrameReg; 686 if (Opc == TargetOpcode::LOCAL_ESCAPE) { 687 MachineOperand &FI = MI.getOperand(FIOperandNum); 688 int Offset; 689 Offset = TFI->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg); 690 FI.ChangeToImmediate(Offset); 691 return; 692 } 693 694 // For LEA64_32r when BasePtr is 32-bits (X32) we can use full-size 64-bit 695 // register as source operand, semantic is the same and destination is 696 // 32-bits. It saves one byte per lea in code since 0x67 prefix is avoided. 697 // Don't change BasePtr since it is used later for stack adjustment. 698 unsigned MachineBasePtr = BasePtr; 699 if (Opc == X86::LEA64_32r && X86::GR32RegClass.contains(BasePtr)) 700 MachineBasePtr = getX86SubSuperRegister(BasePtr, 64); 701 702 // This must be part of a four operand memory reference. Replace the 703 // FrameIndex with base register. Add an offset to the offset. 704 MI.getOperand(FIOperandNum).ChangeToRegister(MachineBasePtr, false); 705 706 // Now add the frame object offset to the offset from EBP. 707 int FIOffset; 708 if (AfterFPPop) { 709 // Tail call jmp happens after FP is popped. 710 const MachineFrameInfo &MFI = MF.getFrameInfo(); 711 FIOffset = MFI.getObjectOffset(FrameIndex) - TFI->getOffsetOfLocalArea(); 712 } else 713 FIOffset = TFI->getFrameIndexReference(MF, FrameIndex, IgnoredFrameReg); 714 715 if (BasePtr == StackPtr) 716 FIOffset += SPAdj; 717 718 // The frame index format for stackmaps and patchpoints is different from the 719 // X86 format. It only has a FI and an offset. 720 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) { 721 assert(BasePtr == FramePtr && "Expected the FP as base register"); 722 int64_t Offset = MI.getOperand(FIOperandNum + 1).getImm() + FIOffset; 723 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset); 724 return; 725 } 726 727 if (MI.getOperand(FIOperandNum+3).isImm()) { 728 // Offset is a 32-bit integer. 729 int Imm = (int)(MI.getOperand(FIOperandNum + 3).getImm()); 730 int Offset = FIOffset + Imm; 731 assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) && 732 "Requesting 64-bit offset in 32-bit immediate!"); 733 if (Offset != 0 || !tryOptimizeLEAtoMOV(II)) 734 MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset); 735 } else { 736 // Offset is symbolic. This is extremely rare. 737 uint64_t Offset = FIOffset + 738 (uint64_t)MI.getOperand(FIOperandNum+3).getOffset(); 739 MI.getOperand(FIOperandNum + 3).setOffset(Offset); 740 } 741 } 742 743 unsigned X86RegisterInfo::getFrameRegister(const MachineFunction &MF) const { 744 const X86FrameLowering *TFI = getFrameLowering(MF); 745 return TFI->hasFP(MF) ? FramePtr : StackPtr; 746 } 747 748 unsigned 749 X86RegisterInfo::getPtrSizedFrameRegister(const MachineFunction &MF) const { 750 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>(); 751 unsigned FrameReg = getFrameRegister(MF); 752 if (Subtarget.isTarget64BitILP32()) 753 FrameReg = getX86SubSuperRegister(FrameReg, 32); 754 return FrameReg; 755 } 756