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 // ExecutionDepsFixer 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 assert(MF && "MachineFunction required"); 268 269 const X86Subtarget &Subtarget = MF->getSubtarget<X86Subtarget>(); 270 bool HasSSE = Subtarget.hasSSE1(); 271 bool HasAVX = Subtarget.hasAVX(); 272 bool HasAVX512 = Subtarget.hasAVX512(); 273 bool CallsEHReturn = MF->callsEHReturn(); 274 275 switch (MF->getFunction()->getCallingConv()) { 276 case CallingConv::GHC: 277 case CallingConv::HiPE: 278 return CSR_NoRegs_SaveList; 279 case CallingConv::AnyReg: 280 if (HasAVX) 281 return CSR_64_AllRegs_AVX_SaveList; 282 return CSR_64_AllRegs_SaveList; 283 case CallingConv::PreserveMost: 284 return CSR_64_RT_MostRegs_SaveList; 285 case CallingConv::PreserveAll: 286 if (HasAVX) 287 return CSR_64_RT_AllRegs_AVX_SaveList; 288 return CSR_64_RT_AllRegs_SaveList; 289 case CallingConv::CXX_FAST_TLS: 290 if (Is64Bit) 291 return MF->getInfo<X86MachineFunctionInfo>()->isSplitCSR() ? 292 CSR_64_CXX_TLS_Darwin_PE_SaveList : CSR_64_TLS_Darwin_SaveList; 293 break; 294 case CallingConv::Intel_OCL_BI: { 295 if (HasAVX512 && IsWin64) 296 return CSR_Win64_Intel_OCL_BI_AVX512_SaveList; 297 if (HasAVX512 && Is64Bit) 298 return CSR_64_Intel_OCL_BI_AVX512_SaveList; 299 if (HasAVX && IsWin64) 300 return CSR_Win64_Intel_OCL_BI_AVX_SaveList; 301 if (HasAVX && Is64Bit) 302 return CSR_64_Intel_OCL_BI_AVX_SaveList; 303 if (!HasAVX && !IsWin64 && Is64Bit) 304 return CSR_64_Intel_OCL_BI_SaveList; 305 break; 306 } 307 case CallingConv::HHVM: 308 return CSR_64_HHVM_SaveList; 309 case CallingConv::X86_RegCall: 310 if (Is64Bit) { 311 if (IsWin64) { 312 return (HasSSE ? CSR_Win64_RegCall_SaveList : 313 CSR_Win64_RegCall_NoSSE_SaveList); 314 } else { 315 return (HasSSE ? CSR_SysV64_RegCall_SaveList : 316 CSR_SysV64_RegCall_NoSSE_SaveList); 317 } 318 } else { 319 return (HasSSE ? CSR_32_RegCall_SaveList : 320 CSR_32_RegCall_NoSSE_SaveList); 321 } 322 case CallingConv::Cold: 323 if (Is64Bit) 324 return CSR_64_MostRegs_SaveList; 325 break; 326 case CallingConv::X86_64_Win64: 327 if (!HasSSE) 328 return CSR_Win64_NoSSE_SaveList; 329 return CSR_Win64_SaveList; 330 case CallingConv::X86_64_SysV: 331 if (CallsEHReturn) 332 return CSR_64EHRet_SaveList; 333 return CSR_64_SaveList; 334 case CallingConv::X86_INTR: 335 if (Is64Bit) { 336 if (HasAVX512) 337 return CSR_64_AllRegs_AVX512_SaveList; 338 if (HasAVX) 339 return CSR_64_AllRegs_AVX_SaveList; 340 if (HasSSE) 341 return CSR_64_AllRegs_SaveList; 342 return CSR_64_AllRegs_NoSSE_SaveList; 343 } else { 344 if (HasAVX512) 345 return CSR_32_AllRegs_AVX512_SaveList; 346 if (HasAVX) 347 return CSR_32_AllRegs_AVX_SaveList; 348 if (HasSSE) 349 return CSR_32_AllRegs_SSE_SaveList; 350 return CSR_32_AllRegs_SaveList; 351 } 352 default: 353 break; 354 } 355 356 if (Is64Bit) { 357 if (IsWin64) { 358 if (!HasSSE) 359 return CSR_Win64_NoSSE_SaveList; 360 return CSR_Win64_SaveList; 361 } 362 if (CallsEHReturn) 363 return CSR_64EHRet_SaveList; 364 if (Subtarget.getTargetLowering()->supportSwiftError() && 365 MF->getFunction()->getAttributes().hasAttrSomewhere( 366 Attribute::SwiftError)) 367 return CSR_64_SwiftError_SaveList; 368 return CSR_64_SaveList; 369 } 370 if (CallsEHReturn) 371 return CSR_32EHRet_SaveList; 372 return CSR_32_SaveList; 373 } 374 375 const MCPhysReg *X86RegisterInfo::getCalleeSavedRegsViaCopy( 376 const MachineFunction *MF) const { 377 assert(MF && "Invalid MachineFunction pointer."); 378 if (MF->getFunction()->getCallingConv() == CallingConv::CXX_FAST_TLS && 379 MF->getInfo<X86MachineFunctionInfo>()->isSplitCSR()) 380 return CSR_64_CXX_TLS_Darwin_ViaCopy_SaveList; 381 return nullptr; 382 } 383 384 const uint32_t * 385 X86RegisterInfo::getCallPreservedMask(const MachineFunction &MF, 386 CallingConv::ID CC) const { 387 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>(); 388 bool HasSSE = Subtarget.hasSSE1(); 389 bool HasAVX = Subtarget.hasAVX(); 390 bool HasAVX512 = Subtarget.hasAVX512(); 391 392 switch (CC) { 393 case CallingConv::GHC: 394 case CallingConv::HiPE: 395 return CSR_NoRegs_RegMask; 396 case CallingConv::AnyReg: 397 if (HasAVX) 398 return CSR_64_AllRegs_AVX_RegMask; 399 return CSR_64_AllRegs_RegMask; 400 case CallingConv::PreserveMost: 401 return CSR_64_RT_MostRegs_RegMask; 402 case CallingConv::PreserveAll: 403 if (HasAVX) 404 return CSR_64_RT_AllRegs_AVX_RegMask; 405 return CSR_64_RT_AllRegs_RegMask; 406 case CallingConv::CXX_FAST_TLS: 407 if (Is64Bit) 408 return CSR_64_TLS_Darwin_RegMask; 409 break; 410 case CallingConv::Intel_OCL_BI: { 411 if (HasAVX512 && IsWin64) 412 return CSR_Win64_Intel_OCL_BI_AVX512_RegMask; 413 if (HasAVX512 && Is64Bit) 414 return CSR_64_Intel_OCL_BI_AVX512_RegMask; 415 if (HasAVX && IsWin64) 416 return CSR_Win64_Intel_OCL_BI_AVX_RegMask; 417 if (HasAVX && Is64Bit) 418 return CSR_64_Intel_OCL_BI_AVX_RegMask; 419 if (!HasAVX && !IsWin64 && Is64Bit) 420 return CSR_64_Intel_OCL_BI_RegMask; 421 break; 422 } 423 case CallingConv::HHVM: 424 return CSR_64_HHVM_RegMask; 425 case CallingConv::X86_RegCall: 426 if (Is64Bit) { 427 if (IsWin64) { 428 return (HasSSE ? CSR_Win64_RegCall_RegMask : 429 CSR_Win64_RegCall_NoSSE_RegMask); 430 } else { 431 return (HasSSE ? CSR_SysV64_RegCall_RegMask : 432 CSR_SysV64_RegCall_NoSSE_RegMask); 433 } 434 } else { 435 return (HasSSE ? CSR_32_RegCall_RegMask : 436 CSR_32_RegCall_NoSSE_RegMask); 437 } 438 case CallingConv::Cold: 439 if (Is64Bit) 440 return CSR_64_MostRegs_RegMask; 441 break; 442 case CallingConv::X86_64_Win64: 443 return CSR_Win64_RegMask; 444 case CallingConv::X86_64_SysV: 445 return CSR_64_RegMask; 446 case CallingConv::X86_INTR: 447 if (Is64Bit) { 448 if (HasAVX512) 449 return CSR_64_AllRegs_AVX512_RegMask; 450 if (HasAVX) 451 return CSR_64_AllRegs_AVX_RegMask; 452 if (HasSSE) 453 return CSR_64_AllRegs_RegMask; 454 return CSR_64_AllRegs_NoSSE_RegMask; 455 } else { 456 if (HasAVX512) 457 return CSR_32_AllRegs_AVX512_RegMask; 458 if (HasAVX) 459 return CSR_32_AllRegs_AVX_RegMask; 460 if (HasSSE) 461 return CSR_32_AllRegs_SSE_RegMask; 462 return CSR_32_AllRegs_RegMask; 463 } 464 default: 465 break; 466 } 467 468 // Unlike getCalleeSavedRegs(), we don't have MMI so we can't check 469 // callsEHReturn(). 470 if (Is64Bit) { 471 if (IsWin64) 472 return CSR_Win64_RegMask; 473 if (Subtarget.getTargetLowering()->supportSwiftError() && 474 MF.getFunction()->getAttributes().hasAttrSomewhere( 475 Attribute::SwiftError)) 476 return CSR_64_SwiftError_RegMask; 477 return CSR_64_RegMask; 478 } 479 return CSR_32_RegMask; 480 } 481 482 const uint32_t* 483 X86RegisterInfo::getNoPreservedMask() const { 484 return CSR_NoRegs_RegMask; 485 } 486 487 const uint32_t *X86RegisterInfo::getDarwinTLSCallPreservedMask() const { 488 return CSR_64_TLS_Darwin_RegMask; 489 } 490 491 BitVector X86RegisterInfo::getReservedRegs(const MachineFunction &MF) const { 492 BitVector Reserved(getNumRegs()); 493 const X86FrameLowering *TFI = getFrameLowering(MF); 494 495 // Set the stack-pointer register and its aliases as reserved. 496 for (MCSubRegIterator I(X86::RSP, this, /*IncludeSelf=*/true); I.isValid(); 497 ++I) 498 Reserved.set(*I); 499 500 // Set the instruction pointer register and its aliases as reserved. 501 for (MCSubRegIterator I(X86::RIP, this, /*IncludeSelf=*/true); I.isValid(); 502 ++I) 503 Reserved.set(*I); 504 505 // Set the frame-pointer register and its aliases as reserved if needed. 506 if (TFI->hasFP(MF)) { 507 for (MCSubRegIterator I(X86::RBP, this, /*IncludeSelf=*/true); I.isValid(); 508 ++I) 509 Reserved.set(*I); 510 } 511 512 // Set the base-pointer register and its aliases as reserved if needed. 513 if (hasBasePointer(MF)) { 514 CallingConv::ID CC = MF.getFunction()->getCallingConv(); 515 const uint32_t *RegMask = getCallPreservedMask(MF, CC); 516 if (MachineOperand::clobbersPhysReg(RegMask, getBaseRegister())) 517 report_fatal_error( 518 "Stack realignment in presence of dynamic allocas is not supported with" 519 "this calling convention."); 520 521 unsigned BasePtr = getX86SubSuperRegister(getBaseRegister(), 64); 522 for (MCSubRegIterator I(BasePtr, this, /*IncludeSelf=*/true); 523 I.isValid(); ++I) 524 Reserved.set(*I); 525 } 526 527 // Mark the segment registers as reserved. 528 Reserved.set(X86::CS); 529 Reserved.set(X86::SS); 530 Reserved.set(X86::DS); 531 Reserved.set(X86::ES); 532 Reserved.set(X86::FS); 533 Reserved.set(X86::GS); 534 535 // Mark the floating point stack registers as reserved. 536 for (unsigned n = 0; n != 8; ++n) 537 Reserved.set(X86::ST0 + n); 538 539 // Reserve the registers that only exist in 64-bit mode. 540 if (!Is64Bit) { 541 // These 8-bit registers are part of the x86-64 extension even though their 542 // super-registers are old 32-bits. 543 Reserved.set(X86::SIL); 544 Reserved.set(X86::DIL); 545 Reserved.set(X86::BPL); 546 Reserved.set(X86::SPL); 547 548 for (unsigned n = 0; n != 8; ++n) { 549 // R8, R9, ... 550 for (MCRegAliasIterator AI(X86::R8 + n, this, true); AI.isValid(); ++AI) 551 Reserved.set(*AI); 552 553 // XMM8, XMM9, ... 554 for (MCRegAliasIterator AI(X86::XMM8 + n, this, true); AI.isValid(); ++AI) 555 Reserved.set(*AI); 556 } 557 } 558 if (!Is64Bit || !MF.getSubtarget<X86Subtarget>().hasAVX512()) { 559 for (unsigned n = 16; n != 32; ++n) { 560 for (MCRegAliasIterator AI(X86::XMM0 + n, this, true); AI.isValid(); ++AI) 561 Reserved.set(*AI); 562 } 563 } 564 565 assert(checkAllSuperRegsMarked(Reserved, 566 {X86::SIL, X86::DIL, X86::BPL, X86::SPL})); 567 return Reserved; 568 } 569 570 void X86RegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const { 571 // Check if the EFLAGS register is marked as live-out. This shouldn't happen, 572 // because the calling convention defines the EFLAGS register as NOT 573 // preserved. 574 // 575 // Unfortunatelly the EFLAGS show up as live-out after branch folding. Adding 576 // an assert to track this and clear the register afterwards to avoid 577 // unnecessary crashes during release builds. 578 assert(!(Mask[X86::EFLAGS / 32] & (1U << (X86::EFLAGS % 32))) && 579 "EFLAGS are not live-out from a patchpoint."); 580 581 // Also clean other registers that don't need preserving (IP). 582 for (auto Reg : {X86::EFLAGS, X86::RIP, X86::EIP, X86::IP}) 583 Mask[Reg / 32] &= ~(1U << (Reg % 32)); 584 } 585 586 //===----------------------------------------------------------------------===// 587 // Stack Frame Processing methods 588 //===----------------------------------------------------------------------===// 589 590 static bool CantUseSP(const MachineFrameInfo &MFI) { 591 return MFI.hasVarSizedObjects() || MFI.hasOpaqueSPAdjustment(); 592 } 593 594 bool X86RegisterInfo::hasBasePointer(const MachineFunction &MF) const { 595 const MachineFrameInfo &MFI = MF.getFrameInfo(); 596 597 if (!EnableBasePointer) 598 return false; 599 600 // When we need stack realignment, we can't address the stack from the frame 601 // pointer. When we have dynamic allocas or stack-adjusting inline asm, we 602 // can't address variables from the stack pointer. MS inline asm can 603 // reference locals while also adjusting the stack pointer. When we can't 604 // use both the SP and the FP, we need a separate base pointer register. 605 bool CantUseFP = needsStackRealignment(MF); 606 return CantUseFP && CantUseSP(MFI); 607 } 608 609 bool X86RegisterInfo::canRealignStack(const MachineFunction &MF) const { 610 if (!TargetRegisterInfo::canRealignStack(MF)) 611 return false; 612 613 const MachineFrameInfo &MFI = MF.getFrameInfo(); 614 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 615 616 // Stack realignment requires a frame pointer. If we already started 617 // register allocation with frame pointer elimination, it is too late now. 618 if (!MRI->canReserveReg(FramePtr)) 619 return false; 620 621 // If a base pointer is necessary. Check that it isn't too late to reserve 622 // it. 623 if (CantUseSP(MFI)) 624 return MRI->canReserveReg(BasePtr); 625 return true; 626 } 627 628 bool X86RegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 629 unsigned Reg, int &FrameIdx) const { 630 // Since X86 defines assignCalleeSavedSpillSlots which always return true 631 // this function neither used nor tested. 632 llvm_unreachable("Unused function on X86. Otherwise need a test case."); 633 } 634 635 // tryOptimizeLEAtoMOV - helper function that tries to replace a LEA instruction 636 // of the form 'lea (%esp), %ebx' --> 'mov %esp, %ebx'. 637 // TODO: In this case we should be really trying first to entirely eliminate 638 // this instruction which is a plain copy. 639 static bool tryOptimizeLEAtoMOV(MachineBasicBlock::iterator II) { 640 MachineInstr &MI = *II; 641 unsigned Opc = II->getOpcode(); 642 // Check if this is a LEA of the form 'lea (%esp), %ebx' 643 if ((Opc != X86::LEA32r && Opc != X86::LEA64r && Opc != X86::LEA64_32r) || 644 MI.getOperand(2).getImm() != 1 || 645 MI.getOperand(3).getReg() != X86::NoRegister || 646 MI.getOperand(4).getImm() != 0 || 647 MI.getOperand(5).getReg() != X86::NoRegister) 648 return false; 649 unsigned BasePtr = MI.getOperand(1).getReg(); 650 // In X32 mode, ensure the base-pointer is a 32-bit operand, so the LEA will 651 // be replaced with a 32-bit operand MOV which will zero extend the upper 652 // 32-bits of the super register. 653 if (Opc == X86::LEA64_32r) 654 BasePtr = getX86SubSuperRegister(BasePtr, 32); 655 unsigned NewDestReg = MI.getOperand(0).getReg(); 656 const X86InstrInfo *TII = 657 MI.getParent()->getParent()->getSubtarget<X86Subtarget>().getInstrInfo(); 658 TII->copyPhysReg(*MI.getParent(), II, MI.getDebugLoc(), NewDestReg, BasePtr, 659 MI.getOperand(1).isKill()); 660 MI.eraseFromParent(); 661 return true; 662 } 663 664 void 665 X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 666 int SPAdj, unsigned FIOperandNum, 667 RegScavenger *RS) const { 668 MachineInstr &MI = *II; 669 MachineFunction &MF = *MI.getParent()->getParent(); 670 const X86FrameLowering *TFI = getFrameLowering(MF); 671 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 672 673 // Determine base register and offset. 674 int FIOffset; 675 unsigned BasePtr; 676 if (MI.isReturn()) { 677 assert(MF.getFrameInfo().isFixedObjectIndex(FrameIndex) && 678 "Should only reference fixed stack objects here"); 679 FIOffset = TFI->getFrameIndexReferenceSP(MF, FrameIndex, BasePtr, 0); 680 } else { 681 FIOffset = TFI->getFrameIndexReference(MF, FrameIndex, BasePtr); 682 } 683 684 // LOCAL_ESCAPE uses a single offset, with no register. It only works in the 685 // simple FP case, and doesn't work with stack realignment. On 32-bit, the 686 // offset is from the traditional base pointer location. On 64-bit, the 687 // offset is from the SP at the end of the prologue, not the FP location. This 688 // matches the behavior of llvm.frameaddress. 689 unsigned Opc = MI.getOpcode(); 690 if (Opc == TargetOpcode::LOCAL_ESCAPE) { 691 MachineOperand &FI = MI.getOperand(FIOperandNum); 692 FI.ChangeToImmediate(FIOffset); 693 return; 694 } 695 696 // For LEA64_32r when BasePtr is 32-bits (X32) we can use full-size 64-bit 697 // register as source operand, semantic is the same and destination is 698 // 32-bits. It saves one byte per lea in code since 0x67 prefix is avoided. 699 // Don't change BasePtr since it is used later for stack adjustment. 700 unsigned MachineBasePtr = BasePtr; 701 if (Opc == X86::LEA64_32r && X86::GR32RegClass.contains(BasePtr)) 702 MachineBasePtr = getX86SubSuperRegister(BasePtr, 64); 703 704 // This must be part of a four operand memory reference. Replace the 705 // FrameIndex with base register. Add an offset to the offset. 706 MI.getOperand(FIOperandNum).ChangeToRegister(MachineBasePtr, false); 707 708 if (BasePtr == StackPtr) 709 FIOffset += SPAdj; 710 711 // The frame index format for stackmaps and patchpoints is different from the 712 // X86 format. It only has a FI and an offset. 713 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) { 714 assert(BasePtr == FramePtr && "Expected the FP as base register"); 715 int64_t Offset = MI.getOperand(FIOperandNum + 1).getImm() + FIOffset; 716 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset); 717 return; 718 } 719 720 if (MI.getOperand(FIOperandNum+3).isImm()) { 721 // Offset is a 32-bit integer. 722 int Imm = (int)(MI.getOperand(FIOperandNum + 3).getImm()); 723 int Offset = FIOffset + Imm; 724 assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) && 725 "Requesting 64-bit offset in 32-bit immediate!"); 726 if (Offset != 0 || !tryOptimizeLEAtoMOV(II)) 727 MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset); 728 } else { 729 // Offset is symbolic. This is extremely rare. 730 uint64_t Offset = FIOffset + 731 (uint64_t)MI.getOperand(FIOperandNum+3).getOffset(); 732 MI.getOperand(FIOperandNum + 3).setOffset(Offset); 733 } 734 } 735 736 unsigned X86RegisterInfo::getFrameRegister(const MachineFunction &MF) const { 737 const X86FrameLowering *TFI = getFrameLowering(MF); 738 return TFI->hasFP(MF) ? FramePtr : StackPtr; 739 } 740 741 unsigned 742 X86RegisterInfo::getPtrSizedFrameRegister(const MachineFunction &MF) const { 743 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>(); 744 unsigned FrameReg = getFrameRegister(MF); 745 if (Subtarget.isTarget64BitILP32()) 746 FrameReg = getX86SubSuperRegister(FrameReg, 32); 747 return FrameReg; 748 } 749