FixupStatepointCallerSaved.cpp 22 KB

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