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