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