FixupStatepointCallerSaved.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. //===-- FixupStatepointCallerSaved.cpp - Fixup caller saved registers ----===//
  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. /// \file
  11. /// Statepoint instruction in deopt parameters contains values which are
  12. /// meaningful to the runtime and should be able to be read at the moment the
  13. /// call returns. So we can say that we need to encode the fact that these
  14. /// values are "late read" by runtime. If we could express this notion for
  15. /// register allocator it would produce the right form for us.
  16. /// The need to fixup (i.e this pass) is specifically handling the fact that
  17. /// we cannot describe such a late read for the register allocator.
  18. /// Register allocator may put the value on a register clobbered by the call.
  19. /// This pass forces the spill of such registers and replaces corresponding
  20. /// statepoint operands to added spill slots.
  21. ///
  22. //===----------------------------------------------------------------------===//
  23. #include "llvm/ADT/SmallSet.h"
  24. #include "llvm/ADT/Statistic.h"
  25. #include "llvm/CodeGen/MachineFrameInfo.h"
  26. #include "llvm/CodeGen/MachineFunctionPass.h"
  27. #include "llvm/CodeGen/MachineRegisterInfo.h"
  28. #include "llvm/CodeGen/Passes.h"
  29. #include "llvm/CodeGen/StackMaps.h"
  30. #include "llvm/CodeGen/TargetFrameLowering.h"
  31. #include "llvm/CodeGen/TargetInstrInfo.h"
  32. #include "llvm/IR/Statepoint.h"
  33. #include "llvm/InitializePasses.h"
  34. #include "llvm/Support/Debug.h"
  35. using namespace llvm;
  36. #define DEBUG_TYPE "fixup-statepoint-caller-saved"
  37. STATISTIC(NumSpilledRegisters, "Number of spilled register");
  38. STATISTIC(NumSpillSlotsAllocated, "Number of spill slots allocated");
  39. STATISTIC(NumSpillSlotsExtended, "Number of spill slots extended");
  40. static cl::opt<bool> FixupSCSExtendSlotSize(
  41. "fixup-scs-extend-slot-size", cl::Hidden, cl::init(false),
  42. cl::desc("Allow spill in spill slot of greater size than register size"),
  43. cl::Hidden);
  44. static cl::opt<bool> PassGCPtrInCSR(
  45. "fixup-allow-gcptr-in-csr", cl::Hidden, cl::init(false),
  46. cl::desc("Allow passing GC Pointer arguments in callee saved registers"));
  47. static cl::opt<bool> EnableCopyProp(
  48. "fixup-scs-enable-copy-propagation", cl::Hidden, cl::init(true),
  49. cl::desc("Enable simple copy propagation during register reloading"));
  50. // This is purely debugging option.
  51. // It may be handy for investigating statepoint spilling issues.
  52. static cl::opt<unsigned> MaxStatepointsWithRegs(
  53. "fixup-max-csr-statepoints", cl::Hidden,
  54. cl::desc("Max number of statepoints allowed to pass GC Ptrs in registers"));
  55. namespace {
  56. class FixupStatepointCallerSaved : public MachineFunctionPass {
  57. public:
  58. static char ID;
  59. FixupStatepointCallerSaved() : MachineFunctionPass(ID) {
  60. initializeFixupStatepointCallerSavedPass(*PassRegistry::getPassRegistry());
  61. }
  62. void getAnalysisUsage(AnalysisUsage &AU) const override {
  63. AU.setPreservesCFG();
  64. MachineFunctionPass::getAnalysisUsage(AU);
  65. }
  66. StringRef getPassName() const override {
  67. return "Fixup Statepoint Caller Saved";
  68. }
  69. bool runOnMachineFunction(MachineFunction &MF) override;
  70. };
  71. } // End anonymous namespace.
  72. char FixupStatepointCallerSaved::ID = 0;
  73. char &llvm::FixupStatepointCallerSavedID = FixupStatepointCallerSaved::ID;
  74. INITIALIZE_PASS_BEGIN(FixupStatepointCallerSaved, DEBUG_TYPE,
  75. "Fixup Statepoint Caller Saved", false, false)
  76. INITIALIZE_PASS_END(FixupStatepointCallerSaved, DEBUG_TYPE,
  77. "Fixup Statepoint Caller Saved", false, false)
  78. // Utility function to get size of the register.
  79. static unsigned getRegisterSize(const TargetRegisterInfo &TRI, Register Reg) {
  80. const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg);
  81. return TRI.getSpillSize(*RC);
  82. }
  83. // Try to eliminate redundant copy to register which we're going to
  84. // spill, i.e. try to change:
  85. // X = COPY Y
  86. // SPILL X
  87. // to
  88. // SPILL Y
  89. // If there are no uses of X between copy and STATEPOINT, that COPY
  90. // may be eliminated.
  91. // Reg - register we're about to spill
  92. // RI - On entry points to statepoint.
  93. // On successful copy propagation set to new spill point.
  94. // IsKill - set to true if COPY is Kill (there are no uses of Y)
  95. // Returns either found source copy register or original one.
  96. static Register performCopyPropagation(Register Reg,
  97. MachineBasicBlock::iterator &RI,
  98. bool &IsKill, const TargetInstrInfo &TII,
  99. const TargetRegisterInfo &TRI) {
  100. // First check if statepoint itself uses Reg in non-meta operands.
  101. int Idx = RI->findRegisterUseOperandIdx(Reg, false, &TRI);
  102. if (Idx >= 0 && (unsigned)Idx < StatepointOpers(&*RI).getNumDeoptArgsIdx()) {
  103. IsKill = false;
  104. return Reg;
  105. }
  106. if (!EnableCopyProp)
  107. return Reg;
  108. MachineBasicBlock *MBB = RI->getParent();
  109. MachineBasicBlock::reverse_iterator E = MBB->rend();
  110. MachineInstr *Def = nullptr, *Use = nullptr;
  111. for (auto It = ++(RI.getReverse()); It != E; ++It) {
  112. if (It->readsRegister(Reg, &TRI) && !Use)
  113. Use = &*It;
  114. if (It->modifiesRegister(Reg, &TRI)) {
  115. Def = &*It;
  116. break;
  117. }
  118. }
  119. if (!Def)
  120. return Reg;
  121. auto DestSrc = TII.isCopyInstr(*Def);
  122. if (!DestSrc || DestSrc->Destination->getReg() != Reg)
  123. return Reg;
  124. Register SrcReg = DestSrc->Source->getReg();
  125. if (getRegisterSize(TRI, Reg) != getRegisterSize(TRI, SrcReg))
  126. return Reg;
  127. LLVM_DEBUG(dbgs() << "spillRegisters: perform copy propagation "
  128. << printReg(Reg, &TRI) << " -> " << printReg(SrcReg, &TRI)
  129. << "\n");
  130. // Insert spill immediately after Def
  131. RI = ++MachineBasicBlock::iterator(Def);
  132. IsKill = DestSrc->Source->isKill();
  133. // There are no uses of original register between COPY and STATEPOINT.
  134. // There can't be any after STATEPOINT, so we can eliminate Def.
  135. if (!Use) {
  136. LLVM_DEBUG(dbgs() << "spillRegisters: removing dead copy " << *Def);
  137. Def->eraseFromParent();
  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);
  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);
  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);
  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. if (!AllowGCPtrInCSR) {
  418. assert(is_contained(RegsToSpill, Reg));
  419. RegsToReload.push_back(Reg);
  420. } else {
  421. if (isCalleeSaved(Reg)) {
  422. NewIndices.push_back(NewMI->getNumOperands());
  423. MIB.addReg(Reg, RegState::Define);
  424. } else {
  425. NewIndices.push_back(NumOps);
  426. RegsToReload.push_back(Reg);
  427. }
  428. }
  429. }
  430. // Add End marker.
  431. OpsToSpill.push_back(MI.getNumOperands());
  432. unsigned CurOpIdx = 0;
  433. for (unsigned I = NumDefs; I < MI.getNumOperands(); ++I) {
  434. MachineOperand &MO = MI.getOperand(I);
  435. if (I == OpsToSpill[CurOpIdx]) {
  436. int FI = RegToSlotIdx[MO.getReg()];
  437. MIB.addImm(StackMaps::IndirectMemRefOp);
  438. MIB.addImm(getRegisterSize(TRI, MO.getReg()));
  439. assert(MO.isReg() && "Should be register");
  440. assert(MO.getReg().isPhysical() && "Should be physical register");
  441. MIB.addFrameIndex(FI);
  442. MIB.addImm(0);
  443. ++CurOpIdx;
  444. } else {
  445. MIB.add(MO);
  446. unsigned OldDef;
  447. if (AllowGCPtrInCSR && MI.isRegTiedToDefOperand(I, &OldDef)) {
  448. assert(OldDef < NumDefs);
  449. assert(NewIndices[OldDef] < NumOps);
  450. MIB->tieOperands(NewIndices[OldDef], MIB->getNumOperands() - 1);
  451. }
  452. }
  453. }
  454. assert(CurOpIdx == (OpsToSpill.size() - 1) && "Not all operands processed");
  455. // Add mem operands.
  456. NewMI->setMemRefs(MF, MI.memoperands());
  457. for (auto It : RegToSlotIdx) {
  458. Register R = It.first;
  459. int FrameIndex = It.second;
  460. auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
  461. MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad;
  462. if (is_contained(RegsToReload, R))
  463. Flags |= MachineMemOperand::MOStore;
  464. auto *MMO =
  465. MF.getMachineMemOperand(PtrInfo, Flags, getRegisterSize(TRI, R),
  466. MFI.getObjectAlign(FrameIndex));
  467. NewMI->addMemOperand(MF, MMO);
  468. }
  469. // Insert new statepoint and erase old one.
  470. MI.getParent()->insert(MI, NewMI);
  471. LLVM_DEBUG(dbgs() << "rewritten statepoint to : " << *NewMI << "\n");
  472. MI.eraseFromParent();
  473. return NewMI;
  474. }
  475. };
  476. class StatepointProcessor {
  477. private:
  478. MachineFunction &MF;
  479. const TargetRegisterInfo &TRI;
  480. FrameIndexesCache CacheFI;
  481. RegReloadCache ReloadCache;
  482. public:
  483. StatepointProcessor(MachineFunction &MF)
  484. : MF(MF), TRI(*MF.getSubtarget().getRegisterInfo()),
  485. CacheFI(MF.getFrameInfo(), TRI) {}
  486. bool process(MachineInstr &MI, bool AllowGCPtrInCSR) {
  487. StatepointOpers SO(&MI);
  488. uint64_t Flags = SO.getFlags();
  489. // Do nothing for LiveIn, it supports all registers.
  490. if (Flags & (uint64_t)StatepointFlags::DeoptLiveIn)
  491. return false;
  492. LLVM_DEBUG(dbgs() << "\nMBB " << MI.getParent()->getNumber() << " "
  493. << MI.getParent()->getName() << " : process statepoint "
  494. << MI);
  495. CallingConv::ID CC = SO.getCallingConv();
  496. const uint32_t *Mask = TRI.getCallPreservedMask(MF, CC);
  497. StatepointState SS(MI, Mask, CacheFI, AllowGCPtrInCSR);
  498. CacheFI.reset(SS.getEHPad());
  499. if (!SS.findRegistersToSpill())
  500. return false;
  501. SS.spillRegisters();
  502. auto *NewStatepoint = SS.rewriteStatepoint();
  503. SS.insertReloads(NewStatepoint, ReloadCache);
  504. return true;
  505. }
  506. };
  507. } // namespace
  508. bool FixupStatepointCallerSaved::runOnMachineFunction(MachineFunction &MF) {
  509. if (skipFunction(MF.getFunction()))
  510. return false;
  511. const Function &F = MF.getFunction();
  512. if (!F.hasGC())
  513. return false;
  514. SmallVector<MachineInstr *, 16> Statepoints;
  515. for (MachineBasicBlock &BB : MF)
  516. for (MachineInstr &I : BB)
  517. if (I.getOpcode() == TargetOpcode::STATEPOINT)
  518. Statepoints.push_back(&I);
  519. if (Statepoints.empty())
  520. return false;
  521. bool Changed = false;
  522. StatepointProcessor SPP(MF);
  523. unsigned NumStatepoints = 0;
  524. bool AllowGCPtrInCSR = PassGCPtrInCSR;
  525. for (MachineInstr *I : Statepoints) {
  526. ++NumStatepoints;
  527. if (MaxStatepointsWithRegs.getNumOccurrences() &&
  528. NumStatepoints >= MaxStatepointsWithRegs)
  529. AllowGCPtrInCSR = false;
  530. Changed |= SPP.process(*I, AllowGCPtrInCSR);
  531. }
  532. return Changed;
  533. }