LLVM 24.0.0git
FixupStatepointCallerSaved.cpp
Go to the documentation of this file.
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
24#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/Statistic.h"
31#include "llvm/IR/Statepoint.h"
33#include "llvm/Support/Debug.h"
34
35using namespace llvm;
36
37#define DEBUG_TYPE "fixup-statepoint-caller-saved"
38STATISTIC(NumSpilledRegisters, "Number of spilled register");
39STATISTIC(NumSpillSlotsAllocated, "Number of spill slots allocated");
40STATISTIC(NumSpillSlotsExtended, "Number of spill slots extended");
41
43 "fixup-scs-extend-slot-size", cl::Hidden, cl::init(false),
44 cl::desc("Allow spill in spill slot of greater size than register size"),
46
48 "fixup-allow-gcptr-in-csr", cl::Hidden, cl::init(false),
49 cl::desc("Allow passing GC Pointer arguments in callee saved registers"));
50
52 "fixup-scs-enable-copy-propagation", cl::Hidden, cl::init(true),
53 cl::desc("Enable simple copy propagation during register reloading"));
54
55// This is purely debugging option.
56// It may be handy for investigating statepoint spilling issues.
58 "fixup-max-csr-statepoints", cl::Hidden,
59 cl::desc("Max number of statepoints allowed to pass GC Ptrs in registers"));
60
61namespace {
62
63struct FixupStatepointCallerSavedImpl {
64 bool run(MachineFunction &MF);
65};
66
67class FixupStatepointCallerSavedLegacy : public MachineFunctionPass {
68public:
69 static char ID;
70
71 FixupStatepointCallerSavedLegacy() : MachineFunctionPass(ID) {}
72 void getAnalysisUsage(AnalysisUsage &AU) const override {
73 AU.setPreservesCFG();
74 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
76 }
77
78 StringRef getPassName() const override {
79 return "Fixup Statepoint Caller Saved";
80 }
81
82 bool runOnMachineFunction(MachineFunction &MF) override;
83};
84
85} // End anonymous namespace.
86
87char FixupStatepointCallerSavedLegacy::ID = 0;
88char &llvm::FixupStatepointCallerSavedID = FixupStatepointCallerSavedLegacy::ID;
89
90INITIALIZE_PASS_BEGIN(FixupStatepointCallerSavedLegacy, DEBUG_TYPE,
91 "Fixup Statepoint Caller Saved", false, false)
92INITIALIZE_PASS_END(FixupStatepointCallerSavedLegacy, DEBUG_TYPE,
93 "Fixup Statepoint Caller Saved", false, false)
94
95// Utility function to get size of the register.
97 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg);
98 return TRI.getSpillSize(*RC);
99}
100
101// Try to eliminate redundant copy to register which we're going to
102// spill, i.e. try to change:
103// X = COPY Y
104// SPILL X
105// to
106// SPILL Y
107// If there are no uses of X between copy and STATEPOINT, that COPY
108// may be eliminated.
109// Reg - register we're about to spill
110// RI - On entry points to statepoint.
111// On successful copy propagation set to new spill point.
112// IsKill - set to true if COPY is Kill (there are no uses of Y)
113// Returns either found source copy register or original one.
116 bool &IsKill, const TargetInstrInfo &TII,
117 const TargetRegisterInfo &TRI) {
118 // First check if statepoint itself uses Reg in non-meta operands.
119 int Idx = RI->findRegisterUseOperandIdx(Reg, &TRI, false);
120 if (Idx >= 0 && (unsigned)Idx < StatepointOpers(&*RI).getNumDeoptArgsIdx()) {
121 IsKill = false;
122 return Reg;
123 }
124
125 if (!EnableCopyProp)
126 return Reg;
127
128 MachineBasicBlock *MBB = RI->getParent();
130 MachineInstr *Def = nullptr, *Use = nullptr;
131 for (auto It = ++(RI.getReverse()); It != E; ++It) {
132 if (It->readsRegister(Reg, &TRI) && !Use)
133 Use = &*It;
134 if (It->modifiesRegister(Reg, &TRI)) {
135 Def = &*It;
136 break;
137 }
138 }
139
140 if (!Def)
141 return Reg;
142
143 auto DestSrc = TII.isCopyInstr(*Def);
144 if (!DestSrc || DestSrc->Destination->getReg() != Reg)
145 return Reg;
146
147 Register SrcReg = DestSrc->Source->getReg();
148
149 if (getRegisterSize(TRI, Reg) != getRegisterSize(TRI, SrcReg))
150 return Reg;
151
152 LLVM_DEBUG(dbgs() << "spillRegisters: perform copy propagation "
153 << printReg(Reg, &TRI) << " -> " << printReg(SrcReg, &TRI)
154 << "\n");
155
156 // Insert spill immediately after Def
157 RI = ++MachineBasicBlock::iterator(Def);
158 IsKill = DestSrc->Source->isKill();
159
160 if (!Use) {
161 // There are no uses of original register between COPY and STATEPOINT.
162 // There can't be any after STATEPOINT, so we can eliminate Def.
163 LLVM_DEBUG(dbgs() << "spillRegisters: removing dead copy " << *Def);
164 Def->eraseFromParent();
165 } else if (IsKill) {
166 // COPY will remain in place, spill will be inserted *after* it, so it is
167 // not a kill of source anymore.
168 const_cast<MachineOperand *>(DestSrc->Source)->setIsKill(false);
169 }
170
171 return SrcReg;
172}
173
174namespace {
175// Pair {Register, FrameIndex}
176using RegSlotPair = std::pair<Register, int>;
177
178// Keeps track of what reloads were inserted in MBB.
179class RegReloadCache {
180 using ReloadSet = SmallSet<RegSlotPair, 8>;
181 DenseMap<const MachineBasicBlock *, ReloadSet> Reloads;
182
183public:
184 RegReloadCache() = default;
185
186 // Record reload of Reg from FI in block MBB if not present yet.
187 // Return true if the reload is successfully recorded.
188 bool tryRecordReload(Register Reg, int FI, const MachineBasicBlock *MBB) {
189 RegSlotPair RSP(Reg, FI);
190 return Reloads[MBB].insert(RSP).second;
191 }
192};
193
194// Cache used frame indexes during statepoint re-write to re-use them in
195// processing next statepoint instruction.
196// Two strategies. One is to preserve the size of spill slot while another one
197// extends the size of spill slots to reduce the number of them, causing
198// the less total frame size. But unspill will have "implicit" any extend.
199class FrameIndexesCache {
200private:
201 struct FrameIndexesPerSize {
202 // List of used frame indexes during processing previous statepoints.
203 SmallVector<int, 8> Slots;
204 // Current index of un-used yet frame index.
205 unsigned Index = 0;
206 };
207 MachineFrameInfo &MFI;
208 const TargetRegisterInfo &TRI;
209 // Map size to list of frame indexes of this size. If the mode is
210 // FixupSCSExtendSlotSize then the key 0 is used to keep all frame indexes.
211 // If the size of required spill slot is greater than in a cache then the
212 // size will be increased.
213 DenseMap<unsigned, FrameIndexesPerSize> Cache;
214
215 // Keeps track of slots reserved for the shared landing pad processing.
216 // Initialized from GlobalIndices for the current EHPad.
217 SmallSet<int, 8> ReservedSlots;
218
219 // Landing pad can be destination of several statepoints. Every register
220 // defined by such statepoints must be spilled to the same stack slot.
221 // This map keeps that information.
222 DenseMap<const MachineBasicBlock *, SmallVector<RegSlotPair, 8>>
223 GlobalIndices;
224
225 FrameIndexesPerSize &getCacheBucket(unsigned Size) {
226 // In FixupSCSExtendSlotSize mode the bucket with 0 index is used
227 // for all sizes.
228 return Cache[FixupSCSExtendSlotSize ? 0 : Size];
229 }
230
231public:
232 FrameIndexesCache(MachineFrameInfo &MFI, const TargetRegisterInfo &TRI)
233 : MFI(MFI), TRI(TRI) {}
234 // Reset the current state of used frame indexes. After invocation of
235 // this function all frame indexes are available for allocation with
236 // the exception of slots reserved for landing pad processing (if any).
237 void reset(const MachineBasicBlock *EHPad) {
238 for (auto &It : Cache)
239 It.second.Index = 0;
240
241 ReservedSlots.clear();
242 if (EHPad)
243 if (auto It = GlobalIndices.find(EHPad); It != GlobalIndices.end())
244 ReservedSlots.insert_range(llvm::make_second_range(It->second));
245 }
246
247 // Get frame index to spill the register.
248 int getFrameIndex(Register Reg, MachineBasicBlock *EHPad) {
249 // Check if slot for Reg is already reserved at EHPad.
250 auto It = GlobalIndices.find(EHPad);
251 if (It != GlobalIndices.end()) {
252 auto &Vec = It->second;
253 auto Idx = llvm::find_if(
254 Vec, [Reg](RegSlotPair &RSP) { return Reg == RSP.first; });
255 if (Idx != Vec.end()) {
256 int FI = Idx->second;
257 LLVM_DEBUG(dbgs() << "Found global FI " << FI << " for register "
258 << printReg(Reg, &TRI) << " at "
259 << printMBBReference(*EHPad) << "\n");
260 assert(ReservedSlots.count(FI) && "using unreserved slot");
261 return FI;
262 }
263 }
264
265 unsigned Size = getRegisterSize(TRI, Reg);
266 FrameIndexesPerSize &Line = getCacheBucket(Size);
267 while (Line.Index < Line.Slots.size()) {
268 int FI = Line.Slots[Line.Index++];
269 if (ReservedSlots.count(FI))
270 continue;
271 // If all sizes are kept together we probably need to extend the
272 // spill slot size.
273 if (MFI.getObjectSize(FI) < Size) {
274 MFI.setObjectSize(FI, Size);
275 MFI.setObjectAlignment(FI, Align(Size));
276 NumSpillSlotsExtended++;
277 }
278 return FI;
279 }
280 int FI = MFI.CreateSpillStackObject(Size, Align(Size));
281 NumSpillSlotsAllocated++;
282 Line.Slots.push_back(FI);
283 ++Line.Index;
284
285 // Remember assignment {Reg, FI} for EHPad
286 if (EHPad) {
287 GlobalIndices[EHPad].push_back(std::make_pair(Reg, FI));
288 LLVM_DEBUG(dbgs() << "Reserved FI " << FI << " for spilling reg "
289 << printReg(Reg, &TRI) << " at landing pad "
290 << printMBBReference(*EHPad) << "\n");
291 }
292
293 return FI;
294 }
295
296 // Sort all registers to spill in descendent order. In the
297 // FixupSCSExtendSlotSize mode it will minimize the total frame size.
298 // In non FixupSCSExtendSlotSize mode we can skip this step.
299 void sortRegisters(SmallVectorImpl<Register> &Regs) {
301 return;
302 llvm::sort(Regs, [&](Register &A, Register &B) {
303 return getRegisterSize(TRI, A) > getRegisterSize(TRI, B);
304 });
305 }
306};
307
308// Describes the state of the current processing statepoint instruction.
309class StatepointState {
310private:
311 // statepoint instruction.
312 MachineInstr &MI;
313 MachineFunction &MF;
314 // If non-null then statepoint is invoke, and this points to the landing pad.
315 MachineBasicBlock *EHPad;
316 const TargetRegisterInfo &TRI;
317 const TargetInstrInfo &TII;
318 MachineFrameInfo &MFI;
319 // Mask with callee saved registers.
320 const uint32_t *Mask;
321 // Cache of frame indexes used on previous instruction processing.
322 FrameIndexesCache &CacheFI;
323 bool AllowGCPtrInCSR;
324 // Operands with physical registers requiring spilling.
325 SmallVector<unsigned, 8> OpsToSpill;
326 // Set of register to spill.
327 SmallVector<Register, 8> RegsToSpill;
328 // Set of registers to reload after statepoint.
329 SmallVector<Register, 8> RegsToReload;
330 // Map Register to Frame Slot index.
331 DenseMap<Register, int> RegToSlotIdx;
332
333public:
334 StatepointState(MachineInstr &MI, const uint32_t *Mask,
335 FrameIndexesCache &CacheFI, bool AllowGCPtrInCSR)
336 : MI(MI), MF(*MI.getMF()), TRI(*MF.getSubtarget().getRegisterInfo()),
337 TII(*MF.getSubtarget().getInstrInfo()), MFI(MF.getFrameInfo()),
338 Mask(Mask), CacheFI(CacheFI), AllowGCPtrInCSR(AllowGCPtrInCSR) {
339
340 // Find statepoint's landing pad, if any.
341 EHPad = nullptr;
342 MachineBasicBlock *MBB = MI.getParent();
343 // Invoke statepoint must be last one in block.
344 bool Last = std::none_of(++MI.getIterator(), MBB->end().getInstrIterator(),
345 [](MachineInstr &I) {
346 return I.getOpcode() == TargetOpcode::STATEPOINT;
347 });
348
349 if (!Last)
350 return;
351
352 auto IsEHPad = [](MachineBasicBlock *B) { return B->isEHPad(); };
353
354 assert(llvm::count_if(MBB->successors(), IsEHPad) < 2 && "multiple EHPads");
355
356 auto It = llvm::find_if(MBB->successors(), IsEHPad);
357 if (It != MBB->succ_end())
358 EHPad = *It;
359 }
360
361 MachineBasicBlock *getEHPad() const { return EHPad; }
362
363 // Return true if register is callee saved.
364 bool isCalleeSaved(Register Reg) {
365 return (Mask[Reg.id() / 32] >> (Reg.id() % 32)) & 1;
366 }
367
368 // Iterates over statepoint meta args to find caller saver registers.
369 // Also cache the size of found registers.
370 // Returns true if caller save registers found.
371 bool findRegistersToSpill() {
372 SmallSet<Register, 8> GCRegs;
373 // All GC pointer operands assigned to registers produce new value.
374 // Since they're tied to their defs, it is enough to collect def registers.
375 for (const auto &Def : MI.defs())
376 GCRegs.insert(Def.getReg());
377
378 SmallSet<Register, 8> VisitedRegs;
379 for (unsigned Idx = StatepointOpers(&MI).getVarIdx(),
380 EndIdx = MI.getNumOperands();
381 Idx < EndIdx; ++Idx) {
382 MachineOperand &MO = MI.getOperand(Idx);
383 if (!MO.isReg() || MO.isImplicit() || MO.isUndef())
384 continue;
385 Register Reg = MO.getReg();
386 assert(Reg.isPhysical() && "Only physical regs are expected");
387
388 if (isCalleeSaved(Reg) && (AllowGCPtrInCSR || !GCRegs.contains(Reg)))
389 continue;
390
391 LLVM_DEBUG(dbgs() << "Will spill " << printReg(Reg, &TRI) << " at index "
392 << Idx << "\n");
393
394 if (VisitedRegs.insert(Reg).second)
395 RegsToSpill.push_back(Reg);
396 OpsToSpill.push_back(Idx);
397 }
398 CacheFI.sortRegisters(RegsToSpill);
399 return !RegsToSpill.empty();
400 }
401
402 // Spill all caller saved registers right before statepoint instruction.
403 // Remember frame index where register is spilled.
404 void spillRegisters() {
405 for (Register Reg : RegsToSpill) {
406 int FI = CacheFI.getFrameIndex(Reg, EHPad);
407
408 NumSpilledRegisters++;
409 RegToSlotIdx[Reg] = FI;
410
411 LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, &TRI) << " to FI " << FI
412 << "\n");
413
414 // Perform trivial copy propagation
415 bool IsKill = true;
416 MachineBasicBlock::iterator InsertBefore(MI);
417 Reg = performCopyPropagation(Reg, InsertBefore, IsKill, TII, TRI);
418 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg);
419
420 LLVM_DEBUG(dbgs() << "Insert spill before " << *InsertBefore);
421 TII.storeRegToStackSlot(*MI.getParent(), InsertBefore, Reg, IsKill, FI,
422 RC, Register());
423 }
424 }
425
426 void insertReloadBefore(Register Reg, MachineBasicBlock::iterator It,
427 MachineBasicBlock *MBB) {
428 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg);
429 int FI = RegToSlotIdx[Reg];
430 if (It != MBB->end()) {
431 TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, Register());
432 return;
433 }
434
435 // To insert reload at the end of MBB, insert it before last instruction
436 // and then swap them.
437 assert(!MBB->empty() && "Empty block");
438 --It;
439 TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, Register());
440 MachineInstr *Reload = It->getPrevNode();
441 int Dummy = 0;
442 (void)Dummy;
443 assert(TII.isLoadFromStackSlot(*Reload, Dummy) == Reg);
444 assert(Dummy == FI);
445 MBB->remove(Reload);
446 MBB->insertAfter(It, Reload);
447 }
448
449 // Insert reloads of (relocated) registers spilled in statepoint.
450 void insertReloads(MachineInstr *NewStatepoint, RegReloadCache &RC) {
451 MachineBasicBlock *MBB = NewStatepoint->getParent();
452 auto InsertPoint = std::next(NewStatepoint->getIterator());
453
454 for (auto Reg : RegsToReload) {
455 insertReloadBefore(Reg, InsertPoint, MBB);
456 LLVM_DEBUG(dbgs() << "Reloading " << printReg(Reg, &TRI) << " from FI "
457 << RegToSlotIdx[Reg] << " after statepoint\n");
458
459 if (EHPad && RC.tryRecordReload(Reg, RegToSlotIdx[Reg], EHPad)) {
460 auto EHPadInsertPoint =
461 EHPad->SkipPHIsLabelsAndDebug(EHPad->begin(), Reg);
462 insertReloadBefore(Reg, EHPadInsertPoint, EHPad);
463 LLVM_DEBUG(dbgs() << "...also reload at EHPad "
464 << printMBBReference(*EHPad) << "\n");
465 }
466 }
467 }
468
469 // Re-write statepoint machine instruction to replace caller saved operands
470 // with indirect memory location (frame index).
471 MachineInstr *rewriteStatepoint() {
472 MachineInstr *NewMI =
473 MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true);
474 MachineInstrBuilder MIB(MF, NewMI);
475
476 unsigned NumOps = MI.getNumOperands();
477
478 // New indices for the remaining defs.
479 SmallVector<unsigned, 8> NewIndices;
480 unsigned NumDefs = MI.getNumDefs();
481 for (unsigned I = 0; I < NumDefs; ++I) {
482 MachineOperand &DefMO = MI.getOperand(I);
483 assert(DefMO.isReg() && DefMO.isDef() && "Expected Reg Def operand");
484 Register Reg = DefMO.getReg();
485 assert(DefMO.isTied() && "Def is expected to be tied");
486 // We skipped undef uses and did not spill them, so we should not
487 // proceed with defs here.
488 if (MI.getOperand(MI.findTiedOperandIdx(I)).isUndef()) {
489 if (AllowGCPtrInCSR) {
490 NewIndices.push_back(NewMI->getNumOperands());
491 MIB.addReg(Reg, RegState::Define);
492 }
493 continue;
494 }
495 if (!AllowGCPtrInCSR) {
496 assert(is_contained(RegsToSpill, Reg));
497 RegsToReload.push_back(Reg);
498 } else {
499 if (isCalleeSaved(Reg)) {
500 NewIndices.push_back(NewMI->getNumOperands());
501 MIB.addReg(Reg, RegState::Define);
502 } else {
503 NewIndices.push_back(NumOps);
504 RegsToReload.push_back(Reg);
505 }
506 }
507 }
508
509 // Add End marker.
510 OpsToSpill.push_back(MI.getNumOperands());
511 unsigned CurOpIdx = 0;
512
513 for (unsigned I = NumDefs; I < MI.getNumOperands(); ++I) {
514 MachineOperand &MO = MI.getOperand(I);
515 if (I == OpsToSpill[CurOpIdx]) {
516 int FI = RegToSlotIdx[MO.getReg()];
517 MIB.addImm(StackMaps::IndirectMemRefOp);
518 MIB.addImm(getRegisterSize(TRI, MO.getReg()));
519 assert(MO.isReg() && "Should be register");
520 assert(MO.getReg().isPhysical() && "Should be physical register");
521 MIB.addFrameIndex(FI);
522 MIB.addImm(0);
523 ++CurOpIdx;
524 } else {
525 MIB.add(MO);
526 unsigned OldDef;
527 if (AllowGCPtrInCSR && MI.isRegTiedToDefOperand(I, &OldDef)) {
528 assert(OldDef < NumDefs);
529 assert(NewIndices[OldDef] < NumOps);
530 MIB->tieOperands(NewIndices[OldDef], MIB->getNumOperands() - 1);
531 }
532 }
533 }
534 assert(CurOpIdx == (OpsToSpill.size() - 1) && "Not all operands processed");
535 // Add mem operands.
536 NewMI->setMemRefs(MF, MI.memoperands());
537 for (auto It : RegToSlotIdx) {
538 Register R = It.first;
539 int FrameIndex = It.second;
540 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
542 if (is_contained(RegsToReload, R))
544 auto *MMO =
545 MF.getMachineMemOperand(PtrInfo, Flags, getRegisterSize(TRI, R),
546 MFI.getObjectAlign(FrameIndex));
547 NewMI->addMemOperand(MF, MMO);
548 }
549
550 // Insert new statepoint and erase old one.
551 MI.getParent()->insert(MI, NewMI);
552
553 LLVM_DEBUG(dbgs() << "rewritten statepoint to : " << *NewMI << "\n");
554 MI.eraseFromParent();
555 return NewMI;
556 }
557};
558
559class StatepointProcessor {
560private:
561 MachineFunction &MF;
562 const TargetRegisterInfo &TRI;
563 FrameIndexesCache CacheFI;
564 RegReloadCache ReloadCache;
565
566public:
567 StatepointProcessor(MachineFunction &MF)
568 : MF(MF), TRI(*MF.getSubtarget().getRegisterInfo()),
569 CacheFI(MF.getFrameInfo(), TRI) {}
570
571 bool process(MachineInstr &MI, bool AllowGCPtrInCSR) {
572 StatepointOpers SO(&MI);
573 uint64_t Flags = SO.getFlags();
574 // Do nothing for LiveIn, it supports all registers.
575 if (Flags & (uint64_t)StatepointFlags::DeoptLiveIn)
576 return false;
577 LLVM_DEBUG(dbgs() << "\nMBB " << MI.getParent()->getNumber() << " "
578 << MI.getParent()->getName() << " : process statepoint "
579 << MI);
580 CallingConv::ID CC = SO.getCallingConv();
581 const uint32_t *Mask = TRI.getCallPreservedMask(MF, CC);
582 StatepointState SS(MI, Mask, CacheFI, AllowGCPtrInCSR);
583 CacheFI.reset(SS.getEHPad());
584
585 if (!SS.findRegistersToSpill())
586 return false;
587
588 SS.spillRegisters();
589 auto *NewStatepoint = SS.rewriteStatepoint();
590 SS.insertReloads(NewStatepoint, ReloadCache);
591 return true;
592 }
593};
594} // namespace
595
596bool FixupStatepointCallerSavedImpl::run(MachineFunction &MF) {
597 const Function &F = MF.getFunction();
598 if (!F.hasGC())
599 return false;
600
602 for (MachineBasicBlock &BB : MF)
603 for (MachineInstr &I : BB)
604 if (I.getOpcode() == TargetOpcode::STATEPOINT)
605 Statepoints.push_back(&I);
606
607 if (Statepoints.empty())
608 return false;
609
610 bool Changed = false;
611 StatepointProcessor SPP(MF);
612 unsigned NumStatepoints = 0;
613 bool AllowGCPtrInCSR = PassGCPtrInCSR;
614 for (MachineInstr *I : Statepoints) {
615 ++NumStatepoints;
616 if (MaxStatepointsWithRegs.getNumOccurrences() &&
617 NumStatepoints >= MaxStatepointsWithRegs)
618 AllowGCPtrInCSR = false;
619 Changed |= SPP.process(*I, AllowGCPtrInCSR);
620 }
621 return Changed;
622}
623
624bool FixupStatepointCallerSavedLegacy::runOnMachineFunction(
625 MachineFunction &MF) {
626 if (skipFunction(MF.getFunction()))
627 return false;
628
629 return FixupStatepointCallerSavedImpl().run(MF);
630}
631
632PreservedAnalyses
635
636 if (!FixupStatepointCallerSavedImpl().run(MF))
637 return PreservedAnalyses::all();
638
640 PA.preserveSet<CFGAnalyses>();
641 return PA;
642}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static Register performCopyPropagation(Register Reg, MachineBasicBlock::iterator &RI, bool &IsKill, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI)
static cl::opt< bool > PassGCPtrInCSR("fixup-allow-gcptr-in-csr", cl::Hidden, cl::init(false), cl::desc("Allow passing GC Pointer arguments in callee saved registers"))
static cl::opt< unsigned > MaxStatepointsWithRegs("fixup-max-csr-statepoints", cl::Hidden, cl::desc("Max number of statepoints allowed to pass GC Ptrs in registers"))
Fixup Statepoint Caller static false unsigned getRegisterSize(const TargetRegisterInfo &TRI, Register Reg)
static cl::opt< bool > FixupSCSExtendSlotSize("fixup-scs-extend-slot-size", cl::Hidden, cl::init(false), cl::desc("Allow spill in spill slot of greater size than register size"), cl::Hidden)
static cl::opt< bool > EnableCopyProp("fixup-scs-enable-copy-propagation", cl::Hidden, cl::init(true), cl::desc("Enable simple copy propagation during register reloading"))
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the SmallSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
MachineInstr * remove(MachineInstr *I)
Remove the unbundled instruction from the instruction list without deleting it.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< succ_iterator > successors()
iterator insertAfter(iterator I, MachineInstr *MI)
Insert MI into the instruction list after I.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Function & getFunction()
Return the LLVM function that this machine code represents.
reverse_iterator getReverse() const
Get a reverse iterator to the same node.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI void setMemRefs(MachineFunction &MF, ArrayRef< MachineMemOperand * > MemRefs)
Assign this MachineInstr's memory reference descriptor list.
LLVM_ABI void addMemOperand(MachineFunction &MF, MachineMemOperand *MO)
Add a MachineMemOperand to the machine instruction.
Flags
Flags values. These may be or'd together.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr unsigned id() const
Definition Register.h:100
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition SmallSet.h:229
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
void push_back(const T &Elt)
MI-level Statepoint operands.
Definition StackMaps.h:159
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
self_iterator getIterator()
Definition ilist_node.h:123
Changed
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
initializer< Ty > init(const Ty &Val)
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI char & FixupStatepointCallerSavedID
The pass fixups statepoint machine instruction to replace usage of caller saved registers with stack ...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
Definition STLExtras.h:1409
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2019
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.