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