LLVM 24.0.0git
PrologEpilogInserter.cpp
Go to the documentation of this file.
1//===- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function ---===//
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// This pass is responsible for finalizing the functions frame layout, saving
10// callee saved registers, and for emitting prolog & epilog code for the
11// function.
12//
13// This pass must be run after register allocation. After this pass is
14// executed, it is illegal to construct MO_FrameIndex operands.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/Statistic.h"
37#include "llvm/CodeGen/PEI.h"
45#include "llvm/IR/Attributes.h"
46#include "llvm/IR/CallingConv.h"
49#include "llvm/IR/Function.h"
50#include "llvm/IR/LLVMContext.h"
52#include "llvm/Pass.h"
54#include "llvm/Support/Debug.h"
60#include <algorithm>
61#include <cassert>
62#include <cstdint>
63#include <limits>
64#include <utility>
65#include <vector>
66
67using namespace llvm;
68
69#define DEBUG_TYPE "prolog-epilog"
70
72
73STATISTIC(NumLeafFuncWithSpills, "Number of leaf functions with CSRs");
74STATISTIC(NumFuncSeen, "Number of functions seen in PEI");
75
76
77namespace {
78
79class PEIImpl {
80 RegScavenger *RS = nullptr;
81
82 // Save and Restore blocks of the current function. Typically there is a
83 // single save block, unless Windows EH funclets are involved.
84 MBBVector SaveBlocks;
85 MBBVector RestoreBlocks;
86
87 // Flag to control whether to use the register scavenger to resolve
88 // frame index materialization registers. Set according to
89 // TRI->requiresFrameIndexScavenging() for the current function.
90 bool FrameIndexVirtualScavenging = false;
91
92 // Flag to control whether the scavenger should be passed even though
93 // FrameIndexVirtualScavenging is used.
94 bool FrameIndexEliminationScavenging = false;
95
96 // Emit remarks.
98
99 void calculateCallFrameInfo(MachineFunction &MF);
100 void calculateSaveRestoreBlocks(MachineFunction &MF);
101 void spillCalleeSavedRegs(MachineFunction &MF);
102
103 void calculateFrameObjectOffsets(MachineFunction &MF);
104 void replaceFrameIndices(MachineFunction &MF);
105 void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
106 int &SPAdj);
107 // Frame indices in debug values are encoded in a target independent
108 // way with simply the frame index and offset rather than any
109 // target-specific addressing mode.
111 unsigned OpIdx, int SPAdj = 0);
112 // Does same as replaceFrameIndices but using the backward MIR walk and
113 // backward register scavenger walk.
114 void replaceFrameIndicesBackward(MachineFunction &MF);
115 void replaceFrameIndicesBackward(MachineBasicBlock *BB, MachineFunction &MF,
116 int &SPAdj);
117
118 void insertPrologEpilogCode(MachineFunction &MF);
119 void insertZeroCallUsedRegs(MachineFunction &MF);
120
121public:
122 PEIImpl(MachineOptimizationRemarkEmitter *ORE) : ORE(ORE) {}
123 bool run(MachineFunction &MF);
124};
125
126class PEILegacy : public MachineFunctionPass {
127public:
128 static char ID;
129
130 PEILegacy() : MachineFunctionPass(ID) {}
131
132 void getAnalysisUsage(AnalysisUsage &AU) const override;
133
134 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
135 /// frame indexes with appropriate references.
136 bool runOnMachineFunction(MachineFunction &MF) override;
137};
138
139} // end anonymous namespace
140
141char PEILegacy::ID = 0;
142
144
145INITIALIZE_PASS_BEGIN(PEILegacy, DEBUG_TYPE, "Prologue/Epilogue Insertion",
146 false, false)
151 "Prologue/Epilogue Insertion & Frame Finalization", false,
152 false)
153
155 return new PEILegacy();
156}
157
158STATISTIC(NumBytesStackSpace,
159 "Number of bytes used for stack in all functions");
160
161void PEILegacy::getAnalysisUsage(AnalysisUsage &AU) const {
162 AU.setPreservesCFG();
165}
166
167/// StackObjSet - A set of stack object indexes
169
172
173/// Stash DBG_VALUEs that describe parameters and which are placed at the start
174/// of the block. Later on, after the prologue code has been emitted, the
175/// stashed DBG_VALUEs will be reinserted at the start of the block.
177 SavedDbgValuesMap &EntryDbgValues) {
179
180 for (auto &MI : MBB) {
181 if (!MI.isDebugInstr())
182 break;
183 if (!MI.isDebugValue() || !MI.getDebugVariable()->isParameter())
184 continue;
185 if (any_of(MI.debug_operands(),
186 [](const MachineOperand &MO) { return MO.isFI(); })) {
187 // We can only emit valid locations for frame indices after the frame
188 // setup, so do not stash away them.
189 FrameIndexValues.push_back(&MI);
190 continue;
191 }
192 const DILocalVariable *Var = MI.getDebugVariable();
193 const DIExpression *Expr = MI.getDebugExpression();
194 auto Overlaps = [Var, Expr](const MachineInstr *DV) {
195 return Var == DV->getDebugVariable() &&
196 Expr->fragmentsOverlap(DV->getDebugExpression());
197 };
198 // See if the debug value overlaps with any preceding debug value that will
199 // not be stashed. If that is the case, then we can't stash this value, as
200 // we would then reorder the values at reinsertion.
201 if (llvm::none_of(FrameIndexValues, Overlaps))
202 EntryDbgValues[&MBB].push_back(&MI);
203 }
204
205 // Remove stashed debug values from the block.
206 if (auto It = EntryDbgValues.find(&MBB); It != EntryDbgValues.end())
207 for (auto *MI : It->second)
208 MI->removeFromParent();
209}
210
211bool PEIImpl::run(MachineFunction &MF) {
212 NumFuncSeen++;
213 const Function &F = MF.getFunction();
214 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
215 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
216
217 RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr;
218 FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
219
220 // Spill frame pointer and/or base pointer registers if they are clobbered.
221 // It is placed before call frame instruction elimination so it will not mess
222 // with stack arguments.
223 TFI->spillFPBP(MF);
224
225 // Calculate the MaxCallFrameSize value for the function's frame
226 // information. Also eliminates call frame pseudo instructions.
227 calculateCallFrameInfo(MF);
228
229 // Determine placement of CSR spill/restore code and prolog/epilog code:
230 // place all spills in the entry block, all restores in return blocks.
231 calculateSaveRestoreBlocks(MF);
232
233 // Stash away DBG_VALUEs that should not be moved by insertion of prolog code.
234 SavedDbgValuesMap EntryDbgValues;
235 for (MachineBasicBlock *SaveBlock : SaveBlocks)
236 stashEntryDbgValues(*SaveBlock, EntryDbgValues);
237
238 // Handle CSR spilling and restoring, for targets that need it.
240 spillCalleeSavedRegs(MF);
241
242 // Allow the target machine to make final modifications to the function
243 // before the frame layout is finalized.
245
246 // Calculate actual frame offsets for all abstract stack objects...
247 calculateFrameObjectOffsets(MF);
248
249 // Add prolog and epilog code to the function. This function is required
250 // to align the stack frame as necessary for any stack variables or
251 // called functions. Because of this, calculateCalleeSavedRegisters()
252 // must be called before this function in order to set the AdjustsStack
253 // and MaxCallFrameSize variables.
254 if (!F.hasFnAttribute(Attribute::Naked))
255 insertPrologEpilogCode(MF);
256
257 // Reinsert stashed debug values at the start of the entry blocks.
258 for (auto &I : EntryDbgValues)
259 I.first->insert(I.first->begin(), I.second.begin(), I.second.end());
260
261 // Allow the target machine to make final modifications to the function
262 // before the frame layout is finalized.
264
265 // Replace all MO_FrameIndex operands with physical register references
266 // and actual offsets.
267 if (TFI->needsFrameIndexResolution(MF)) {
268 // Allow the target to determine this after knowing the frame size.
269 FrameIndexEliminationScavenging =
270 (RS && !FrameIndexVirtualScavenging) ||
271 TRI->requiresFrameIndexReplacementScavenging(MF);
272
273 if (TRI->eliminateFrameIndicesBackwards())
274 replaceFrameIndicesBackward(MF);
275 else
276 replaceFrameIndices(MF);
277 }
278
279 // If register scavenging is needed, as we've enabled doing it as a
280 // post-pass, scavenge the virtual registers that frame index elimination
281 // inserted.
282 if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging)
284
285 insertZeroCallUsedRegs(MF);
286
287 // Warn on stack size when we exceeds the given limit.
288 MachineFrameInfo &MFI = MF.getFrameInfo();
289 uint64_t StackSize = MFI.getStackSize();
290
291 uint64_t Threshold = TFI->getStackThreshold();
292 if (MF.getFunction().hasFnAttribute("warn-stack-size")) {
293 bool Failed = MF.getFunction()
294 .getFnAttribute("warn-stack-size")
296 .getAsInteger(10, Threshold);
297 // Verifier should have caught this.
298 assert(!Failed && "Invalid warn-stack-size fn attr value");
299 (void)Failed;
300 }
301 uint64_t UnsafeStackSize = MFI.getUnsafeStackSize();
302 if (MF.getFunction().hasFnAttribute(Attribute::SafeStack))
303 StackSize += UnsafeStackSize;
304
305 if (StackSize > Threshold) {
306 DiagnosticInfoStackSize DiagStackSize(F, StackSize, Threshold, DS_Warning);
307 F.getContext().diagnose(DiagStackSize);
308 int64_t SpillSize = 0;
309 for (int Idx = MFI.getObjectIndexBegin(), End = MFI.getObjectIndexEnd();
310 Idx != End; ++Idx) {
311 if (MFI.isSpillSlotObjectIndex(Idx))
312 SpillSize += MFI.getObjectSize(Idx);
313 }
314
315 [[maybe_unused]] float SpillPct =
316 static_cast<float>(SpillSize) / static_cast<float>(StackSize);
318 dbgs() << formatv("{0}/{1} ({3:P}) spills, {2}/{1} ({4:P}) variables",
319 SpillSize, StackSize, StackSize - SpillSize, SpillPct,
320 1.0f - SpillPct));
321 if (UnsafeStackSize != 0) {
322 LLVM_DEBUG(dbgs() << formatv(", {0}/{2} ({1:P}) unsafe stack",
323 UnsafeStackSize,
324 static_cast<float>(UnsafeStackSize) /
325 static_cast<float>(StackSize),
326 StackSize));
327 }
328 LLVM_DEBUG(dbgs() << "\n");
329 }
330
331 ORE->emit([&]() {
332 return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
334 &MF.front())
335 << ore::NV("NumStackBytes", StackSize)
336 << " stack bytes in function '"
337 << ore::NV("Function", MF.getFunction().getName()) << "'";
338 });
339
340 // Emit any remarks implemented for the target, based on final frame layout.
341 TFI->emitRemarks(MF, ORE);
342
343 delete RS;
344 SaveBlocks.clear();
345 RestoreBlocks.clear();
346 MFI.clearSavePoints();
347 MFI.clearRestorePoints();
348 return true;
349}
350
351/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
352/// frame indexes with appropriate references.
353bool PEILegacy::runOnMachineFunction(MachineFunction &MF) {
354 MachineOptimizationRemarkEmitter *ORE =
355 &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
356 return PEIImpl(ORE).run(MF);
357}
358
359PreservedAnalyses
364 if (!PEIImpl(&ORE).run(MF))
365 return PreservedAnalyses::all();
366
367 // Segmented-stack and HiPE prologues insert new basic blocks, so the CFG is
368 // not preserved for such functions.
369 // TODO: We should propagate this info directly once we are done with NewPM
370 // transition.
371 if (MF.shouldSplitStack() ||
374
376}
377
378/// Calculate the MaxCallFrameSize variable for the function's frame
379/// information and eliminate call frame pseudo instructions.
380void PEIImpl::calculateCallFrameInfo(MachineFunction &MF) {
383 MachineFrameInfo &MFI = MF.getFrameInfo();
384
385 // Get the function call frame set-up and tear-down instruction opcode
386 unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
387 unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
388
389 // Early exit for targets which have no call frame setup/destroy pseudo
390 // instructions.
391 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
392 return;
393
394 // (Re-)Compute the MaxCallFrameSize.
395 [[maybe_unused]] uint64_t MaxCFSIn =
397 std::vector<MachineBasicBlock::iterator> FrameSDOps;
398 MFI.computeMaxCallFrameSize(MF, &FrameSDOps);
399 assert(MFI.getMaxCallFrameSize() <= MaxCFSIn &&
400 "Recomputing MaxCFS gave a larger value.");
401 assert((FrameSDOps.empty() || MF.getFrameInfo().adjustsStack()) &&
402 "AdjustsStack not set in presence of a frame pseudo instruction.");
403
404 if (TFI->canSimplifyCallFramePseudos(MF)) {
405 // If call frames are not being included as part of the stack frame, and
406 // the target doesn't indicate otherwise, remove the call frame pseudos
407 // here. The sub/add sp instruction pairs are still inserted, but we don't
408 // need to track the SP adjustment for frame index elimination.
409 for (MachineBasicBlock::iterator I : FrameSDOps)
410 TFI->eliminateCallFramePseudoInstr(MF, *I->getParent(), I);
411
412 // We can't track the call frame size after call frame pseudos have been
413 // eliminated. Set it to zero everywhere to keep MachineVerifier happy.
414 for (MachineBasicBlock &MBB : MF)
415 MBB.setCallFrameSize(0);
416 }
417}
418
419/// Compute the sets of entry and return blocks for saving and restoring
420/// callee-saved registers, and placing prolog and epilog code.
421void PEIImpl::calculateSaveRestoreBlocks(MachineFunction &MF) {
422 const MachineFrameInfo &MFI = MF.getFrameInfo();
423 // Even when we do not change any CSR, we still want to insert the
424 // prologue and epilogue of the function.
425 // So set the save points for those.
426
427 // Use the points found by shrink-wrapping, if any.
428 if (!MFI.getSavePoints().empty()) {
429 assert(MFI.getSavePoints().size() == 1 &&
430 "Multiple save points are not yet supported!");
431 const auto &SavePoint = *MFI.getSavePoints().begin();
432 SaveBlocks.push_back(SavePoint.first);
433 assert(MFI.getRestorePoints().size() == 1 &&
434 "Multiple restore points are not yet supported!");
435 const auto &RestorePoint = *MFI.getRestorePoints().begin();
436 MachineBasicBlock *RestoreBlock = RestorePoint.first;
437 // If RestoreBlock does not have any successor and is not a return block
438 // then the end point is unreachable and we do not need to insert any
439 // epilogue.
440 if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
441 RestoreBlocks.push_back(RestoreBlock);
442 return;
443 }
444
445 // Save refs to entry and return blocks.
446 SaveBlocks.push_back(&MF.front());
447 for (MachineBasicBlock &MBB : MF) {
448 if (MBB.isEHFuncletEntry())
449 SaveBlocks.push_back(&MBB);
450 if (MBB.isReturnBlock())
451 RestoreBlocks.push_back(&MBB);
452 }
453}
454
456 const BitVector &SavedRegs) {
457 if (SavedRegs.empty())
458 return;
459
460 const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
461 const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs();
462 BitVector CSMask(SavedRegs.size());
463
464 for (unsigned i = 0; CSRegs[i]; ++i)
465 CSMask.set(CSRegs[i]);
466
467 std::vector<CalleeSavedInfo> CSI;
468 for (unsigned i = 0; CSRegs[i]; ++i) {
469 unsigned Reg = CSRegs[i];
470 if (SavedRegs.test(Reg)) {
471 bool SavedSuper = false;
472 for (const MCPhysReg &SuperReg : RegInfo->superregs(Reg)) {
473 // Some backends set all aliases for some registers as saved, such as
474 // Mips's $fp, so they appear in SavedRegs but not CSRegs.
475 if (SavedRegs.test(SuperReg) && CSMask.test(SuperReg)) {
476 SavedSuper = true;
477 break;
478 }
479 }
480
481 if (!SavedSuper)
482 CSI.push_back(CalleeSavedInfo(Reg));
483 }
484 }
485
486 const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering();
487 MachineFrameInfo &MFI = F.getFrameInfo();
488 if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
489 // If target doesn't implement this, use generic code.
490
491 if (CSI.empty())
492 return; // Early exit if no callee saved registers are modified!
493
494 unsigned NumFixedSpillSlots;
495 const TargetFrameLowering::SpillSlot *FixedSpillSlots =
496 TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
497
498 // Now that we know which registers need to be saved and restored, allocate
499 // stack slots for them.
500 for (auto &CS : CSI) {
501 // If the target has spilled this register to another register or already
502 // handled it , we don't need to allocate a stack slot.
503 if (CS.isSpilledToReg())
504 continue;
505
506 MCRegister Reg = CS.getReg();
507 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
508
509 int FrameIdx;
510 if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
511 CS.setFrameIdx(FrameIdx);
512 continue;
513 }
514
515 // Check to see if this physreg must be spilled to a particular stack slot
516 // on this target.
517 const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
518 while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
519 FixedSlot->Reg != Reg)
520 ++FixedSlot;
521
522 unsigned Size = RegInfo->getSpillSize(*RC);
523 if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
524 // Nope, just spill it anywhere convenient.
525 Align Alignment = RegInfo->getSpillAlign(*RC);
526 // We may not be able to satisfy the desired alignment specification of
527 // the TargetRegisterClass if the stack alignment is smaller. Use the
528 // min.
529 Alignment = std::min(Alignment, TFI->getStackAlign());
530 FrameIdx = MFI.CreateStackObject(Size, Alignment, true, nullptr,
531 RegInfo->getSpillStackID(*RC));
532 MFI.setIsCalleeSavedObjectIndex(FrameIdx, true);
533 } else {
534 // Spill it to the stack where we must.
535 FrameIdx = MFI.CreateFixedSpillStackObject(Size, FixedSlot->Offset);
536 }
537
538 CS.setFrameIdx(FrameIdx);
539 }
540 }
541
542 MFI.setCalleeSavedInfo(CSI);
543}
544
545/// Helper function to update the liveness information for the callee-saved
546/// registers.
548 MachineFrameInfo &MFI = MF.getFrameInfo();
549 // Visited will contain all the basic blocks that are in the region
550 // where the callee saved registers are alive:
551 // - Anything that is not Save or Restore -> LiveThrough.
552 // - Save -> LiveIn.
553 // - Restore -> LiveOut.
554 // The live-out is not attached to the block, so no need to keep
555 // Restore in this set.
558 MachineBasicBlock *Entry = &MF.front();
559
560 assert(MFI.getSavePoints().size() < 2 &&
561 "Multiple save points not yet supported!");
562 MachineBasicBlock *Save = MFI.getSavePoints().empty()
563 ? nullptr
564 : (*MFI.getSavePoints().begin()).first;
565
566 if (!Save)
567 Save = Entry;
568
569 if (Entry != Save) {
570 WorkList.push_back(Entry);
571 Visited.insert(Entry);
572 }
573 Visited.insert(Save);
574
575 assert(MFI.getRestorePoints().size() < 2 &&
576 "Multiple restore points not yet supported!");
577 MachineBasicBlock *Restore = MFI.getRestorePoints().empty()
578 ? nullptr
579 : (*MFI.getRestorePoints().begin()).first;
580 if (Restore)
581 // By construction Restore cannot be visited, otherwise it
582 // means there exists a path to Restore that does not go
583 // through Save.
584 WorkList.push_back(Restore);
585
586 while (!WorkList.empty()) {
587 const MachineBasicBlock *CurBB = WorkList.pop_back_val();
588 // By construction, the region that is after the save point is
589 // dominated by the Save and post-dominated by the Restore.
590 if (CurBB == Save && Save != Restore)
591 continue;
592 // Enqueue all the successors not already visited.
593 // Those are by construction either before Save or after Restore.
594 for (MachineBasicBlock *SuccBB : CurBB->successors())
595 if (Visited.insert(SuccBB).second)
596 WorkList.push_back(SuccBB);
597 }
598
599 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
600
602 for (const CalleeSavedInfo &I : CSI) {
603 for (MachineBasicBlock *MBB : Visited) {
604 MCRegister Reg = I.getReg();
605 // Add the callee-saved register as live-in.
606 // It's killed at the spill.
607 if (!MRI.isReserved(Reg) && !MBB->isLiveIn(Reg))
608 MBB->addLiveIn(Reg);
609 }
610 // If callee-saved register is spilled to another register rather than
611 // spilling to stack, the destination register has to be marked as live for
612 // each MBB between the prologue and epilogue so that it is not clobbered
613 // before it is reloaded in the epilogue. The Visited set contains all
614 // blocks outside of the region delimited by prologue/epilogue.
615 if (I.isSpilledToReg()) {
616 for (MachineBasicBlock &MBB : MF) {
617 if (Visited.count(&MBB))
618 continue;
619 MCRegister DstReg = I.getDstReg();
620 if (!MBB.isLiveIn(DstReg))
621 MBB.addLiveIn(DstReg);
622 }
623 }
624 }
625}
626
627/// Insert spill code for the callee-saved registers used in the function.
628static void insertCSRSaves(MachineBasicBlock &SaveBlock,
630 MachineFunction &MF = *SaveBlock.getParent();
634
635 MachineBasicBlock::iterator I = SaveBlock.begin();
636 if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
637 for (const CalleeSavedInfo &CS : CSI) {
638 TFI->spillCalleeSavedRegister(SaveBlock, I, CS, TII, TRI);
639 }
640 }
641}
642
643/// Insert restore code for the callee-saved registers used in the function.
644static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
645 std::vector<CalleeSavedInfo> &CSI) {
646 MachineFunction &MF = *RestoreBlock.getParent();
650
651 // Restore all registers immediately before the return and any
652 // terminators that precede it.
654
655 if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
656 for (const CalleeSavedInfo &CI : reverse(CSI)) {
657 TFI->restoreCalleeSavedRegister(RestoreBlock, I, CI, TII, TRI);
658 }
659 }
660}
661
662void PEIImpl::spillCalleeSavedRegs(MachineFunction &MF) {
663 // We can't list this requirement in getRequiredProperties because some
664 // targets (WebAssembly) use virtual registers past this point, and the pass
665 // pipeline is set up without giving the passes a chance to look at the
666 // TargetMachine.
667 // FIXME: Find a way to express this in getRequiredProperties.
668 assert(MF.getProperties().hasNoVRegs());
669
670 const Function &F = MF.getFunction();
671 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
672 MachineFrameInfo &MFI = MF.getFrameInfo();
673
674 // Determine which of the registers in the callee save list should be saved.
675 BitVector SavedRegs;
676 TFI->determineCalleeSaves(MF, SavedRegs, RS);
677
678 // Assign stack slots for any callee-saved registers that must be spilled.
679 assignCalleeSavedSpillSlots(MF, SavedRegs);
680
681 // Add the code to save and restore the callee saved registers.
682 if (!F.hasFnAttribute(Attribute::Naked)) {
683 MFI.setCalleeSavedInfoValid(true);
684
685 std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
686
687 // Fill SavePoints and RestorePoints with CalleeSavedRegisters
688 if (!MFI.getSavePoints().empty()) {
689 SaveRestorePoints SaveRestorePts;
690 for (const auto &SavePoint : MFI.getSavePoints())
691 SaveRestorePts.insert({SavePoint.first, CSI});
692 MFI.setSavePoints(std::move(SaveRestorePts));
693
694 SaveRestorePts.clear();
695 for (const auto &RestorePoint : MFI.getRestorePoints())
696 SaveRestorePts.insert({RestorePoint.first, CSI});
697 MFI.setRestorePoints(std::move(SaveRestorePts));
698 }
699
700 if (!CSI.empty()) {
701 if (!MFI.hasCalls())
702 NumLeafFuncWithSpills++;
703
704 for (MachineBasicBlock *SaveBlock : SaveBlocks)
705 insertCSRSaves(*SaveBlock, CSI);
706
707 // Update the live-in information of all the blocks up to the save point.
708 updateLiveness(MF);
709
710 for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
711 insertCSRRestores(*RestoreBlock, CSI);
712 }
713 }
714}
715
716/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
717static inline void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
718 bool StackGrowsDown, int64_t &Offset,
719 Align &MaxAlign) {
720 // If the stack grows down, add the object size to find the lowest address.
721 if (StackGrowsDown)
722 Offset += MFI.getObjectSize(FrameIdx);
723
724 Align Alignment = MFI.getObjectAlign(FrameIdx);
725
726 // If the alignment of this object is greater than that of the stack, then
727 // increase the stack alignment to match.
728 MaxAlign = std::max(MaxAlign, Alignment);
729
730 // Adjust to alignment boundary.
731 Offset = alignTo(Offset, Alignment);
732
733 if (StackGrowsDown) {
734 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
735 << "]\n");
736 MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
737 } else {
738 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
739 << "]\n");
740 MFI.setObjectOffset(FrameIdx, Offset);
741 Offset += MFI.getObjectSize(FrameIdx);
742 }
743}
744
745/// Compute which bytes of fixed and callee-save stack area are unused and keep
746/// track of them in StackBytesFree.
748 bool StackGrowsDown,
749 int64_t FixedCSEnd,
750 BitVector &StackBytesFree) {
751 // Avoid undefined int64_t -> int conversion below in extreme case.
752 if (FixedCSEnd > std::numeric_limits<int>::max())
753 return;
754
755 StackBytesFree.resize(FixedCSEnd, true);
756
757 SmallVector<int, 16> AllocatedFrameSlots;
758 // Add fixed objects.
759 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i)
760 // StackSlot scavenging is only implemented for the default stack.
762 AllocatedFrameSlots.push_back(i);
763 // Add callee-save objects if there are any.
764 for (int i = MFI.getObjectIndexBegin(); i < MFI.getObjectIndexEnd(); i++)
765 if (MFI.isCalleeSavedObjectIndex(i) &&
767 AllocatedFrameSlots.push_back(i);
768
769 for (int i : AllocatedFrameSlots) {
770 // These are converted from int64_t, but they should always fit in int
771 // because of the FixedCSEnd check above.
772 int ObjOffset = MFI.getObjectOffset(i);
773 int ObjSize = MFI.getObjectSize(i);
774 int ObjStart, ObjEnd;
775 if (StackGrowsDown) {
776 // ObjOffset is negative when StackGrowsDown is true.
777 ObjStart = -ObjOffset - ObjSize;
778 ObjEnd = -ObjOffset;
779 } else {
780 ObjStart = ObjOffset;
781 ObjEnd = ObjOffset + ObjSize;
782 }
783 // Ignore fixed holes that are in the previous stack frame.
784 if (ObjEnd > 0)
785 StackBytesFree.reset(ObjStart, ObjEnd);
786 }
787}
788
789/// Assign frame object to an unused portion of the stack in the fixed stack
790/// object range. Return true if the allocation was successful.
791static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx,
792 bool StackGrowsDown, Align MaxAlign,
793 BitVector &StackBytesFree) {
794 if (MFI.isVariableSizedObjectIndex(FrameIdx))
795 return false;
796
797 if (StackBytesFree.none()) {
798 // clear it to speed up later scavengeStackSlot calls to
799 // StackBytesFree.none()
800 StackBytesFree.clear();
801 return false;
802 }
803
804 Align ObjAlign = MFI.getObjectAlign(FrameIdx);
805 if (ObjAlign > MaxAlign)
806 return false;
807
808 int64_t ObjSize = MFI.getObjectSize(FrameIdx);
809 int FreeStart;
810 for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
811 FreeStart = StackBytesFree.find_next(FreeStart)) {
812
813 // Check that free space has suitable alignment.
814 unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
815 if (alignTo(ObjStart, ObjAlign) != ObjStart)
816 continue;
817
818 if (FreeStart + ObjSize > StackBytesFree.size())
819 return false;
820
821 bool AllBytesFree = true;
822 for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
823 if (!StackBytesFree.test(FreeStart + Byte)) {
824 AllBytesFree = false;
825 break;
826 }
827 if (AllBytesFree)
828 break;
829 }
830
831 if (FreeStart == -1)
832 return false;
833
834 if (StackGrowsDown) {
835 int ObjStart = -(FreeStart + ObjSize);
836 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
837 << ObjStart << "]\n");
838 MFI.setObjectOffset(FrameIdx, ObjStart);
839 } else {
840 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
841 << FreeStart << "]\n");
842 MFI.setObjectOffset(FrameIdx, FreeStart);
843 }
844
845 StackBytesFree.reset(FreeStart, FreeStart + ObjSize);
846 return true;
847}
848
849/// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
850/// those required to be close to the Stack Protector) to stack offsets.
851static void AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
852 SmallSet<int, 16> &ProtectedObjs,
853 MachineFrameInfo &MFI, bool StackGrowsDown,
854 int64_t &Offset, Align &MaxAlign) {
855
856 for (int i : UnassignedObjs) {
857 AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
858 ProtectedObjs.insert(i);
859 }
860}
861
862/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
863/// abstract stack objects.
864void PEIImpl::calculateFrameObjectOffsets(MachineFunction &MF) {
865 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
866
867 bool StackGrowsDown =
869
870 // Loop over all of the stack objects, assigning sequential addresses...
871 MachineFrameInfo &MFI = MF.getFrameInfo();
872
873 // Start at the beginning of the local area.
874 // The Offset is the distance from the stack top in the direction
875 // of stack growth -- so it's always nonnegative.
876 int LocalAreaOffset = TFI.getOffsetOfLocalArea();
877 if (StackGrowsDown)
878 LocalAreaOffset = -LocalAreaOffset;
879 assert(LocalAreaOffset >= 0
880 && "Local area offset should be in direction of stack growth");
881 int64_t Offset = LocalAreaOffset;
882
883#ifdef EXPENSIVE_CHECKS
884 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i)
885 if (!MFI.isDeadObjectIndex(i) &&
887 assert(MFI.getObjectAlign(i) <= MFI.getMaxAlign() &&
888 "MaxAlignment is invalid");
889#endif
890
891 // If there are fixed sized objects that are preallocated in the local area,
892 // non-fixed objects can't be allocated right at the start of local area.
893 // Adjust 'Offset' to point to the end of last fixed sized preallocated
894 // object.
895 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
896 // Only allocate objects on the default stack.
898 continue;
899
900 int64_t FixedOff;
901 if (StackGrowsDown) {
902 // The maximum distance from the stack pointer is at lower address of
903 // the object -- which is given by offset. For down growing stack
904 // the offset is negative, so we negate the offset to get the distance.
905 FixedOff = -MFI.getObjectOffset(i);
906 } else {
907 // The maximum distance from the start pointer is at the upper
908 // address of the object.
909 FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
910 }
911 if (FixedOff > Offset) Offset = FixedOff;
912 }
913
914 Align MaxAlign = MFI.getMaxAlign();
915 // First assign frame offsets to stack objects that are used to spill
916 // callee saved registers.
917 auto AllFIs = seq(MFI.getObjectIndexBegin(), MFI.getObjectIndexEnd());
918 for (int FI : reverse_conditionally(AllFIs, /*Reverse=*/!StackGrowsDown)) {
919 // Only allocate objects on the default stack.
920 if (!MFI.isCalleeSavedObjectIndex(FI) ||
922 continue;
923
924 // TODO: should this just be if (MFI.isDeadObjectIndex(FI))
925 if (!StackGrowsDown && MFI.isDeadObjectIndex(FI))
926 continue;
927
928 AdjustStackOffset(MFI, FI, StackGrowsDown, Offset, MaxAlign);
929 }
930
931 assert(MaxAlign == MFI.getMaxAlign() &&
932 "MFI.getMaxAlign should already account for all callee-saved "
933 "registers without a fixed stack slot");
934
935 // FixedCSEnd is the stack offset to the end of the fixed and callee-save
936 // stack area.
937 int64_t FixedCSEnd = Offset;
938
939 // Make sure the special register scavenging spill slot is closest to the
940 // incoming stack pointer if a frame pointer is required and is closer
941 // to the incoming rather than the final stack pointer.
942 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
943 bool EarlyScavengingSlots = TFI.allocateScavengingFrameIndexesNearIncomingSP(MF);
944 if (RS && EarlyScavengingSlots) {
945 SmallVector<int, 2> SFIs;
947 for (int SFI : SFIs)
948 AdjustStackOffset(MFI, SFI, StackGrowsDown, Offset, MaxAlign);
949 }
950
951 // FIXME: Once this is working, then enable flag will change to a target
952 // check for whether the frame is large enough to want to use virtual
953 // frame index registers. Functions which don't want/need this optimization
954 // will continue to use the existing code path.
957
958 // Adjust to alignment boundary.
959 Offset = alignTo(Offset, Alignment);
960
961 LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
962
963 // Resolve offsets for objects in the local block.
964 for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
965 std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
966 int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
967 LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
968 << "]\n");
969 MFI.setObjectOffset(Entry.first, FIOffset);
970 }
971 // Allocate the local block
972 Offset += MFI.getLocalFrameSize();
973
974 MaxAlign = std::max(Alignment, MaxAlign);
975 }
976
977 // Retrieve the Exception Handler registration node.
978 int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
979 if (const WinEHFuncInfo *FuncInfo = MF.getWinEHFuncInfo())
980 EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
981
982 // Make sure that the stack protector comes before the local variables on the
983 // stack.
984 SmallSet<int, 16> ProtectedObjs;
985 if (MFI.hasStackProtectorIndex()) {
986 int StackProtectorFI = MFI.getStackProtectorIndex();
987 StackObjSet LargeArrayObjs;
988 StackObjSet SmallArrayObjs;
989 StackObjSet AddrOfObjs;
990
991 // If we need a stack protector, we need to make sure that
992 // LocalStackSlotPass didn't already allocate a slot for it.
993 // If we are told to use the LocalStackAllocationBlock, the stack protector
994 // is expected to be already pre-allocated.
995 if (MFI.getStackID(StackProtectorFI) != TargetStackID::Default) {
996 // If the stack protector isn't on the default stack then it's up to the
997 // target to set the stack offset.
998 assert(MFI.getObjectOffset(StackProtectorFI) != 0 &&
999 "Offset of stack protector on non-default stack expected to be "
1000 "already set.");
1002 "Stack protector on non-default stack expected to not be "
1003 "pre-allocated by LocalStackSlotPass.");
1004 } else if (!MFI.getUseLocalStackAllocationBlock()) {
1005 AdjustStackOffset(MFI, StackProtectorFI, StackGrowsDown, Offset,
1006 MaxAlign);
1007 } else if (!MFI.isObjectPreAllocated(MFI.getStackProtectorIndex())) {
1009 "Stack protector not pre-allocated by LocalStackSlotPass.");
1010 }
1011
1012 // Assign large stack objects first.
1013 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
1015 continue;
1016 if (MFI.isCalleeSavedObjectIndex(i))
1017 continue;
1018 if (RS && RS->isScavengingFrameIndex((int)i))
1019 continue;
1020 if (MFI.isDeadObjectIndex(i))
1021 continue;
1022 if (StackProtectorFI == (int)i || EHRegNodeFrameIndex == (int)i)
1023 continue;
1024 // Only allocate objects on the default stack.
1025 if (MFI.getStackID(i) != TargetStackID::Default)
1026 continue;
1027
1028 switch (MFI.getObjectSSPLayout(i)) {
1030 continue;
1032 SmallArrayObjs.insert(i);
1033 continue;
1035 AddrOfObjs.insert(i);
1036 continue;
1038 LargeArrayObjs.insert(i);
1039 continue;
1040 }
1041 llvm_unreachable("Unexpected SSPLayoutKind.");
1042 }
1043
1044 // We expect **all** the protected stack objects to be pre-allocated by
1045 // LocalStackSlotPass. If it turns out that PEI still has to allocate some
1046 // of them, we may end up messing up the expected order of the objects.
1048 !(LargeArrayObjs.empty() && SmallArrayObjs.empty() &&
1049 AddrOfObjs.empty()))
1050 llvm_unreachable("Found protected stack objects not pre-allocated by "
1051 "LocalStackSlotPass.");
1052
1053 AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
1054 Offset, MaxAlign);
1055 AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
1056 Offset, MaxAlign);
1057 AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
1058 Offset, MaxAlign);
1059 }
1060
1061 SmallVector<int, 8> ObjectsToAllocate;
1062
1063 // Then prepare to assign frame offsets to stack objects that are not used to
1064 // spill callee saved registers.
1065 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
1067 continue;
1068 if (MFI.isCalleeSavedObjectIndex(i))
1069 continue;
1070 if (RS && RS->isScavengingFrameIndex((int)i))
1071 continue;
1072 if (MFI.isDeadObjectIndex(i))
1073 continue;
1074 if (MFI.getStackProtectorIndex() == (int)i || EHRegNodeFrameIndex == (int)i)
1075 continue;
1076 if (ProtectedObjs.count(i))
1077 continue;
1078 // Only allocate objects on the default stack.
1079 if (MFI.getStackID(i) != TargetStackID::Default)
1080 continue;
1081
1082 // Add the objects that we need to allocate to our working set.
1083 ObjectsToAllocate.push_back(i);
1084 }
1085
1086 // Allocate the EH registration node first if one is present.
1087 if (EHRegNodeFrameIndex != std::numeric_limits<int>::max())
1088 AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset,
1089 MaxAlign);
1090
1091 // Give the targets a chance to order the objects the way they like it.
1092 if (MF.getTarget().getOptLevel() != CodeGenOptLevel::None &&
1094 TFI.orderFrameObjects(MF, ObjectsToAllocate);
1095
1096 // Keep track of which bytes in the fixed and callee-save range are used so we
1097 // can use the holes when allocating later stack objects. Only do this if
1098 // stack protector isn't being used and the target requests it and we're
1099 // optimizing.
1100 BitVector StackBytesFree;
1101 if (!ObjectsToAllocate.empty() &&
1102 MF.getTarget().getOptLevel() != CodeGenOptLevel::None &&
1104 computeFreeStackSlots(MFI, StackGrowsDown, FixedCSEnd, StackBytesFree);
1105
1106 // Now walk the objects and actually assign base offsets to them.
1107 for (auto &Object : ObjectsToAllocate)
1108 if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign,
1109 StackBytesFree))
1110 AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign);
1111
1112 // Make sure the special register scavenging spill slot is closest to the
1113 // stack pointer.
1114 if (RS && !EarlyScavengingSlots) {
1115 SmallVector<int, 2> SFIs;
1116 RS->getScavengingFrameIndices(SFIs);
1117 for (int SFI : SFIs)
1118 AdjustStackOffset(MFI, SFI, StackGrowsDown, Offset, MaxAlign);
1119 }
1120
1122 // If we have reserved argument space for call sites in the function
1123 // immediately on entry to the current function, count it as part of the
1124 // overall stack size.
1125 if (MFI.adjustsStack() && TFI.hasReservedCallFrame(MF))
1126 Offset += MFI.getMaxCallFrameSize();
1127
1128 // Round up the size to a multiple of the alignment. If the function has
1129 // any calls or alloca's, align to the target's StackAlignment value to
1130 // ensure that the callee's frame or the alloca data is suitably aligned;
1131 // otherwise, for leaf functions, align to the TransientStackAlignment
1132 // value.
1133 Align StackAlign;
1134 if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
1135 (RegInfo->hasStackRealignment(MF) && MFI.getObjectIndexEnd() != 0))
1136 StackAlign = TFI.getStackAlign();
1137 else
1138 StackAlign = TFI.getTransientStackAlign();
1139
1140 // If the frame pointer is eliminated, all frame offsets will be relative to
1141 // SP not FP. Align to MaxAlign so this works.
1142 StackAlign = std::max(StackAlign, MaxAlign);
1143 int64_t OffsetBeforeAlignment = Offset;
1144 Offset = alignTo(Offset, StackAlign);
1145
1146 // If we have increased the offset to fulfill the alignment constrants,
1147 // then the scavenging spill slots may become harder to reach from the
1148 // stack pointer, float them so they stay close.
1149 if (StackGrowsDown && OffsetBeforeAlignment != Offset && RS &&
1150 !EarlyScavengingSlots) {
1151 SmallVector<int, 2> SFIs;
1152 RS->getScavengingFrameIndices(SFIs);
1153 LLVM_DEBUG(if (!SFIs.empty()) llvm::dbgs()
1154 << "Adjusting emergency spill slots!\n";);
1155 int64_t Delta = Offset - OffsetBeforeAlignment;
1156 for (int SFI : SFIs) {
1158 << "Adjusting offset of emergency spill slot #" << SFI
1159 << " from " << MFI.getObjectOffset(SFI););
1160 MFI.setObjectOffset(SFI, MFI.getObjectOffset(SFI) - Delta);
1161 LLVM_DEBUG(llvm::dbgs() << " to " << MFI.getObjectOffset(SFI) << "\n";);
1162 }
1163 }
1164 }
1165
1166 // Update frame info to pretend that this is part of the stack...
1167 int64_t StackSize = Offset - LocalAreaOffset;
1168 MFI.setStackSize(StackSize);
1169 NumBytesStackSpace += StackSize;
1170}
1171
1172/// insertPrologEpilogCode - Scan the function for modified callee saved
1173/// registers, insert spill code for these callee saved registers, then add
1174/// prolog and epilog code to the function.
1175void PEIImpl::insertPrologEpilogCode(MachineFunction &MF) {
1176 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1177
1178 // Add prologue to the function...
1179 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1180 TFI.emitPrologue(MF, *SaveBlock);
1181
1182 // Add epilogue to restore the callee-save registers in each exiting block.
1183 for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
1184 TFI.emitEpilogue(MF, *RestoreBlock);
1185
1186 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1187 TFI.inlineStackProbe(MF, *SaveBlock);
1188
1189 // Emit additional code that is required to support segmented stacks, if
1190 // we've been asked for it. This, when linked with a runtime with support
1191 // for segmented stacks (libgcc is one), will result in allocating stack
1192 // space in small chunks instead of one large contiguous block.
1193 if (MF.shouldSplitStack()) {
1194 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1195 TFI.adjustForSegmentedStacks(MF, *SaveBlock);
1196 }
1197
1198 // Emit additional code that is required to explicitly handle the stack in
1199 // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
1200 // approach is rather similar to that of Segmented Stacks, but it uses a
1201 // different conditional check and another BIF for allocating more stack
1202 // space.
1203 if (MF.getFunction().getCallingConv() == CallingConv::HiPE)
1204 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1205 TFI.adjustForHiPEPrologue(MF, *SaveBlock);
1206}
1207
1208/// insertZeroCallUsedRegs - Zero out call used registers.
1209void PEIImpl::insertZeroCallUsedRegs(MachineFunction &MF) {
1210 const Function &F = MF.getFunction();
1211
1212 if (!F.hasFnAttribute("zero-call-used-regs"))
1213 return;
1214
1215 using namespace ZeroCallUsedRegs;
1216
1217 ZeroCallUsedRegsKind ZeroRegsKind =
1218 StringSwitch<ZeroCallUsedRegsKind>(
1219 F.getFnAttribute("zero-call-used-regs").getValueAsString())
1220 .Case("skip", ZeroCallUsedRegsKind::Skip)
1221 .Case("used-gpr-arg", ZeroCallUsedRegsKind::UsedGPRArg)
1222 .Case("used-gpr", ZeroCallUsedRegsKind::UsedGPR)
1223 .Case("used-arg", ZeroCallUsedRegsKind::UsedArg)
1224 .Case("used", ZeroCallUsedRegsKind::Used)
1225 .Case("all-gpr-arg", ZeroCallUsedRegsKind::AllGPRArg)
1226 .Case("all-gpr", ZeroCallUsedRegsKind::AllGPR)
1227 .Case("all-arg", ZeroCallUsedRegsKind::AllArg)
1228 .Case("all", ZeroCallUsedRegsKind::All);
1229
1230 if (ZeroRegsKind == ZeroCallUsedRegsKind::Skip)
1231 return;
1232
1233 const bool OnlyGPR = static_cast<unsigned>(ZeroRegsKind) & ONLY_GPR;
1234 const bool OnlyUsed = static_cast<unsigned>(ZeroRegsKind) & ONLY_USED;
1235 const bool OnlyArg = static_cast<unsigned>(ZeroRegsKind) & ONLY_ARG;
1236
1237 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1238 const BitVector AllocatableSet(TRI.getAllocatableSet(MF));
1239
1240 // Mark all used registers.
1241 BitVector UsedRegs(TRI.getNumRegs());
1242 if (OnlyUsed)
1243 for (const MachineBasicBlock &MBB : MF)
1244 for (const MachineInstr &MI : MBB) {
1245 // skip debug instructions
1246 if (MI.isDebugInstr())
1247 continue;
1248
1249 for (const MachineOperand &MO : MI.operands()) {
1250 if (!MO.isReg())
1251 continue;
1252
1253 MCRegister Reg = MO.getReg();
1254 if (AllocatableSet[Reg.id()] && !MO.isImplicit() &&
1255 (MO.isDef() || MO.isUse()))
1256 UsedRegs.set(Reg.id());
1257 }
1258 }
1259
1260 // Get a list of registers that are used.
1261 BitVector LiveIns(TRI.getNumRegs());
1262 for (const MachineBasicBlock::RegisterMaskPair &LI : MF.front().liveins())
1263 LiveIns.set(LI.PhysReg);
1264
1265 BitVector RegsToZero(TRI.getNumRegs());
1266 for (MCRegister Reg : AllocatableSet.set_bits()) {
1267 // Skip over fixed registers.
1268 if (TRI.isFixedRegister(MF, Reg))
1269 continue;
1270
1271 // Want only general purpose registers.
1272 if (OnlyGPR && !TRI.isGeneralPurposeRegister(MF, Reg))
1273 continue;
1274
1275 // Want only used registers.
1276 if (OnlyUsed && !UsedRegs[Reg.id()])
1277 continue;
1278
1279 // Want only registers used for arguments.
1280 if (OnlyArg) {
1281 if (OnlyUsed) {
1282 for (MCRegister LiveReg : LiveIns.set_bits()) {
1283 if (TRI.regsOverlap(Reg, LiveReg))
1284 RegsToZero.set(LiveReg);
1285 }
1286 continue;
1287 } else if (!TRI.isArgumentRegister(MF, Reg)) {
1288 continue;
1289 }
1290 }
1291
1292 RegsToZero.set(Reg.id());
1293 }
1294
1295 // Don't clear registers that are live when leaving the function.
1296 for (const MachineBasicBlock &MBB : MF)
1297 for (const MachineInstr &MI : MBB.terminators()) {
1298 if (!MI.isReturn())
1299 continue;
1300
1301 for (const auto &MO : MI.operands()) {
1302 if (!MO.isReg())
1303 continue;
1304
1305 MCRegister Reg = MO.getReg();
1306 if (!Reg)
1307 continue;
1308
1309 // This picks up sibling registers (e.q. %al -> %ah).
1310 // FIXME: Mixing physical registers and register units is likely a bug.
1311 for (MCRegUnit Unit : TRI.regunits(Reg))
1312 RegsToZero.reset(static_cast<unsigned>(Unit));
1313
1314 for (MCPhysReg SReg : TRI.sub_and_superregs_inclusive(Reg))
1315 RegsToZero.reset(SReg);
1316 }
1317 }
1318
1319 // Don't need to clear registers that are used/clobbered by terminating
1320 // instructions.
1321 for (const MachineBasicBlock &MBB : MF) {
1322 if (!MBB.isReturnBlock())
1323 continue;
1324
1327 ++I) {
1328 for (const MachineOperand &MO : I->operands()) {
1329 if (!MO.isReg())
1330 continue;
1331
1332 MCRegister Reg = MO.getReg();
1333 if (!Reg)
1334 continue;
1335
1336 for (const MCPhysReg Reg : TRI.sub_and_superregs_inclusive(Reg))
1337 RegsToZero.reset(Reg);
1338 }
1339 }
1340 }
1341
1342 // Don't clear registers that must be preserved.
1343 for (const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
1344 MCPhysReg CSReg = *CSRegs; ++CSRegs)
1345 for (MCRegister Reg : TRI.sub_and_superregs_inclusive(CSReg))
1346 RegsToZero.reset(Reg.id());
1347
1348 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1349 for (MachineBasicBlock &MBB : MF)
1350 if (MBB.isReturnBlock())
1351 TFI.emitZeroCallUsedRegs(RegsToZero, MBB, RS);
1352}
1353
1354/// Replace all FrameIndex operands with physical register references and actual
1355/// offsets.
1356void PEIImpl::replaceFrameIndicesBackward(MachineFunction &MF) {
1357 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1358
1359 for (auto &MBB : MF) {
1360 int SPAdj = 0;
1361 if (!MBB.succ_empty()) {
1362 // Get the SP adjustment for the end of MBB from the start of any of its
1363 // successors. They should all be the same.
1364 assert(all_of(MBB.successors(), [&MBB](const MachineBasicBlock *Succ) {
1365 return Succ->getCallFrameSize() ==
1366 (*MBB.succ_begin())->getCallFrameSize();
1367 }));
1368 const MachineBasicBlock &FirstSucc = **MBB.succ_begin();
1369 SPAdj = TFI.alignSPAdjust(FirstSucc.getCallFrameSize());
1371 SPAdj = -SPAdj;
1372 }
1373
1374 replaceFrameIndicesBackward(&MBB, MF, SPAdj);
1375
1376 // We can't track the call frame size after call frame pseudos have been
1377 // eliminated. Set it to zero everywhere to keep MachineVerifier happy.
1379 }
1380}
1381
1382/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1383/// register references and actual offsets.
1384void PEIImpl::replaceFrameIndices(MachineFunction &MF) {
1385 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1386
1387 for (auto &MBB : MF) {
1388 int SPAdj = TFI.alignSPAdjust(MBB.getCallFrameSize());
1390 SPAdj = -SPAdj;
1391
1392 replaceFrameIndices(&MBB, MF, SPAdj);
1393
1394 // We can't track the call frame size after call frame pseudos have been
1395 // eliminated. Set it to zero everywhere to keep MachineVerifier happy.
1397 }
1398}
1399
1400bool PEIImpl::replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI,
1401 unsigned OpIdx, int SPAdj) {
1402 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
1403 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1404 if (MI.isDebugValue()) {
1405
1406 MachineOperand &Op = MI.getOperand(OpIdx);
1407 assert(MI.isDebugOperand(&Op) &&
1408 "Frame indices can only appear as a debug operand in a DBG_VALUE*"
1409 " machine instruction");
1410 Register Reg;
1411 unsigned FrameIdx = Op.getIndex();
1412 unsigned Size = MF.getFrameInfo().getObjectSize(FrameIdx);
1413
1414 StackOffset Offset = TFI->getFrameIndexReference(MF, FrameIdx, Reg);
1415 Op.ChangeToRegister(Reg, false /*isDef*/);
1416
1417 const DIExpression *DIExpr = MI.getDebugExpression();
1418
1419 // If we have a direct DBG_VALUE, and its location expression isn't
1420 // currently complex, then adding an offset will morph it into a
1421 // complex location that is interpreted as being a memory address.
1422 // This changes a pointer-valued variable to dereference that pointer,
1423 // which is incorrect. Fix by adding DW_OP_stack_value.
1424
1425 if (MI.isNonListDebugValue()) {
1426 unsigned PrependFlags = DIExpression::ApplyOffset;
1427 if (!MI.isIndirectDebugValue() && !DIExpr->isComplex())
1428 PrependFlags |= DIExpression::StackValue;
1429
1430 // If we have DBG_VALUE that is indirect and has a Implicit location
1431 // expression need to insert a deref before prepending a Memory
1432 // location expression. Also after doing this we change the DBG_VALUE
1433 // to be direct.
1434 if (MI.isIndirectDebugValue() && DIExpr->isImplicit()) {
1435 // DW_OP_deref_size may not read more than the size of an address.
1436 if (Size > MF.getDataLayout().getPointerSize()) {
1437 LLVM_DEBUG(dbgs() << "Dropping location of object larger than an "
1438 "address: "
1439 << MI);
1440 MI.setDebugValueUndef();
1441 return true;
1442 }
1444 "DW_OP_deref_size operand must fit in a byte");
1445 SmallVector<uint64_t, 2> Ops = {dwarf::DW_OP_deref_size, Size};
1446 bool WithStackValue = true;
1447 DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue);
1448 // Make the DBG_VALUE direct.
1449 MI.getDebugOffset().ChangeToRegister(0, false);
1450 }
1451 DIExpr = TRI.prependOffsetExpression(DIExpr, PrependFlags, Offset);
1452 } else {
1453 // The debug operand at DebugOpIndex was a frame index at offset
1454 // `Offset`; now the operand has been replaced with the frame
1455 // register, we must add Offset with `register x, plus Offset`.
1456 unsigned DebugOpIndex = MI.getDebugOperandIndex(&Op);
1458 TRI.getOffsetOpcodes(Offset, Ops);
1459 DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, DebugOpIndex);
1460 }
1461 MI.getDebugExpressionOp().setMetadata(DIExpr);
1462 return true;
1463 }
1464
1465 if (MI.isDebugPHI()) {
1466 // Allow stack ref to continue onwards.
1467 return true;
1468 }
1469
1470 // TODO: This code should be commoned with the code for
1471 // PATCHPOINT. There's no good reason for the difference in
1472 // implementation other than historical accident. The only
1473 // remaining difference is the unconditional use of the stack
1474 // pointer as the base register.
1475 if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
1476 assert((!MI.isDebugValue() || OpIdx == 0) &&
1477 "Frame indices can only appear as the first operand of a "
1478 "DBG_VALUE machine instruction");
1479 Register Reg;
1480 MachineOperand &Offset = MI.getOperand(OpIdx + 1);
1481 StackOffset refOffset = TFI->getFrameIndexReferencePreferSP(
1482 MF, MI.getOperand(OpIdx).getIndex(), Reg, /*IgnoreSPUpdates*/ false);
1483 assert(!refOffset.getScalable() &&
1484 "Frame offsets with a scalable component are not supported");
1485 Offset.setImm(Offset.getImm() + refOffset.getFixed() + SPAdj);
1486 MI.getOperand(OpIdx).ChangeToRegister(Reg, false /*isDef*/);
1487 return true;
1488 }
1489 return false;
1490}
1491
1492void PEIImpl::replaceFrameIndicesBackward(MachineBasicBlock *BB,
1493 MachineFunction &MF, int &SPAdj) {
1495 "getRegisterInfo() must be implemented!");
1496
1497 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1498 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1499 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1500
1501 RegScavenger *LocalRS = FrameIndexEliminationScavenging ? RS : nullptr;
1502 if (LocalRS)
1503 LocalRS->enterBasicBlockEnd(*BB);
1504
1505 for (MachineBasicBlock::iterator I = BB->end(); I != BB->begin();) {
1506 MachineInstr &MI = *std::prev(I);
1507
1508 if (TII.isFrameInstr(MI)) {
1509 SPAdj -= TII.getSPAdjust(MI);
1510 TFI.eliminateCallFramePseudoInstr(MF, *BB, &MI);
1511 continue;
1512 }
1513
1514 // Step backwards to get the liveness state at (immedately after) MI.
1515 if (LocalRS)
1516 LocalRS->backward(I);
1517
1518 bool RemovedMI = false;
1519 for (const auto &[Idx, Op] : enumerate(MI.operands())) {
1520 if (!Op.isFI())
1521 continue;
1522
1523 if (replaceFrameIndexDebugInstr(MF, MI, Idx, SPAdj))
1524 continue;
1525
1526 // Eliminate this FrameIndex operand.
1527 RemovedMI = TRI.eliminateFrameIndex(MI, SPAdj, Idx, LocalRS);
1528 if (RemovedMI)
1529 break;
1530 }
1531
1532 if (!RemovedMI)
1533 --I;
1534 }
1535}
1536
1537void PEIImpl::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1538 int &SPAdj) {
1540 "getRegisterInfo() must be implemented!");
1541 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1542 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1543 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
1544
1545 bool InsideCallSequence = false;
1546
1547 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
1548 if (TII.isFrameInstr(*I)) {
1549 InsideCallSequence = TII.isFrameSetup(*I);
1550 SPAdj += TII.getSPAdjust(*I);
1551 I = TFI->eliminateCallFramePseudoInstr(MF, *BB, I);
1552 continue;
1553 }
1554
1555 MachineInstr &MI = *I;
1556 bool DoIncr = true;
1557 bool DidFinishLoop = true;
1558 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1559 if (!MI.getOperand(i).isFI())
1560 continue;
1561
1562 if (replaceFrameIndexDebugInstr(MF, MI, i, SPAdj))
1563 continue;
1564
1565 // Some instructions (e.g. inline asm instructions) can have
1566 // multiple frame indices and/or cause eliminateFrameIndex
1567 // to insert more than one instruction. We need the register
1568 // scavenger to go through all of these instructions so that
1569 // it can update its register information. We keep the
1570 // iterator at the point before insertion so that we can
1571 // revisit them in full.
1572 bool AtBeginning = (I == BB->begin());
1573 if (!AtBeginning) --I;
1574
1575 // If this instruction has a FrameIndex operand, we need to
1576 // use that target machine register info object to eliminate
1577 // it.
1578 TRI.eliminateFrameIndex(MI, SPAdj, i, RS);
1579
1580 // Reset the iterator if we were at the beginning of the BB.
1581 if (AtBeginning) {
1582 I = BB->begin();
1583 DoIncr = false;
1584 }
1585
1586 DidFinishLoop = false;
1587 break;
1588 }
1589
1590 // If we are looking at a call sequence, we need to keep track of
1591 // the SP adjustment made by each instruction in the sequence.
1592 // This includes both the frame setup/destroy pseudos (handled above),
1593 // as well as other instructions that have side effects w.r.t the SP.
1594 // Note that this must come after eliminateFrameIndex, because
1595 // if I itself referred to a frame index, we shouldn't count its own
1596 // adjustment.
1597 if (DidFinishLoop && InsideCallSequence)
1598 SPAdj += TII.getSPAdjust(MI);
1599
1600 if (DoIncr && I != BB->end())
1601 ++I;
1602 }
1603}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file contains the simple types necessary to represent the attributes associated with functions a...
This file implements the BitVector class.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static bool replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI, unsigned OpIdx)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#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
static void insertCSRRestores(MachineBasicBlock &RestoreBlock, std::vector< CalleeSavedInfo > &CSI)
Insert restore code for the callee-saved registers used in the function.
SmallVector< MachineBasicBlock *, 4 > MBBVector
static bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx, bool StackGrowsDown, Align MaxAlign, BitVector &StackBytesFree)
Assign frame object to an unused portion of the stack in the fixed stack object range.
static void insertCSRSaves(MachineBasicBlock &SaveBlock, ArrayRef< CalleeSavedInfo > CSI)
Insert spill code for the callee-saved registers used in the function.
static void AssignProtectedObjSet(const StackObjSet &UnassignedObjs, SmallSet< int, 16 > &ProtectedObjs, MachineFrameInfo &MFI, bool StackGrowsDown, int64_t &Offset, Align &MaxAlign)
AssignProtectedObjSet - Helper function to assign large stack objects (i.e., those required to be clo...
static void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, bool StackGrowsDown, int64_t &Offset, Align &MaxAlign)
AdjustStackOffset - Helper function used to adjust the stack frame offset.
SmallDenseMap< MachineBasicBlock *, SmallVector< MachineInstr *, 4 >, 4 > SavedDbgValuesMap
static void computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown, int64_t FixedCSEnd, BitVector &StackBytesFree)
Compute which bytes of fixed and callee-save stack area are unused and keep track of them in StackByt...
static void updateLiveness(MachineFunction &MF)
Helper function to update the liveness information for the callee-saved registers.
SmallSetVector< int, 8 > StackObjSet
StackObjSet - A set of stack object indexes.
static void stashEntryDbgValues(MachineBasicBlock &MBB, SavedDbgValuesMap &EntryDbgValues)
Stash DBG_VALUEs that describe parameters and which are placed at the start of the block.
static void assignCalleeSavedSpillSlots(MachineFunction &F, const BitVector &SavedRegs)
MachineRegisterInfo & MRI
This file declares the machine register scavenger class.
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector 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
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:482
BitVector & reset()
Reset all bits in the bitvector.
Definition BitVector.h:409
int find_first() const
Returns the index of the first set bit, -1 if none of the bits are set.
Definition BitVector.h:317
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
Definition BitVector.h:355
void clear()
Removes all bits from the bitvector.
Definition BitVector.h:349
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
int find_next(unsigned Prev) const
Returns the index of the next set bit following the "Prev" bit.
Definition BitVector.h:324
bool none() const
Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
Returns the number of bits in this bitvector.
Definition BitVector.h:178
bool empty() const
Returns whether there are no bits in this bitvector.
Definition BitVector.h:175
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
DWARF expression.
LLVM_ABI bool isImplicit() const
Return whether this is an implicit location description.
static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B)
Check if fragments overlap between a pair of FragmentInfos.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
LLVM_ABI bool isComplex() const
Return whether the location is computed on the expression stack, meaning it cannot be a simple regist...
static LLVM_ABI DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
LLVM_ABI unsigned getPointerSize(unsigned AS=0) const
The pointer representation size in bytes, rounded up to a whole number of bytes.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:766
DISubprogram * getSubprogram() const
Get the attached subprogram.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
MachineInstrBundleIterator< const MachineInstr > const_iterator
void setCallFrameSize(unsigned N)
Set the call frame size on entry to this basic block.
bool isEHFuncletEntry() const
Returns true if this is the entry block of an EH funclet.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< iterator > terminators()
unsigned getCallFrameSize() const
Return the call frame size on entry to this basic block.
iterator_range< succ_iterator > successors()
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const
bool isObjectPreAllocated(int ObjectIdx) const
Return true if the object was pre-allocated into the local block.
LLVM_ABI void computeMaxCallFrameSize(MachineFunction &MF, std::vector< MachineBasicBlock::iterator > *FrameSDOps=nullptr)
Computes the maximum size of a callframe.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
int64_t getLocalFrameObjectCount() const
Return the number of objects allocated into the local object block.
bool hasCalls() const
Return true if the current function has any function calls.
Align getMaxAlign() const
Return alignment of this function's frame.
Align getLocalFrameMaxAlign() const
Return the required alignment of the local object blob.
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
@ SSPLK_SmallArray
Array or nested array < SSP-buffer-size.
@ SSPLK_LargeArray
Array or nested array >= SSP-buffer-size.
@ SSPLK_AddrOf
The address of this allocation is exposed and triggered protection.
@ SSPLK_None
Did not trigger a stack protector.
bool isCalleeSavedObjectIndex(int ObjectIdx) const
std::pair< int, int64_t > getLocalFrameObjectMap(int i) const
Get the local offset mapping for a for an object.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
void setSavePoints(SaveRestorePoints NewSavePoints)
bool getUseLocalStackAllocationBlock() const
Get whether the local allocation blob should be allocated together or let PEI allocate the locals in ...
int getStackProtectorIndex() const
Return the index for the stack protector object.
void setCalleeSavedInfoValid(bool v)
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
bool isMaxCallFrameSizeComputed() const
int64_t getLocalFrameSize() const
Get the size of the local object blob.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
void setCalleeSavedInfo(std::vector< CalleeSavedInfo > CSI)
Used by prolog/epilog inserter to set the function's callee saved information.
bool isVariableSizedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a variable sized object.
uint64_t getUnsafeStackSize() const
int getObjectIndexEnd() const
Return one past the maximum frame object index.
bool hasStackProtectorIndex() const
void setRestorePoints(SaveRestorePoints NewRestorePoints)
LLVM_ABI int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, bool IsImmutable=false)
Create a spill slot at a fixed location on the stack.
uint8_t getStackID(int ObjectIdx) const
const SaveRestorePoints & getRestorePoints() const
void setIsCalleeSavedObjectIndex(int ObjectIdx, bool IsCalleeSaved)
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
void setStackSize(uint64_t Size)
Set the size of the stack.
int getObjectIndexBegin() const
Return the minimum frame object index.
const SaveRestorePoints & getSavePoints() const
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
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.
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
bool shouldSplitStack() const
Should we be emitting segmented stack stuff for the function.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const MachineBasicBlock & front() const
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI void enterBasicBlockEnd(MachineBasicBlock &MBB)
Start tracking liveness from the end of basic block MBB.
LLVM_ABI void backward()
Update internal register state and move MBB iterator backwards.
void getScavengingFrameIndices(SmallVectorImpl< int > &A) const
Get an array of scavenging frame indices.
bool isScavengingFrameIndex(int FI) const
Query whether a frame index is a scavenging frame index.
constexpr unsigned id() const
Definition Register.h:100
bool empty() const
Determine if the SetVector is empty or not.
Definition SetVector.h:100
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:346
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:176
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)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static StackOffset getScalable(int64_t Scalable)
Definition TypeSize.h:41
static StackOffset getFixed(int64_t Fixed)
Definition TypeSize.h:40
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:490
Information about stack frame layout on the target.
virtual void spillFPBP(MachineFunction &MF) const
If frame pointer or base pointer is clobbered by an instruction, we should spill/restore it around th...
virtual void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
virtual const SpillSlot * getCalleeSavedSpillSlots(unsigned &NumEntries) const
getCalleeSavedSpillSlots - This method returns a pointer to an array of pairs, that contains an entry...
virtual bool hasReservedCallFrame(const MachineFunction &MF) const
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
virtual bool enableStackSlotScavenging(const MachineFunction &MF) const
Returns true if the stack slot holes in the fixed and callee-save stack area should be used when allo...
virtual bool allocateScavengingFrameIndexesNearIncomingSP(const MachineFunction &MF) const
Control the placement of special register scavenging spill slots when allocating a stack frame.
Align getTransientStackAlign() const
getTransientStackAlignment - This method returns the number of bytes to which the stack pointer must ...
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
virtual uint64_t getStackThreshold() const
getStackThreshold - Return the maximum stack size
virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS=nullptr) const
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
virtual void inlineStackProbe(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Replace a StackProbe stub (if any) with the actual probe code inline.
void restoreCalleeSavedRegister(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const CalleeSavedInfo &CS, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
void spillCalleeSavedRegister(MachineBasicBlock &SaveBlock, MachineBasicBlock::iterator MI, const CalleeSavedInfo &CS, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
spillCalleeSavedRegister - Default implementation for spilling a single callee saved register.
virtual void orderFrameObjects(const MachineFunction &MF, SmallVectorImpl< int > &objectsToAllocate) const
Order the symbols in the local stack frame.
virtual void adjustForHiPEPrologue(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in the assembly prologue to ex...
virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
virtual bool needsFrameIndexResolution(const MachineFunction &MF) const
virtual MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
virtual void emitZeroCallUsedRegs(BitVector RegsToZero, MachineBasicBlock &MBB, RegScavenger *RS) const
emitZeroCallUsedRegs - Zeros out call used registers.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
virtual bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
virtual void processFunctionBeforeFrameIndicesReplaced(MachineFunction &MF, RegScavenger *RS=nullptr) const
processFunctionBeforeFrameIndicesReplaced - This method is called immediately before MO_FrameIndex op...
virtual StackOffset getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI, Register &FrameReg, bool IgnoreSPUpdates) const
Same as getFrameIndexReference, except that the stack pointer (as opposed to the frame pointer) will ...
StackDirection getStackGrowthDirection() const
getStackGrowthDirection - Return the direction the stack grows
virtual void adjustForSegmentedStacks(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Adjust the prologue to have the function use segmented stacks.
int alignSPAdjust(int SPAdj) const
alignSPAdjust - This method aligns the stack adjustment to the correct alignment.
virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const
canSimplifyCallFramePseudos - When possible, it's best to simplify the call frame pseudo ops before d...
virtual void emitRemarks(const MachineFunction &MF, MachineOptimizationRemarkEmitter *ORE) const
This method is called at the end of prolog/epilog code insertion, so targets can emit remarks based o...
virtual bool targetHandlesStackFrameRounding() const
targetHandlesStackFrameRounding - Returns true if the target is responsible for rounding up the stack...
virtual void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, MutableArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
virtual StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const
getFrameIndexReference - This method should return the base register and offset used to reference a f...
TargetInstrInfo - Interface to description of machine instruction set.
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
virtual bool usesPhysRegsForValues() const
True if the target uses physical regs (as nearly all targets do).
TargetOptions Options
unsigned StackSymbolOrdering
StackSymbolOrdering - When true, this will allow CodeGen to order the local stack symbols (for code s...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ Entry
Definition COFF.h:862
@ HiPE
Used by the High-Performance Erlang Compiler (HiPE).
Definition CallingConv.h:53
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
testing::Matcher< const detail::ErrorHolder & > Failed()
Definition Error.h:198
LLVM_ABI void scavengeFrameVirtualRegs(MachineFunction &MF, RegScavenger &RS)
Replaces all frame index virtual registers with physical registers.
LLVM_ABI MachineFunctionPass * createPrologEpilogInserterPass()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI char & PrologEpilogCodeInserterID
PrologEpilogCodeInserter - This pass inserts prolog and epilog code, and eliminates abstract frame re...
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
DenseMap< MachineBasicBlock *, std::vector< CalleeSavedInfo > > SaveRestorePoints
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1769
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:149
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
auto reverse_conditionally(ContainerTy &&C, bool ShouldReverse)
Return a range that conditionally reverses C.
Definition STLExtras.h:1439
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39