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