FixupStatepointCallerSaved.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. //===-- FixupStatepointCallerSaved.cpp - Fixup caller saved registers ----===//
  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. /// \file
  10. /// Statepoint instruction in deopt parameters contains values which are
  11. /// meaningful to the runtime and should be able to be read at the moment the
  12. /// call returns. So we can say that we need to encode the fact that these
  13. /// values are "late read" by runtime. If we could express this notion for
  14. /// register allocator it would produce the right form for us.
  15. /// The need to fixup (i.e this pass) is specifically handling the fact that
  16. /// we cannot describe such a late read for the register allocator.
  17. /// Register allocator may put the value on a register clobbered by the call.
  18. /// This pass forces the spill of such registers and replaces corresponding
  19. /// statepoint operands to added spill slots.
  20. ///
  21. //===----------------------------------------------------------------------===//
  22. #include "llvm/ADT/SmallSet.h"
  23. #include "llvm/ADT/Statistic.h"
  24. #include "llvm/CodeGen/MachineFrameInfo.h"
  25. #include "llvm/CodeGen/MachineFunctionPass.h"
  26. #include "llvm/CodeGen/MachineRegisterInfo.h"
  27. #include "llvm/CodeGen/Passes.h"
  28. #include "llvm/CodeGen/StackMaps.h"
  29. #include "llvm/CodeGen/TargetFrameLowering.h"
  30. #include "llvm/CodeGen/TargetInstrInfo.h"
  31. #include "llvm/IR/Statepoint.h"
  32. #include "llvm/InitializePasses.h"
  33. #include "llvm/Support/Debug.h"
  34. using namespace llvm;
  35. #define DEBUG_TYPE "fixup-statepoint-caller-saved"
  36. STATISTIC(NumSpilledRegisters, "Number of spilled register");
  37. STATISTIC(NumSpillSlotsAllocated, "Number of spill slots allocated");
  38. STATISTIC(NumSpillSlotsExtended, "Number of spill slots extended");
  39. static cl::opt<bool> FixupSCSExtendSlotSize(
  40. "fixup-scs-extend-slot-size", cl::Hidden, cl::init(false),
  41. cl::desc("Allow spill in spill slot of greater size than register size"),
  42. cl::Hidden);
  43. static cl::opt<bool> PassGCPtrInCSR(
  44. "fixup-allow-gcptr-in-csr", cl::Hidden, cl::init(false),
  45. cl::desc("Allow passing GC Pointer arguments in callee saved registers"));
  46. static cl::opt<bool> EnableCopyProp(
  47. "fixup-scs-enable-copy-propagation", cl::Hidden, cl::init(true),
  48. cl::desc("Enable simple copy propagation during register reloading"));
  49. // This is purely debugging option.
  50. // It may be handy for investigating statepoint spilling issues.
  51. static cl::opt<unsigned> MaxStatepointsWithRegs(
  52. "fixup-max-csr-statepoints", cl::Hidden,
  53. cl::desc("Max number of statepoints allowed to pass GC Ptrs in registers"));
  54. namespace {
  55. class FixupStatepointCallerSaved : public MachineFunctionPass {
  56. public:
  57. static char ID;
  58. FixupStatepointCallerSaved() : MachineFunctionPass(ID) {
  59. initializeFixupStatepointCallerSavedPass(*PassRegistry::getPassRegistry());
  60. }
  61. void getAnalysisUsage(AnalysisUsage &AU) const override {
  62. AU.setPreservesCFG();
  63. MachineFunctionPass::getAnalysisUsage(AU);
  64. }
  65. StringRef getPassName() const override {
  66. return "Fixup Statepoint Caller Saved";
  67. }
  68. bool runOnMachineFunction(MachineFunction &MF) override;
  69. };
  70. } // End anonymous namespace.
  71. char FixupStatepointCallerSaved::ID = 0;
  72. char &llvm::FixupStatepointCallerSavedID = FixupStatepointCallerSaved::ID;
  73. INITIALIZE_PASS_BEGIN(FixupStatepointCallerSaved, DEBUG_TYPE,
  74. "Fixup Statepoint Caller Saved", false, false)
  75. INITIALIZE_PASS_END(FixupStatepointCallerSaved, DEBUG_TYPE,
  76. "Fixup Statepoint Caller Saved", false, false)
  77. // Utility function to get size of the register.
  78. static unsigned getRegisterSize(const TargetRegisterInfo &TRI, Register Reg) {
  79. const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg);
  80. return TRI.getSpillSize(*RC);
  81. }
  82. // Try to eliminate redundant copy to register which we're going to
  83. // spill, i.e. try to change:
  84. // X = COPY Y
  85. // SPILL X
  86. // to
  87. // SPILL Y
  88. // If there are no uses of X between copy and STATEPOINT, that COPY
  89. // may be eliminated.
  90. // Reg - register we're about to spill
  91. // RI - On entry points to statepoint.
  92. // On successful copy propagation set to new spill point.
  93. // IsKill - set to true if COPY is Kill (there are no uses of Y)
  94. // Returns either found source copy register or original one.
  95. static Register performCopyPropagation(Register Reg,
  96. MachineBasicBlock::iterator &RI,
  97. bool &IsKill, const TargetInstrInfo &TII,
  98. const TargetRegisterInfo &TRI) {
  99. // First check if statepoint itself uses Reg in non-meta operands.
  100. int Idx = RI->findRegisterUseOperandIdx(Reg, false, &TRI);
  101. if (Idx >= 0 && (unsigned)Idx < StatepointOpers(&*RI).getNumDeoptArgsIdx()) {
  102. IsKill = false;
  103. return Reg;
  104. }
  105. if (!EnableCopyProp)
  106. return Reg;
  107. MachineBasicBlock *MBB = RI->getParent();
  108. MachineBasicBlock::reverse_iterator E = MBB->rend();
  109. MachineInstr *Def = nullptr, *Use = nullptr;
  110. for (auto It = ++(RI.getReverse()); It != E; ++It) {
  111. if (It->readsRegister(Reg, &TRI) && !Use)
  112. Use = &*It;
  113. if (It->modifiesRegister(Reg, &TRI)) {
  114. Def = &*It;
  115. break;
  116. }
  117. }
  118. if (!Def)
  119. return Reg;
  120. auto DestSrc = TII.isCopyInstr(*Def);
  121. if (!DestSrc || DestSrc->Destination->getReg() != Reg)
  122. return Reg;
  123. Register SrcReg = DestSrc->Source->getReg();
  124. if (getRegisterSize(TRI, Reg) != getRegisterSize(TRI, SrcReg))
  125. return Reg;
  126. LLVM_DEBUG(dbgs() << "spillRegisters: perform copy propagation "
  127. << printReg(Reg, &TRI) << " -> " << printReg(SrcReg, &TRI)
  128. << "\n");
  129. // Insert spill immediately after Def
  130. RI = ++MachineBasicBlock::iterator(Def);
  131. IsKill = DestSrc->Source->isKill();
  132. // There are no uses of original register between COPY and STATEPOINT.
  133. // There can't be any after STATEPOINT, so we can eliminate Def.
  134. if (!Use) {
  135. LLVM_DEBUG(dbgs() << "spillRegisters: removing dead copy " << *Def);
  136. Def->eraseFromParent();
  137. }
  138. return SrcReg;
  139. }
  140. namespace {
  141. // Pair {Register, FrameIndex}
  142. using RegSlotPair = std::pair<Register, int>;
  143. // Keeps track of what reloads were inserted in MBB.
  144. class RegReloadCache {
  145. using ReloadSet = SmallSet<RegSlotPair, 8>;
  146. DenseMap<const MachineBasicBlock *, ReloadSet> Reloads;
  147. public:
  148. RegReloadCache() = default;
  149. // Record reload of Reg from FI in block MBB
  150. void recordReload(Register Reg, int FI, const MachineBasicBlock *MBB) {
  151. RegSlotPair RSP(Reg, FI);
  152. auto Res = Reloads[MBB].insert(RSP);
  153. (void)Res;
  154. assert(Res.second && "reload already exists");
  155. }
  156. // Does basic block MBB contains reload of Reg from FI?
  157. bool hasReload(Register Reg, int FI, const MachineBasicBlock *MBB) {
  158. RegSlotPair RSP(Reg, FI);
  159. return Reloads.count(MBB) && Reloads[MBB].count(RSP);
  160. }
  161. };
  162. // Cache used frame indexes during statepoint re-write to re-use them in
  163. // processing next statepoint instruction.
  164. // Two strategies. One is to preserve the size of spill slot while another one
  165. // extends the size of spill slots to reduce the number of them, causing
  166. // the less total frame size. But unspill will have "implicit" any extend.
  167. class FrameIndexesCache {
  168. private:
  169. struct FrameIndexesPerSize {
  170. // List of used frame indexes during processing previous statepoints.
  171. SmallVector<int, 8> Slots;
  172. // Current index of un-used yet frame index.
  173. unsigned Index = 0;
  174. };
  175. MachineFrameInfo &MFI;
  176. const TargetRegisterInfo &TRI;
  177. // Map size to list of frame indexes of this size. If the mode is
  178. // FixupSCSExtendSlotSize then the key 0 is used to keep all frame indexes.
  179. // If the size of required spill slot is greater than in a cache then the
  180. // size will be increased.
  181. DenseMap<unsigned, FrameIndexesPerSize> Cache;
  182. // Keeps track of slots reserved for the shared landing pad processing.
  183. // Initialized from GlobalIndices for the current EHPad.
  184. SmallSet<int, 8> ReservedSlots;
  185. // Landing pad can be destination of several statepoints. Every register
  186. // defined by such statepoints must be spilled to the same stack slot.
  187. // This map keeps that information.
  188. DenseMap<const MachineBasicBlock *, SmallVector<RegSlotPair, 8>>
  189. GlobalIndices;
  190. FrameIndexesPerSize &getCacheBucket(unsigned Size) {
  191. // In FixupSCSExtendSlotSize mode the bucket with 0 index is used
  192. // for all sizes.
  193. return Cache[FixupSCSExtendSlotSize ? 0 : Size];
  194. }
  195. public:
  196. FrameIndexesCache(MachineFrameInfo &MFI, const TargetRegisterInfo &TRI)
  197. : MFI(MFI), TRI(TRI) {}
  198. // Reset the current state of used frame indexes. After invocation of
  199. // this function all frame indexes are available for allocation with
  200. // the exception of slots reserved for landing pad processing (if any).
  201. void reset(const MachineBasicBlock *EHPad) {
  202. for (auto &It : Cache)
  203. It.second.Index = 0;
  204. ReservedSlots.clear();
  205. if (EHPad && GlobalIndices.count(EHPad))
  206. for (auto &RSP : GlobalIndices[EHPad])
  207. ReservedSlots.insert(RSP.second);
  208. }
  209. // Get frame index to spill the register.
  210. int getFrameIndex(Register Reg, MachineBasicBlock *EHPad) {
  211. // Check if slot for Reg is already reserved at EHPad.
  212. auto It = GlobalIndices.find(EHPad);
  213. if (It != GlobalIndices.end()) {
  214. auto &Vec = It->second;
  215. auto Idx = llvm::find_if(
  216. Vec, [Reg](RegSlotPair &RSP) { return Reg == RSP.first; });
  217. if (Idx != Vec.end()) {
  218. int FI = Idx->second;
  219. LLVM_DEBUG(dbgs() << "Found global FI " << FI << " for register "
  220. << printReg(Reg, &TRI) << " at "
  221. << printMBBReference(*EHPad) << "\n");
  222. assert(ReservedSlots.count(FI) && "using unreserved slot");
  223. return FI;
  224. }
  225. }
  226. unsigned Size = getRegisterSize(TRI, Reg);
  227. FrameIndexesPerSize &Line = getCacheBucket(Size);
  228. while (Line.Index < Line.Slots.size()) {
  229. int FI = Line.Slots[Line.Index++];
  230. if (ReservedSlots.count(FI))
  231. continue;
  232. // If all sizes are kept together we probably need to extend the
  233. // spill slot size.
  234. if (MFI.getObjectSize(FI) < Size) {
  235. MFI.setObjectSize(FI, Size);
  236. MFI.setObjectAlignment(FI, Align(Size));
  237. NumSpillSlotsExtended++;
  238. }
  239. return FI;
  240. }
  241. int FI = MFI.CreateSpillStackObject(Size, Align(Size));
  242. NumSpillSlotsAllocated++;
  243. Line.Slots.push_back(FI);
  244. ++Line.Index;
  245. // Remember assignment {Reg, FI} for EHPad
  246. if (EHPad) {
  247. GlobalIndices[EHPad].push_back(std::make_pair(Reg, FI));
  248. LLVM_DEBUG(dbgs() << "Reserved FI " << FI << " for spilling reg "
  249. << printReg(Reg, &TRI) << " at landing pad "
  250. << printMBBReference(*EHPad) << "\n");
  251. }
  252. return FI;
  253. }
  254. // Sort all registers to spill in descendent order. In the
  255. // FixupSCSExtendSlotSize mode it will minimize the total frame size.
  256. // In non FixupSCSExtendSlotSize mode we can skip this step.
  257. void sortRegisters(SmallVectorImpl<Register> &Regs) {
  258. if (!FixupSCSExtendSlotSize)
  259. return;
  260. llvm::sort(Regs, [&](Register &A, Register &B) {
  261. return getRegisterSize(TRI, A) > getRegisterSize(TRI, B);
  262. });
  263. }
  264. };
  265. // Describes the state of the current processing statepoint instruction.
  266. class StatepointState {
  267. private:
  268. // statepoint instruction.
  269. MachineInstr &MI;
  270. MachineFunction &MF;
  271. // If non-null then statepoint is invoke, and this points to the landing pad.
  272. MachineBasicBlock *EHPad;
  273. const TargetRegisterInfo &TRI;
  274. const TargetInstrInfo &TII;
  275. MachineFrameInfo &MFI;
  276. // Mask with callee saved registers.
  277. const uint32_t *Mask;
  278. // Cache of frame indexes used on previous instruction processing.
  279. FrameIndexesCache &CacheFI;
  280. bool AllowGCPtrInCSR;
  281. // Operands with physical registers requiring spilling.
  282. SmallVector<unsigned, 8> OpsToSpill;
  283. // Set of register to spill.
  284. SmallVector<Register, 8> RegsToSpill;
  285. // Set of registers to reload after statepoint.
  286. SmallVector<Register, 8> RegsToReload;
  287. // Map Register to Frame Slot index.
  288. DenseMap<Register, int> RegToSlotIdx;
  289. public:
  290. StatepointState(MachineInstr &MI, const uint32_t *Mask,
  291. FrameIndexesCache &CacheFI, bool AllowGCPtrInCSR)
  292. : MI(MI), MF(*MI.getMF()), TRI(*MF.getSubtarget().getRegisterInfo()),
  293. TII(*MF.getSubtarget().getInstrInfo()), MFI(MF.getFrameInfo()),
  294. Mask(Mask), CacheFI(CacheFI), AllowGCPtrInCSR(AllowGCPtrInCSR) {
  295. // Find statepoint's landing pad, if any.
  296. EHPad = nullptr;
  297. MachineBasicBlock *MBB = MI.getParent();
  298. // Invoke statepoint must be last one in block.
  299. bool Last = std::none_of(++MI.getIterator(), MBB->end().getInstrIterator(),
  300. [](MachineInstr &I) {
  301. return I.getOpcode() == TargetOpcode::STATEPOINT;
  302. });
  303. if (!Last)
  304. return;
  305. auto IsEHPad = [](MachineBasicBlock *B) { return B->isEHPad(); };
  306. assert(llvm::count_if(MBB->successors(), IsEHPad) < 2 && "multiple EHPads");
  307. auto It = llvm::find_if(MBB->successors(), IsEHPad);
  308. if (It != MBB->succ_end())
  309. EHPad = *It;
  310. }
  311. MachineBasicBlock *getEHPad() const { return EHPad; }
  312. // Return true if register is callee saved.
  313. bool isCalleeSaved(Register Reg) { return (Mask[Reg / 32] >> Reg % 32) & 1; }
  314. // Iterates over statepoint meta args to find caller saver registers.
  315. // Also cache the size of found registers.
  316. // Returns true if caller save registers found.
  317. bool findRegistersToSpill() {
  318. SmallSet<Register, 8> GCRegs;
  319. // All GC pointer operands assigned to registers produce new value.
  320. // Since they're tied to their defs, it is enough to collect def registers.
  321. for (const auto &Def : MI.defs())
  322. GCRegs.insert(Def.getReg());
  323. SmallSet<Register, 8> VisitedRegs;
  324. for (unsigned Idx = StatepointOpers(&MI).getVarIdx(),
  325. EndIdx = MI.getNumOperands();
  326. Idx < EndIdx; ++Idx) {
  327. MachineOperand &MO = MI.getOperand(Idx);
  328. // Leave `undef` operands as is, StackMaps will rewrite them
  329. // into a constant.
  330. if (!MO.isReg() || MO.isImplicit() || MO.isUndef())
  331. continue;
  332. Register Reg = MO.getReg();
  333. assert(Reg.isPhysical() && "Only physical regs are expected");
  334. if (isCalleeSaved(Reg) && (AllowGCPtrInCSR || !is_contained(GCRegs, Reg)))
  335. continue;
  336. LLVM_DEBUG(dbgs() << "Will spill " << printReg(Reg, &TRI) << " at index "
  337. << Idx << "\n");
  338. if (VisitedRegs.insert(Reg).second)
  339. RegsToSpill.push_back(Reg);
  340. OpsToSpill.push_back(Idx);
  341. }
  342. CacheFI.sortRegisters(RegsToSpill);
  343. return !RegsToSpill.empty();
  344. }
  345. // Spill all caller saved registers right before statepoint instruction.
  346. // Remember frame index where register is spilled.
  347. void spillRegisters() {
  348. for (Register Reg : RegsToSpill) {
  349. int FI = CacheFI.getFrameIndex(Reg, EHPad);
  350. const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg);
  351. NumSpilledRegisters++;
  352. RegToSlotIdx[Reg] = FI;
  353. LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, &TRI) << " to FI " << FI
  354. << "\n");
  355. // Perform trivial copy propagation
  356. bool IsKill = true;
  357. MachineBasicBlock::iterator InsertBefore(MI);
  358. Reg = performCopyPropagation(Reg, InsertBefore, IsKill, TII, TRI);
  359. LLVM_DEBUG(dbgs() << "Insert spill before " << *InsertBefore);
  360. TII.storeRegToStackSlot(*MI.getParent(), InsertBefore, Reg, IsKill, FI,
  361. RC, &TRI);
  362. }
  363. }
  364. void insertReloadBefore(unsigned Reg, MachineBasicBlock::iterator It,
  365. MachineBasicBlock *MBB) {
  366. const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg);
  367. int FI = RegToSlotIdx[Reg];
  368. if (It != MBB->end()) {
  369. TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, &TRI);
  370. return;
  371. }
  372. // To insert reload at the end of MBB, insert it before last instruction
  373. // and then swap them.
  374. assert(!MBB->empty() && "Empty block");
  375. --It;
  376. TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, &TRI);
  377. MachineInstr *Reload = It->getPrevNode();
  378. int Dummy = 0;
  379. (void)Dummy;
  380. assert(TII.isLoadFromStackSlot(*Reload, Dummy) == Reg);
  381. assert(Dummy == FI);
  382. MBB->remove(Reload);
  383. MBB->insertAfter(It, Reload);
  384. }
  385. // Insert reloads of (relocated) registers spilled in statepoint.
  386. void insertReloads(MachineInstr *NewStatepoint, RegReloadCache &RC) {
  387. MachineBasicBlock *MBB = NewStatepoint->getParent();
  388. auto InsertPoint = std::next(NewStatepoint->getIterator());
  389. for (auto Reg : RegsToReload) {
  390. insertReloadBefore(Reg, InsertPoint, MBB);
  391. LLVM_DEBUG(dbgs() << "Reloading " << printReg(Reg, &TRI) << " from FI "
  392. << RegToSlotIdx[Reg] << " after statepoint\n");
  393. if (EHPad && !RC.hasReload(Reg, RegToSlotIdx[Reg], EHPad)) {
  394. RC.recordReload(Reg, RegToSlotIdx[Reg], EHPad);
  395. auto EHPadInsertPoint = EHPad->SkipPHIsLabelsAndDebug(EHPad->begin());
  396. insertReloadBefore(Reg, EHPadInsertPoint, EHPad);
  397. LLVM_DEBUG(dbgs() << "...also reload at EHPad "
  398. << printMBBReference(*EHPad) << "\n");
  399. }
  400. }
  401. }
  402. // Re-write statepoint machine instruction to replace caller saved operands
  403. // with indirect memory location (frame index).
  404. MachineInstr *rewriteStatepoint() {
  405. MachineInstr *NewMI =
  406. MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true);
  407. MachineInstrBuilder MIB(MF, NewMI);
  408. unsigned NumOps = MI.getNumOperands();
  409. // New indices for the remaining defs.
  410. SmallVector<unsigned, 8> NewIndices;
  411. unsigned NumDefs = MI.getNumDefs();
  412. for (unsigned I = 0; I < NumDefs; ++I) {
  413. MachineOperand &DefMO = MI.getOperand(I);
  414. assert(DefMO.isReg() && DefMO.isDef() && "Expected Reg Def operand");
  415. Register Reg = DefMO.getReg();
  416. assert(DefMO.isTied() && "Def is expected to be tied");
  417. // We skipped undef uses and did not spill them, so we should not
  418. // proceed with defs here.
  419. if (MI.getOperand(MI.findTiedOperandIdx(I)).isUndef()) {
  420. if (AllowGCPtrInCSR) {
  421. NewIndices.push_back(NewMI->getNumOperands());
  422. MIB.addReg(Reg, RegState::Define);
  423. }
  424. continue;
  425. }
  426. if (!AllowGCPtrInCSR) {
  427. assert(is_contained(RegsToSpill, Reg));
  428. RegsToReload.push_back(Reg);
  429. } else {
  430. if (isCalleeSaved(Reg)) {
  431. NewIndices.push_back(NewMI->getNumOperands());
  432. MIB.addReg(Reg, RegState::Define);
  433. } else {
  434. NewIndices.push_back(NumOps);
  435. RegsToReload.push_back(Reg);
  436. }
  437. }
  438. }
  439. // Add End marker.
  440. OpsToSpill.push_back(MI.getNumOperands());
  441. unsigned CurOpIdx = 0;
  442. for (unsigned I = NumDefs; I < MI.getNumOperands(); ++I) {
  443. MachineOperand &MO = MI.getOperand(I);
  444. if (I == OpsToSpill[CurOpIdx]) {
  445. int FI = RegToSlotIdx[MO.getReg()];
  446. MIB.addImm(StackMaps::IndirectMemRefOp);
  447. MIB.addImm(getRegisterSize(TRI, MO.getReg()));
  448. assert(MO.isReg() && "Should be register");
  449. assert(MO.getReg().isPhysical() && "Should be physical register");
  450. MIB.addFrameIndex(FI);
  451. MIB.addImm(0);
  452. ++CurOpIdx;
  453. } else {
  454. MIB.add(MO);
  455. unsigned OldDef;
  456. if (AllowGCPtrInCSR && MI.isRegTiedToDefOperand(I, &OldDef)) {
  457. assert(OldDef < NumDefs);
  458. assert(NewIndices[OldDef] < NumOps);
  459. MIB->tieOperands(NewIndices[OldDef], MIB->getNumOperands() - 1);
  460. }
  461. }
  462. }
  463. assert(CurOpIdx == (OpsToSpill.size() - 1) && "Not all operands processed");
  464. // Add mem operands.
  465. NewMI->setMemRefs(MF, MI.memoperands());
  466. for (auto It : RegToSlotIdx) {
  467. Register R = It.first;
  468. int FrameIndex = It.second;
  469. auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
  470. MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad;
  471. if (is_contained(RegsToReload, R))
  472. Flags |= MachineMemOperand::MOStore;
  473. auto *MMO =
  474. MF.getMachineMemOperand(PtrInfo, Flags, getRegisterSize(TRI, R),
  475. MFI.getObjectAlign(FrameIndex));
  476. NewMI->addMemOperand(MF, MMO);
  477. }
  478. // Insert new statepoint and erase old one.
  479. MI.getParent()->insert(MI, NewMI);
  480. LLVM_DEBUG(dbgs() << "rewritten statepoint to : " << *NewMI << "\n");
  481. MI.eraseFromParent();
  482. return NewMI;
  483. }
  484. };
  485. class StatepointProcessor {
  486. private:
  487. MachineFunction &MF;
  488. const TargetRegisterInfo &TRI;
  489. FrameIndexesCache CacheFI;
  490. RegReloadCache ReloadCache;
  491. public:
  492. StatepointProcessor(MachineFunction &MF)
  493. : MF(MF), TRI(*MF.getSubtarget().getRegisterInfo()),
  494. CacheFI(MF.getFrameInfo(), TRI) {}
  495. bool process(MachineInstr &MI, bool AllowGCPtrInCSR) {
  496. StatepointOpers SO(&MI);
  497. uint64_t Flags = SO.getFlags();
  498. // Do nothing for LiveIn, it supports all registers.
  499. if (Flags & (uint64_t)StatepointFlags::DeoptLiveIn)
  500. return false;
  501. LLVM_DEBUG(dbgs() << "\nMBB " << MI.getParent()->getNumber() << " "
  502. << MI.getParent()->getName() << " : process statepoint "
  503. << MI);
  504. CallingConv::ID CC = SO.getCallingConv();
  505. const uint32_t *Mask = TRI.getCallPreservedMask(MF, CC);
  506. StatepointState SS(MI, Mask, CacheFI, AllowGCPtrInCSR);
  507. CacheFI.reset(SS.getEHPad());
  508. if (!SS.findRegistersToSpill())
  509. return false;
  510. SS.spillRegisters();
  511. auto *NewStatepoint = SS.rewriteStatepoint();
  512. SS.insertReloads(NewStatepoint, ReloadCache);
  513. return true;
  514. }
  515. };
  516. } // namespace
  517. bool FixupStatepointCallerSaved::runOnMachineFunction(MachineFunction &MF) {
  518. if (skipFunction(MF.getFunction()))
  519. return false;
  520. const Function &F = MF.getFunction();
  521. if (!F.hasGC())
  522. return false;
  523. SmallVector<MachineInstr *, 16> Statepoints;
  524. for (MachineBasicBlock &BB : MF)
  525. for (MachineInstr &I : BB)
  526. if (I.getOpcode() == TargetOpcode::STATEPOINT)
  527. Statepoints.push_back(&I);
  528. if (Statepoints.empty())
  529. return false;
  530. bool Changed = false;
  531. StatepointProcessor SPP(MF);
  532. unsigned NumStatepoints = 0;
  533. bool AllowGCPtrInCSR = PassGCPtrInCSR;
  534. for (MachineInstr *I : Statepoints) {
  535. ++NumStatepoints;
  536. if (MaxStatepointsWithRegs.getNumOccurrences() &&
  537. NumStatepoints >= MaxStatepointsWithRegs)
  538. AllowGCPtrInCSR = false;
  539. Changed |= SPP.process(*I, AllowGCPtrInCSR);
  540. }
  541. return Changed;
  542. }