X86RegisterInfo.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. //===-- X86RegisterInfo.cpp - X86 Register Information --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the X86 implementation of the TargetRegisterInfo class.
  10. // This file is responsible for the frame pointer elimination optimization
  11. // on X86.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "X86RegisterInfo.h"
  15. #include "X86FrameLowering.h"
  16. #include "X86MachineFunctionInfo.h"
  17. #include "X86Subtarget.h"
  18. #include "llvm/ADT/BitVector.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SmallSet.h"
  21. #include "llvm/CodeGen/LiveRegMatrix.h"
  22. #include "llvm/CodeGen/MachineFrameInfo.h"
  23. #include "llvm/CodeGen/MachineFunction.h"
  24. #include "llvm/CodeGen/MachineFunctionPass.h"
  25. #include "llvm/CodeGen/MachineRegisterInfo.h"
  26. #include "llvm/CodeGen/TargetFrameLowering.h"
  27. #include "llvm/CodeGen/TargetInstrInfo.h"
  28. #include "llvm/IR/Constants.h"
  29. #include "llvm/IR/Function.h"
  30. #include "llvm/IR/Type.h"
  31. #include "llvm/Support/CommandLine.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include "llvm/Target/TargetMachine.h"
  34. #include "llvm/Target/TargetOptions.h"
  35. using namespace llvm;
  36. #define GET_REGINFO_TARGET_DESC
  37. #include "X86GenRegisterInfo.inc"
  38. static cl::opt<bool>
  39. EnableBasePointer("x86-use-base-pointer", cl::Hidden, cl::init(true),
  40. cl::desc("Enable use of a base pointer for complex stack frames"));
  41. X86RegisterInfo::X86RegisterInfo(const Triple &TT)
  42. : X86GenRegisterInfo((TT.isArch64Bit() ? X86::RIP : X86::EIP),
  43. X86_MC::getDwarfRegFlavour(TT, false),
  44. X86_MC::getDwarfRegFlavour(TT, true),
  45. (TT.isArch64Bit() ? X86::RIP : X86::EIP)) {
  46. X86_MC::initLLVMToSEHAndCVRegMapping(this);
  47. // Cache some information.
  48. Is64Bit = TT.isArch64Bit();
  49. IsWin64 = Is64Bit && TT.isOSWindows();
  50. // Use a callee-saved register as the base pointer. These registers must
  51. // not conflict with any ABI requirements. For example, in 32-bit mode PIC
  52. // requires GOT in the EBX register before function calls via PLT GOT pointer.
  53. if (Is64Bit) {
  54. SlotSize = 8;
  55. // This matches the simplified 32-bit pointer code in the data layout
  56. // computation.
  57. // FIXME: Should use the data layout?
  58. bool Use64BitReg = !TT.isX32();
  59. StackPtr = Use64BitReg ? X86::RSP : X86::ESP;
  60. FramePtr = Use64BitReg ? X86::RBP : X86::EBP;
  61. BasePtr = Use64BitReg ? X86::RBX : X86::EBX;
  62. } else {
  63. SlotSize = 4;
  64. StackPtr = X86::ESP;
  65. FramePtr = X86::EBP;
  66. BasePtr = X86::ESI;
  67. }
  68. }
  69. int
  70. X86RegisterInfo::getSEHRegNum(unsigned i) const {
  71. return getEncodingValue(i);
  72. }
  73. const TargetRegisterClass *
  74. X86RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC,
  75. unsigned Idx) const {
  76. // The sub_8bit sub-register index is more constrained in 32-bit mode.
  77. // It behaves just like the sub_8bit_hi index.
  78. if (!Is64Bit && Idx == X86::sub_8bit)
  79. Idx = X86::sub_8bit_hi;
  80. // Forward to TableGen's default version.
  81. return X86GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
  82. }
  83. const TargetRegisterClass *
  84. X86RegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
  85. const TargetRegisterClass *B,
  86. unsigned SubIdx) const {
  87. // The sub_8bit sub-register index is more constrained in 32-bit mode.
  88. if (!Is64Bit && SubIdx == X86::sub_8bit) {
  89. A = X86GenRegisterInfo::getSubClassWithSubReg(A, X86::sub_8bit_hi);
  90. if (!A)
  91. return nullptr;
  92. }
  93. return X86GenRegisterInfo::getMatchingSuperRegClass(A, B, SubIdx);
  94. }
  95. const TargetRegisterClass *
  96. X86RegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
  97. const MachineFunction &MF) const {
  98. // Don't allow super-classes of GR8_NOREX. This class is only used after
  99. // extracting sub_8bit_hi sub-registers. The H sub-registers cannot be copied
  100. // to the full GR8 register class in 64-bit mode, so we cannot allow the
  101. // reigster class inflation.
  102. //
  103. // The GR8_NOREX class is always used in a way that won't be constrained to a
  104. // sub-class, so sub-classes like GR8_ABCD_L are allowed to expand to the
  105. // full GR8 class.
  106. if (RC == &X86::GR8_NOREXRegClass)
  107. return RC;
  108. const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
  109. const TargetRegisterClass *Super = RC;
  110. TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
  111. do {
  112. switch (Super->getID()) {
  113. case X86::FR32RegClassID:
  114. case X86::FR64RegClassID:
  115. // If AVX-512 isn't supported we should only inflate to these classes.
  116. if (!Subtarget.hasAVX512() &&
  117. getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
  118. return Super;
  119. break;
  120. case X86::VR128RegClassID:
  121. case X86::VR256RegClassID:
  122. // If VLX isn't supported we should only inflate to these classes.
  123. if (!Subtarget.hasVLX() &&
  124. getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
  125. return Super;
  126. break;
  127. case X86::VR128XRegClassID:
  128. case X86::VR256XRegClassID:
  129. // If VLX isn't support we shouldn't inflate to these classes.
  130. if (Subtarget.hasVLX() &&
  131. getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
  132. return Super;
  133. break;
  134. case X86::FR32XRegClassID:
  135. case X86::FR64XRegClassID:
  136. // If AVX-512 isn't support we shouldn't inflate to these classes.
  137. if (Subtarget.hasAVX512() &&
  138. getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
  139. return Super;
  140. break;
  141. case X86::GR8RegClassID:
  142. case X86::GR16RegClassID:
  143. case X86::GR32RegClassID:
  144. case X86::GR64RegClassID:
  145. case X86::RFP32RegClassID:
  146. case X86::RFP64RegClassID:
  147. case X86::RFP80RegClassID:
  148. case X86::VR512_0_15RegClassID:
  149. case X86::VR512RegClassID:
  150. // Don't return a super-class that would shrink the spill size.
  151. // That can happen with the vector and float classes.
  152. if (getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
  153. return Super;
  154. }
  155. Super = *I++;
  156. } while (Super);
  157. return RC;
  158. }
  159. const TargetRegisterClass *
  160. X86RegisterInfo::getPointerRegClass(const MachineFunction &MF,
  161. unsigned Kind) const {
  162. const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
  163. switch (Kind) {
  164. default: llvm_unreachable("Unexpected Kind in getPointerRegClass!");
  165. case 0: // Normal GPRs.
  166. if (Subtarget.isTarget64BitLP64())
  167. return &X86::GR64RegClass;
  168. // If the target is 64bit but we have been told to use 32bit addresses,
  169. // we can still use 64-bit register as long as we know the high bits
  170. // are zeros.
  171. // Reflect that in the returned register class.
  172. if (Is64Bit) {
  173. // When the target also allows 64-bit frame pointer and we do have a
  174. // frame, this is fine to use it for the address accesses as well.
  175. const X86FrameLowering *TFI = getFrameLowering(MF);
  176. return TFI->hasFP(MF) && TFI->Uses64BitFramePtr
  177. ? &X86::LOW32_ADDR_ACCESS_RBPRegClass
  178. : &X86::LOW32_ADDR_ACCESSRegClass;
  179. }
  180. return &X86::GR32RegClass;
  181. case 1: // Normal GPRs except the stack pointer (for encoding reasons).
  182. if (Subtarget.isTarget64BitLP64())
  183. return &X86::GR64_NOSPRegClass;
  184. // NOSP does not contain RIP, so no special case here.
  185. return &X86::GR32_NOSPRegClass;
  186. case 2: // NOREX GPRs.
  187. if (Subtarget.isTarget64BitLP64())
  188. return &X86::GR64_NOREXRegClass;
  189. return &X86::GR32_NOREXRegClass;
  190. case 3: // NOREX GPRs except the stack pointer (for encoding reasons).
  191. if (Subtarget.isTarget64BitLP64())
  192. return &X86::GR64_NOREX_NOSPRegClass;
  193. // NOSP does not contain RIP, so no special case here.
  194. return &X86::GR32_NOREX_NOSPRegClass;
  195. case 4: // Available for tailcall (not callee-saved GPRs).
  196. return getGPRsForTailCall(MF);
  197. }
  198. }
  199. bool X86RegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
  200. unsigned DefSubReg,
  201. const TargetRegisterClass *SrcRC,
  202. unsigned SrcSubReg) const {
  203. // Prevent rewriting a copy where the destination size is larger than the
  204. // input size. See PR41619.
  205. // FIXME: Should this be factored into the base implementation somehow.
  206. if (DefRC->hasSuperClassEq(&X86::GR64RegClass) && DefSubReg == 0 &&
  207. SrcRC->hasSuperClassEq(&X86::GR64RegClass) && SrcSubReg == X86::sub_32bit)
  208. return false;
  209. return TargetRegisterInfo::shouldRewriteCopySrc(DefRC, DefSubReg,
  210. SrcRC, SrcSubReg);
  211. }
  212. const TargetRegisterClass *
  213. X86RegisterInfo::getGPRsForTailCall(const MachineFunction &MF) const {
  214. const Function &F = MF.getFunction();
  215. if (IsWin64 || (F.getCallingConv() == CallingConv::Win64))
  216. return &X86::GR64_TCW64RegClass;
  217. else if (Is64Bit)
  218. return &X86::GR64_TCRegClass;
  219. bool hasHipeCC = (F.getCallingConv() == CallingConv::HiPE);
  220. if (hasHipeCC)
  221. return &X86::GR32RegClass;
  222. return &X86::GR32_TCRegClass;
  223. }
  224. const TargetRegisterClass *
  225. X86RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
  226. if (RC == &X86::CCRRegClass) {
  227. if (Is64Bit)
  228. return &X86::GR64RegClass;
  229. else
  230. return &X86::GR32RegClass;
  231. }
  232. return RC;
  233. }
  234. unsigned
  235. X86RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
  236. MachineFunction &MF) const {
  237. const X86FrameLowering *TFI = getFrameLowering(MF);
  238. unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
  239. switch (RC->getID()) {
  240. default:
  241. return 0;
  242. case X86::GR32RegClassID:
  243. return 4 - FPDiff;
  244. case X86::GR64RegClassID:
  245. return 12 - FPDiff;
  246. case X86::VR128RegClassID:
  247. return Is64Bit ? 10 : 4;
  248. case X86::VR64RegClassID:
  249. return 4;
  250. }
  251. }
  252. const MCPhysReg *
  253. X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
  254. assert(MF && "MachineFunction required");
  255. const X86Subtarget &Subtarget = MF->getSubtarget<X86Subtarget>();
  256. const Function &F = MF->getFunction();
  257. bool HasSSE = Subtarget.hasSSE1();
  258. bool HasAVX = Subtarget.hasAVX();
  259. bool HasAVX512 = Subtarget.hasAVX512();
  260. bool CallsEHReturn = MF->callsEHReturn();
  261. CallingConv::ID CC = F.getCallingConv();
  262. // If attribute NoCallerSavedRegisters exists then we set X86_INTR calling
  263. // convention because it has the CSR list.
  264. if (MF->getFunction().hasFnAttribute("no_caller_saved_registers"))
  265. CC = CallingConv::X86_INTR;
  266. // If atribute specified, override the CSRs normally specified by the
  267. // calling convention and use the empty set instead.
  268. if (MF->getFunction().hasFnAttribute("no_callee_saved_registers"))
  269. return CSR_NoRegs_SaveList;
  270. switch (CC) {
  271. case CallingConv::GHC:
  272. case CallingConv::HiPE:
  273. return CSR_NoRegs_SaveList;
  274. case CallingConv::AnyReg:
  275. if (HasAVX)
  276. return CSR_64_AllRegs_AVX_SaveList;
  277. return CSR_64_AllRegs_SaveList;
  278. case CallingConv::PreserveMost:
  279. return CSR_64_RT_MostRegs_SaveList;
  280. case CallingConv::PreserveAll:
  281. if (HasAVX)
  282. return CSR_64_RT_AllRegs_AVX_SaveList;
  283. return CSR_64_RT_AllRegs_SaveList;
  284. case CallingConv::CXX_FAST_TLS:
  285. if (Is64Bit)
  286. return MF->getInfo<X86MachineFunctionInfo>()->isSplitCSR() ?
  287. CSR_64_CXX_TLS_Darwin_PE_SaveList : CSR_64_TLS_Darwin_SaveList;
  288. break;
  289. case CallingConv::Intel_OCL_BI: {
  290. if (HasAVX512 && IsWin64)
  291. return CSR_Win64_Intel_OCL_BI_AVX512_SaveList;
  292. if (HasAVX512 && Is64Bit)
  293. return CSR_64_Intel_OCL_BI_AVX512_SaveList;
  294. if (HasAVX && IsWin64)
  295. return CSR_Win64_Intel_OCL_BI_AVX_SaveList;
  296. if (HasAVX && Is64Bit)
  297. return CSR_64_Intel_OCL_BI_AVX_SaveList;
  298. if (!HasAVX && !IsWin64 && Is64Bit)
  299. return CSR_64_Intel_OCL_BI_SaveList;
  300. break;
  301. }
  302. case CallingConv::HHVM:
  303. return CSR_64_HHVM_SaveList;
  304. case CallingConv::X86_RegCall:
  305. if (Is64Bit) {
  306. if (IsWin64) {
  307. return (HasSSE ? CSR_Win64_RegCall_SaveList :
  308. CSR_Win64_RegCall_NoSSE_SaveList);
  309. } else {
  310. return (HasSSE ? CSR_SysV64_RegCall_SaveList :
  311. CSR_SysV64_RegCall_NoSSE_SaveList);
  312. }
  313. } else {
  314. return (HasSSE ? CSR_32_RegCall_SaveList :
  315. CSR_32_RegCall_NoSSE_SaveList);
  316. }
  317. case CallingConv::CFGuard_Check:
  318. assert(!Is64Bit && "CFGuard check mechanism only used on 32-bit X86");
  319. return (HasSSE ? CSR_Win32_CFGuard_Check_SaveList
  320. : CSR_Win32_CFGuard_Check_NoSSE_SaveList);
  321. case CallingConv::Cold:
  322. if (Is64Bit)
  323. return CSR_64_MostRegs_SaveList;
  324. break;
  325. case CallingConv::Win64:
  326. if (!HasSSE)
  327. return CSR_Win64_NoSSE_SaveList;
  328. return CSR_Win64_SaveList;
  329. case CallingConv::SwiftTail:
  330. if (!Is64Bit)
  331. return CSR_32_SaveList;
  332. return IsWin64 ? CSR_Win64_SwiftTail_SaveList : CSR_64_SwiftTail_SaveList;
  333. case CallingConv::X86_64_SysV:
  334. if (CallsEHReturn)
  335. return CSR_64EHRet_SaveList;
  336. return CSR_64_SaveList;
  337. case CallingConv::X86_INTR:
  338. if (Is64Bit) {
  339. if (HasAVX512)
  340. return CSR_64_AllRegs_AVX512_SaveList;
  341. if (HasAVX)
  342. return CSR_64_AllRegs_AVX_SaveList;
  343. if (HasSSE)
  344. return CSR_64_AllRegs_SaveList;
  345. return CSR_64_AllRegs_NoSSE_SaveList;
  346. } else {
  347. if (HasAVX512)
  348. return CSR_32_AllRegs_AVX512_SaveList;
  349. if (HasAVX)
  350. return CSR_32_AllRegs_AVX_SaveList;
  351. if (HasSSE)
  352. return CSR_32_AllRegs_SSE_SaveList;
  353. return CSR_32_AllRegs_SaveList;
  354. }
  355. default:
  356. break;
  357. }
  358. if (Is64Bit) {
  359. bool IsSwiftCC = Subtarget.getTargetLowering()->supportSwiftError() &&
  360. F.getAttributes().hasAttrSomewhere(Attribute::SwiftError);
  361. if (IsSwiftCC)
  362. return IsWin64 ? CSR_Win64_SwiftError_SaveList
  363. : CSR_64_SwiftError_SaveList;
  364. if (IsWin64)
  365. return HasSSE ? CSR_Win64_SaveList : CSR_Win64_NoSSE_SaveList;
  366. if (CallsEHReturn)
  367. return CSR_64EHRet_SaveList;
  368. return CSR_64_SaveList;
  369. }
  370. return CallsEHReturn ? CSR_32EHRet_SaveList : CSR_32_SaveList;
  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. const uint32_t *
  381. X86RegisterInfo::getCallPreservedMask(const MachineFunction &MF,
  382. CallingConv::ID CC) const {
  383. const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
  384. bool HasSSE = Subtarget.hasSSE1();
  385. bool HasAVX = Subtarget.hasAVX();
  386. bool HasAVX512 = Subtarget.hasAVX512();
  387. switch (CC) {
  388. case CallingConv::GHC:
  389. case CallingConv::HiPE:
  390. return CSR_NoRegs_RegMask;
  391. case CallingConv::AnyReg:
  392. if (HasAVX)
  393. return CSR_64_AllRegs_AVX_RegMask;
  394. return CSR_64_AllRegs_RegMask;
  395. case CallingConv::PreserveMost:
  396. return CSR_64_RT_MostRegs_RegMask;
  397. case CallingConv::PreserveAll:
  398. if (HasAVX)
  399. return CSR_64_RT_AllRegs_AVX_RegMask;
  400. return CSR_64_RT_AllRegs_RegMask;
  401. case CallingConv::CXX_FAST_TLS:
  402. if (Is64Bit)
  403. return CSR_64_TLS_Darwin_RegMask;
  404. break;
  405. case CallingConv::Intel_OCL_BI: {
  406. if (HasAVX512 && IsWin64)
  407. return CSR_Win64_Intel_OCL_BI_AVX512_RegMask;
  408. if (HasAVX512 && Is64Bit)
  409. return CSR_64_Intel_OCL_BI_AVX512_RegMask;
  410. if (HasAVX && IsWin64)
  411. return CSR_Win64_Intel_OCL_BI_AVX_RegMask;
  412. if (HasAVX && Is64Bit)
  413. return CSR_64_Intel_OCL_BI_AVX_RegMask;
  414. if (!HasAVX && !IsWin64 && Is64Bit)
  415. return CSR_64_Intel_OCL_BI_RegMask;
  416. break;
  417. }
  418. case CallingConv::HHVM:
  419. return CSR_64_HHVM_RegMask;
  420. case CallingConv::X86_RegCall:
  421. if (Is64Bit) {
  422. if (IsWin64) {
  423. return (HasSSE ? CSR_Win64_RegCall_RegMask :
  424. CSR_Win64_RegCall_NoSSE_RegMask);
  425. } else {
  426. return (HasSSE ? CSR_SysV64_RegCall_RegMask :
  427. CSR_SysV64_RegCall_NoSSE_RegMask);
  428. }
  429. } else {
  430. return (HasSSE ? CSR_32_RegCall_RegMask :
  431. CSR_32_RegCall_NoSSE_RegMask);
  432. }
  433. case CallingConv::CFGuard_Check:
  434. assert(!Is64Bit && "CFGuard check mechanism only used on 32-bit X86");
  435. return (HasSSE ? CSR_Win32_CFGuard_Check_RegMask
  436. : CSR_Win32_CFGuard_Check_NoSSE_RegMask);
  437. case CallingConv::Cold:
  438. if (Is64Bit)
  439. return CSR_64_MostRegs_RegMask;
  440. break;
  441. case CallingConv::Win64:
  442. return CSR_Win64_RegMask;
  443. case CallingConv::SwiftTail:
  444. if (!Is64Bit)
  445. return CSR_32_RegMask;
  446. return IsWin64 ? CSR_Win64_SwiftTail_RegMask : CSR_64_SwiftTail_RegMask;
  447. case CallingConv::X86_64_SysV:
  448. return CSR_64_RegMask;
  449. case CallingConv::X86_INTR:
  450. if (Is64Bit) {
  451. if (HasAVX512)
  452. return CSR_64_AllRegs_AVX512_RegMask;
  453. if (HasAVX)
  454. return CSR_64_AllRegs_AVX_RegMask;
  455. if (HasSSE)
  456. return CSR_64_AllRegs_RegMask;
  457. return CSR_64_AllRegs_NoSSE_RegMask;
  458. } else {
  459. if (HasAVX512)
  460. return CSR_32_AllRegs_AVX512_RegMask;
  461. if (HasAVX)
  462. return CSR_32_AllRegs_AVX_RegMask;
  463. if (HasSSE)
  464. return CSR_32_AllRegs_SSE_RegMask;
  465. return CSR_32_AllRegs_RegMask;
  466. }
  467. default:
  468. break;
  469. }
  470. // Unlike getCalleeSavedRegs(), we don't have MMI so we can't check
  471. // callsEHReturn().
  472. if (Is64Bit) {
  473. const Function &F = MF.getFunction();
  474. bool IsSwiftCC = Subtarget.getTargetLowering()->supportSwiftError() &&
  475. F.getAttributes().hasAttrSomewhere(Attribute::SwiftError);
  476. if (IsSwiftCC)
  477. return IsWin64 ? CSR_Win64_SwiftError_RegMask : CSR_64_SwiftError_RegMask;
  478. return IsWin64 ? CSR_Win64_RegMask : CSR_64_RegMask;
  479. }
  480. return CSR_32_RegMask;
  481. }
  482. const uint32_t*
  483. X86RegisterInfo::getNoPreservedMask() const {
  484. return CSR_NoRegs_RegMask;
  485. }
  486. const uint32_t *X86RegisterInfo::getDarwinTLSCallPreservedMask() const {
  487. return CSR_64_TLS_Darwin_RegMask;
  488. }
  489. BitVector X86RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
  490. BitVector Reserved(getNumRegs());
  491. const X86FrameLowering *TFI = getFrameLowering(MF);
  492. // Set the floating point control register as reserved.
  493. Reserved.set(X86::FPCW);
  494. // Set the floating point status register as reserved.
  495. Reserved.set(X86::FPSW);
  496. // Set the SIMD floating point control register as reserved.
  497. Reserved.set(X86::MXCSR);
  498. // Set the stack-pointer register and its aliases as reserved.
  499. for (const MCPhysReg &SubReg : subregs_inclusive(X86::RSP))
  500. Reserved.set(SubReg);
  501. // Set the Shadow Stack Pointer as reserved.
  502. Reserved.set(X86::SSP);
  503. // Set the instruction pointer register and its aliases as reserved.
  504. for (const MCPhysReg &SubReg : subregs_inclusive(X86::RIP))
  505. Reserved.set(SubReg);
  506. // Set the frame-pointer register and its aliases as reserved if needed.
  507. if (TFI->hasFP(MF)) {
  508. for (const MCPhysReg &SubReg : subregs_inclusive(X86::RBP))
  509. Reserved.set(SubReg);
  510. }
  511. // Set the base-pointer register and its aliases as reserved if needed.
  512. if (hasBasePointer(MF)) {
  513. CallingConv::ID CC = MF.getFunction().getCallingConv();
  514. const uint32_t *RegMask = getCallPreservedMask(MF, CC);
  515. if (MachineOperand::clobbersPhysReg(RegMask, getBaseRegister()))
  516. report_fatal_error(
  517. "Stack realignment in presence of dynamic allocas is not supported with"
  518. "this calling convention.");
  519. Register BasePtr = getX86SubSuperRegister(getBaseRegister(), 64);
  520. for (const MCPhysReg &SubReg : subregs_inclusive(BasePtr))
  521. Reserved.set(SubReg);
  522. }
  523. // Mark the segment registers as reserved.
  524. Reserved.set(X86::CS);
  525. Reserved.set(X86::SS);
  526. Reserved.set(X86::DS);
  527. Reserved.set(X86::ES);
  528. Reserved.set(X86::FS);
  529. Reserved.set(X86::GS);
  530. // Mark the floating point stack registers as reserved.
  531. for (unsigned n = 0; n != 8; ++n)
  532. Reserved.set(X86::ST0 + n);
  533. // Reserve the registers that only exist in 64-bit mode.
  534. if (!Is64Bit) {
  535. // These 8-bit registers are part of the x86-64 extension even though their
  536. // super-registers are old 32-bits.
  537. Reserved.set(X86::SIL);
  538. Reserved.set(X86::DIL);
  539. Reserved.set(X86::BPL);
  540. Reserved.set(X86::SPL);
  541. Reserved.set(X86::SIH);
  542. Reserved.set(X86::DIH);
  543. Reserved.set(X86::BPH);
  544. Reserved.set(X86::SPH);
  545. for (unsigned n = 0; n != 8; ++n) {
  546. // R8, R9, ...
  547. for (MCRegAliasIterator AI(X86::R8 + n, this, true); AI.isValid(); ++AI)
  548. Reserved.set(*AI);
  549. // XMM8, XMM9, ...
  550. for (MCRegAliasIterator AI(X86::XMM8 + n, this, true); AI.isValid(); ++AI)
  551. Reserved.set(*AI);
  552. }
  553. }
  554. if (!Is64Bit || !MF.getSubtarget<X86Subtarget>().hasAVX512()) {
  555. for (unsigned n = 16; n != 32; ++n) {
  556. for (MCRegAliasIterator AI(X86::XMM0 + n, this, true); AI.isValid(); ++AI)
  557. Reserved.set(*AI);
  558. }
  559. }
  560. assert(checkAllSuperRegsMarked(Reserved,
  561. {X86::SIL, X86::DIL, X86::BPL, X86::SPL,
  562. X86::SIH, X86::DIH, X86::BPH, X86::SPH}));
  563. return Reserved;
  564. }
  565. void X86RegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
  566. // Check if the EFLAGS register is marked as live-out. This shouldn't happen,
  567. // because the calling convention defines the EFLAGS register as NOT
  568. // preserved.
  569. //
  570. // Unfortunatelly the EFLAGS show up as live-out after branch folding. Adding
  571. // an assert to track this and clear the register afterwards to avoid
  572. // unnecessary crashes during release builds.
  573. assert(!(Mask[X86::EFLAGS / 32] & (1U << (X86::EFLAGS % 32))) &&
  574. "EFLAGS are not live-out from a patchpoint.");
  575. // Also clean other registers that don't need preserving (IP).
  576. for (auto Reg : {X86::EFLAGS, X86::RIP, X86::EIP, X86::IP})
  577. Mask[Reg / 32] &= ~(1U << (Reg % 32));
  578. }
  579. //===----------------------------------------------------------------------===//
  580. // Stack Frame Processing methods
  581. //===----------------------------------------------------------------------===//
  582. static bool CantUseSP(const MachineFrameInfo &MFI) {
  583. return MFI.hasVarSizedObjects() || MFI.hasOpaqueSPAdjustment();
  584. }
  585. bool X86RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
  586. const X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
  587. if (X86FI->hasPreallocatedCall())
  588. return true;
  589. const MachineFrameInfo &MFI = MF.getFrameInfo();
  590. if (!EnableBasePointer)
  591. return false;
  592. // When we need stack realignment, we can't address the stack from the frame
  593. // pointer. When we have dynamic allocas or stack-adjusting inline asm, we
  594. // can't address variables from the stack pointer. MS inline asm can
  595. // reference locals while also adjusting the stack pointer. When we can't
  596. // use both the SP and the FP, we need a separate base pointer register.
  597. bool CantUseFP = hasStackRealignment(MF);
  598. return CantUseFP && CantUseSP(MFI);
  599. }
  600. bool X86RegisterInfo::canRealignStack(const MachineFunction &MF) const {
  601. if (!TargetRegisterInfo::canRealignStack(MF))
  602. return false;
  603. const MachineFrameInfo &MFI = MF.getFrameInfo();
  604. const MachineRegisterInfo *MRI = &MF.getRegInfo();
  605. // Stack realignment requires a frame pointer. If we already started
  606. // register allocation with frame pointer elimination, it is too late now.
  607. if (!MRI->canReserveReg(FramePtr))
  608. return false;
  609. // If a base pointer is necessary. Check that it isn't too late to reserve
  610. // it.
  611. if (CantUseSP(MFI))
  612. return MRI->canReserveReg(BasePtr);
  613. return true;
  614. }
  615. // tryOptimizeLEAtoMOV - helper function that tries to replace a LEA instruction
  616. // of the form 'lea (%esp), %ebx' --> 'mov %esp, %ebx'.
  617. // TODO: In this case we should be really trying first to entirely eliminate
  618. // this instruction which is a plain copy.
  619. static bool tryOptimizeLEAtoMOV(MachineBasicBlock::iterator II) {
  620. MachineInstr &MI = *II;
  621. unsigned Opc = II->getOpcode();
  622. // Check if this is a LEA of the form 'lea (%esp), %ebx'
  623. if ((Opc != X86::LEA32r && Opc != X86::LEA64r && Opc != X86::LEA64_32r) ||
  624. MI.getOperand(2).getImm() != 1 ||
  625. MI.getOperand(3).getReg() != X86::NoRegister ||
  626. MI.getOperand(4).getImm() != 0 ||
  627. MI.getOperand(5).getReg() != X86::NoRegister)
  628. return false;
  629. Register BasePtr = MI.getOperand(1).getReg();
  630. // In X32 mode, ensure the base-pointer is a 32-bit operand, so the LEA will
  631. // be replaced with a 32-bit operand MOV which will zero extend the upper
  632. // 32-bits of the super register.
  633. if (Opc == X86::LEA64_32r)
  634. BasePtr = getX86SubSuperRegister(BasePtr, 32);
  635. Register NewDestReg = MI.getOperand(0).getReg();
  636. const X86InstrInfo *TII =
  637. MI.getParent()->getParent()->getSubtarget<X86Subtarget>().getInstrInfo();
  638. TII->copyPhysReg(*MI.getParent(), II, MI.getDebugLoc(), NewDestReg, BasePtr,
  639. MI.getOperand(1).isKill());
  640. MI.eraseFromParent();
  641. return true;
  642. }
  643. static bool isFuncletReturnInstr(MachineInstr &MI) {
  644. switch (MI.getOpcode()) {
  645. case X86::CATCHRET:
  646. case X86::CLEANUPRET:
  647. return true;
  648. default:
  649. return false;
  650. }
  651. llvm_unreachable("impossible");
  652. }
  653. void
  654. X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
  655. int SPAdj, unsigned FIOperandNum,
  656. RegScavenger *RS) const {
  657. MachineInstr &MI = *II;
  658. MachineBasicBlock &MBB = *MI.getParent();
  659. MachineFunction &MF = *MBB.getParent();
  660. MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
  661. bool IsEHFuncletEpilogue = MBBI == MBB.end() ? false
  662. : isFuncletReturnInstr(*MBBI);
  663. const X86FrameLowering *TFI = getFrameLowering(MF);
  664. int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
  665. // Determine base register and offset.
  666. int FIOffset;
  667. Register BasePtr;
  668. if (MI.isReturn()) {
  669. assert((!hasStackRealignment(MF) ||
  670. MF.getFrameInfo().isFixedObjectIndex(FrameIndex)) &&
  671. "Return instruction can only reference SP relative frame objects");
  672. FIOffset =
  673. TFI->getFrameIndexReferenceSP(MF, FrameIndex, BasePtr, 0).getFixed();
  674. } else if (TFI->Is64Bit && (MBB.isEHFuncletEntry() || IsEHFuncletEpilogue)) {
  675. FIOffset = TFI->getWin64EHFrameIndexRef(MF, FrameIndex, BasePtr);
  676. } else {
  677. FIOffset = TFI->getFrameIndexReference(MF, FrameIndex, BasePtr).getFixed();
  678. }
  679. // LOCAL_ESCAPE uses a single offset, with no register. It only works in the
  680. // simple FP case, and doesn't work with stack realignment. On 32-bit, the
  681. // offset is from the traditional base pointer location. On 64-bit, the
  682. // offset is from the SP at the end of the prologue, not the FP location. This
  683. // matches the behavior of llvm.frameaddress.
  684. unsigned Opc = MI.getOpcode();
  685. if (Opc == TargetOpcode::LOCAL_ESCAPE) {
  686. MachineOperand &FI = MI.getOperand(FIOperandNum);
  687. FI.ChangeToImmediate(FIOffset);
  688. return;
  689. }
  690. // For LEA64_32r when BasePtr is 32-bits (X32) we can use full-size 64-bit
  691. // register as source operand, semantic is the same and destination is
  692. // 32-bits. It saves one byte per lea in code since 0x67 prefix is avoided.
  693. // Don't change BasePtr since it is used later for stack adjustment.
  694. Register MachineBasePtr = BasePtr;
  695. if (Opc == X86::LEA64_32r && X86::GR32RegClass.contains(BasePtr))
  696. MachineBasePtr = getX86SubSuperRegister(BasePtr, 64);
  697. // This must be part of a four operand memory reference. Replace the
  698. // FrameIndex with base register. Add an offset to the offset.
  699. MI.getOperand(FIOperandNum).ChangeToRegister(MachineBasePtr, false);
  700. if (BasePtr == StackPtr)
  701. FIOffset += SPAdj;
  702. // The frame index format for stackmaps and patchpoints is different from the
  703. // X86 format. It only has a FI and an offset.
  704. if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
  705. assert(BasePtr == FramePtr && "Expected the FP as base register");
  706. int64_t Offset = MI.getOperand(FIOperandNum + 1).getImm() + FIOffset;
  707. MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
  708. return;
  709. }
  710. if (MI.getOperand(FIOperandNum+3).isImm()) {
  711. // Offset is a 32-bit integer.
  712. int Imm = (int)(MI.getOperand(FIOperandNum + 3).getImm());
  713. int Offset = FIOffset + Imm;
  714. assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) &&
  715. "Requesting 64-bit offset in 32-bit immediate!");
  716. if (Offset != 0 || !tryOptimizeLEAtoMOV(II))
  717. MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset);
  718. } else {
  719. // Offset is symbolic. This is extremely rare.
  720. uint64_t Offset = FIOffset +
  721. (uint64_t)MI.getOperand(FIOperandNum+3).getOffset();
  722. MI.getOperand(FIOperandNum + 3).setOffset(Offset);
  723. }
  724. }
  725. unsigned X86RegisterInfo::findDeadCallerSavedReg(
  726. MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI) const {
  727. const MachineFunction *MF = MBB.getParent();
  728. if (MF->callsEHReturn())
  729. return 0;
  730. const TargetRegisterClass &AvailableRegs = *getGPRsForTailCall(*MF);
  731. if (MBBI == MBB.end())
  732. return 0;
  733. switch (MBBI->getOpcode()) {
  734. default:
  735. return 0;
  736. case TargetOpcode::PATCHABLE_RET:
  737. case X86::RET:
  738. case X86::RET32:
  739. case X86::RET64:
  740. case X86::RETI32:
  741. case X86::RETI64:
  742. case X86::TCRETURNdi:
  743. case X86::TCRETURNri:
  744. case X86::TCRETURNmi:
  745. case X86::TCRETURNdi64:
  746. case X86::TCRETURNri64:
  747. case X86::TCRETURNmi64:
  748. case X86::EH_RETURN:
  749. case X86::EH_RETURN64: {
  750. SmallSet<uint16_t, 8> Uses;
  751. for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
  752. MachineOperand &MO = MBBI->getOperand(I);
  753. if (!MO.isReg() || MO.isDef())
  754. continue;
  755. Register Reg = MO.getReg();
  756. if (!Reg)
  757. continue;
  758. for (MCRegAliasIterator AI(Reg, this, true); AI.isValid(); ++AI)
  759. Uses.insert(*AI);
  760. }
  761. for (auto CS : AvailableRegs)
  762. if (!Uses.count(CS) && CS != X86::RIP && CS != X86::RSP && CS != X86::ESP)
  763. return CS;
  764. }
  765. }
  766. return 0;
  767. }
  768. Register X86RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
  769. const X86FrameLowering *TFI = getFrameLowering(MF);
  770. return TFI->hasFP(MF) ? FramePtr : StackPtr;
  771. }
  772. unsigned
  773. X86RegisterInfo::getPtrSizedFrameRegister(const MachineFunction &MF) const {
  774. const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
  775. Register FrameReg = getFrameRegister(MF);
  776. if (Subtarget.isTarget64BitILP32())
  777. FrameReg = getX86SubSuperRegister(FrameReg, 32);
  778. return FrameReg;
  779. }
  780. unsigned
  781. X86RegisterInfo::getPtrSizedStackRegister(const MachineFunction &MF) const {
  782. const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
  783. Register StackReg = getStackRegister();
  784. if (Subtarget.isTarget64BitILP32())
  785. StackReg = getX86SubSuperRegister(StackReg, 32);
  786. return StackReg;
  787. }
  788. static ShapeT getTileShape(Register VirtReg, VirtRegMap *VRM,
  789. const MachineRegisterInfo *MRI) {
  790. if (VRM->hasShape(VirtReg))
  791. return VRM->getShape(VirtReg);
  792. const MachineOperand &Def = *MRI->def_begin(VirtReg);
  793. MachineInstr *MI = const_cast<MachineInstr *>(Def.getParent());
  794. unsigned OpCode = MI->getOpcode();
  795. switch (OpCode) {
  796. default:
  797. llvm_unreachable("Unexpected machine instruction on tile register!");
  798. break;
  799. case X86::COPY: {
  800. Register SrcReg = MI->getOperand(1).getReg();
  801. ShapeT Shape = getTileShape(SrcReg, VRM, MRI);
  802. VRM->assignVirt2Shape(VirtReg, Shape);
  803. return Shape;
  804. }
  805. // We only collect the tile shape that is defined.
  806. case X86::PTILELOADDV:
  807. case X86::PTILELOADDT1V:
  808. case X86::PTDPBSSDV:
  809. case X86::PTDPBSUDV:
  810. case X86::PTDPBUSDV:
  811. case X86::PTDPBUUDV:
  812. case X86::PTILEZEROV:
  813. case X86::PTDPBF16PSV:
  814. MachineOperand &MO1 = MI->getOperand(1);
  815. MachineOperand &MO2 = MI->getOperand(2);
  816. ShapeT Shape(&MO1, &MO2, MRI);
  817. VRM->assignVirt2Shape(VirtReg, Shape);
  818. return Shape;
  819. }
  820. }
  821. bool X86RegisterInfo::getRegAllocationHints(Register VirtReg,
  822. ArrayRef<MCPhysReg> Order,
  823. SmallVectorImpl<MCPhysReg> &Hints,
  824. const MachineFunction &MF,
  825. const VirtRegMap *VRM,
  826. const LiveRegMatrix *Matrix) const {
  827. const MachineRegisterInfo *MRI = &MF.getRegInfo();
  828. const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
  829. bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
  830. VirtReg, Order, Hints, MF, VRM, Matrix);
  831. if (RC.getID() != X86::TILERegClassID)
  832. return BaseImplRetVal;
  833. ShapeT VirtShape = getTileShape(VirtReg, const_cast<VirtRegMap *>(VRM), MRI);
  834. auto AddHint = [&](MCPhysReg PhysReg) {
  835. Register VReg = Matrix->getOneVReg(PhysReg);
  836. if (VReg == MCRegister::NoRegister) { // Not allocated yet
  837. Hints.push_back(PhysReg);
  838. return;
  839. }
  840. ShapeT PhysShape = getTileShape(VReg, const_cast<VirtRegMap *>(VRM), MRI);
  841. if (PhysShape == VirtShape)
  842. Hints.push_back(PhysReg);
  843. };
  844. SmallSet<MCPhysReg, 4> CopyHints;
  845. CopyHints.insert(Hints.begin(), Hints.end());
  846. Hints.clear();
  847. for (auto Hint : CopyHints) {
  848. if (RC.contains(Hint) && !MRI->isReserved(Hint))
  849. AddHint(Hint);
  850. }
  851. for (MCPhysReg PhysReg : Order) {
  852. if (!CopyHints.count(PhysReg) && RC.contains(PhysReg) &&
  853. !MRI->isReserved(PhysReg))
  854. AddHint(PhysReg);
  855. }
  856. #define DEBUG_TYPE "tile-hint"
  857. LLVM_DEBUG({
  858. dbgs() << "Hints for virtual register " << format_hex(VirtReg, 8) << "\n";
  859. for (auto Hint : Hints) {
  860. dbgs() << "tmm" << Hint << ",";
  861. }
  862. dbgs() << "\n";
  863. });
  864. #undef DEBUG_TYPE
  865. return true;
  866. }