LLVM 24.0.0git
HexagonFrameLowering.cpp
Go to the documentation of this file.
1//===- HexagonFrameLowering.cpp - Define frame lowering -------------------===//
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
11#include "HexagonBlockRanges.h"
12#include "HexagonISelLowering.h"
13#include "HexagonInstrInfo.h"
15#include "HexagonRegisterInfo.h"
16#include "HexagonSubtarget.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/SmallSet.h"
43#include "llvm/IR/Attributes.h"
44#include "llvm/IR/DebugLoc.h"
47#include "llvm/IR/Function.h"
48#include "llvm/MC/MCDwarf.h"
50#include "llvm/Pass.h"
54#include "llvm/Support/Debug.h"
56#include "llvm/Support/LEB128.h"
60#include <algorithm>
61#include <cassert>
62#include <cstdint>
63#include <iterator>
64#include <limits>
65#include <map>
66#include <optional>
67#include <utility>
68#include <vector>
69
70#define DEBUG_TYPE "hexagon-pei"
71
73 llvm::Type *Ty) {
74 if (Ty->isVectorTy())
75 return HST.isTypeForHLX(Ty);
76 if (auto *ArrayTy = llvm::dyn_cast<llvm::ArrayType>(Ty))
77 return containsHLXObject(HST, ArrayTy->getElementType());
78 if (auto *StructTy = llvm::dyn_cast<llvm::StructType>(Ty))
79 return llvm::any_of(StructTy->elements(), [&](llvm::Type *EltTy) {
80 return containsHLXObject(HST, EltTy);
81 });
82 return false;
83}
84
85// Hexagon stack frame layout as defined by the ABI:
86//
87// Incoming arguments
88// passed via stack
89// |
90// |
91// SP during function's FP during function's |
92// +-- runtime (top of stack) runtime (bottom) --+ |
93// | | |
94// --++---------------------+------------------+-----------------++-+-------
95// | parameter area for | variable-size | fixed-size |LR| arg
96// | called functions | local objects | local objects |FP|
97// --+----------------------+------------------+-----------------+--+-------
98// <- size known -> <- size unknown -> <- size known ->
99//
100// Low address High address
101//
102// <--- stack growth
103//
104//
105// - In any circumstances, the outgoing function arguments are always accessi-
106// ble using the SP, and the incoming arguments are accessible using the FP.
107// - If the local objects are not aligned, they can always be accessed using
108// the FP.
109// - If there are no variable-sized objects, the local objects can always be
110// accessed using the SP, regardless whether they are aligned or not. (The
111// alignment padding will be at the bottom of the stack (highest address),
112// and so the offset with respect to the SP will be known at the compile-
113// -time.)
114//
115// The only complication occurs if there are both, local aligned objects, and
116// dynamically allocated (variable-sized) objects. The alignment pad will be
117// placed between the FP and the local objects, thus preventing the use of the
118// FP to access the local objects. At the same time, the variable-sized objects
119// will be between the SP and the local objects, thus introducing an unknown
120// distance from the SP to the locals.
121//
122// To avoid this problem, a new register is created that holds the aligned
123// address of the bottom of the stack, referred in the sources as AP (aligned
124// pointer). The AP will be equal to "FP-p", where "p" is the smallest pad
125// that aligns AP to the required boundary (a maximum of the alignments of
126// all stack objects, fixed- and variable-sized). All local objects[1] will
127// then use AP as the base pointer.
128// [1] The exception is with "fixed" stack objects. "Fixed" stack objects get
129// their name from being allocated at fixed locations on the stack, relative
130// to the FP. In the presence of dynamic allocation and local alignment, such
131// objects can only be accessed through the FP.
132//
133// Illustration of the AP:
134// FP --+
135// |
136// ---------------+---------------------+-----+-----------------------++-+--
137// Rest of the | Local stack objects | Pad | Fixed stack objects |LR|
138// stack frame | (aligned) | | (CSR, spills, etc.) |FP|
139// ---------------+---------------------+-----+-----------------+-----+--+--
140// |<-- Multiple of the -->|
141// stack alignment +-- AP
142//
143// The AP is set up at the beginning of the function. Since it is not a dedi-
144// cated (reserved) register, it needs to be kept live throughout the function
145// to be available as the base register for local object accesses.
146// Normally, an address of a stack objects is obtained by a pseudo-instruction
147// PS_fi. To access local objects with the AP register present, a different
148// pseudo-instruction needs to be used: PS_fia. The PS_fia takes one extra
149// argument compared to PS_fi: the first input register is the AP register.
150// This keeps the register live between its definition and its uses.
151
152// The AP register is originally set up using pseudo-instruction PS_aligna:
153// AP = PS_aligna A
154// where
155// A - required stack alignment
156// The alignment value must be the maximum of all alignments required by
157// any stack object.
158
159// The dynamic allocation uses a pseudo-instruction PS_alloca:
160// Rd = PS_alloca Rs, A
161// where
162// Rd - address of the allocated space
163// Rs - minimum size (the actual allocated can be larger to accommodate
164// alignment)
165// A - required alignment
166
167using namespace llvm;
168
171 const DebugLoc &DL) {
172 if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
173 return;
174
175 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
176 // Hexagon saves LR (R31) via allocframe. If there is no frame, LR is
177 // not on the regular stack and does not need shadow-stack protection.
178 if (!HST.getFrameLowering()->hasFP(MF))
179 return;
180
181 // The shadow call stack pointer has to survive arbitrary calls, so it is
182 // always one of the callee-saved registers R16-R27 (Hexagon ABI, "Register
183 // usage across calls"). It must also be reserved: besides keeping the
184 // register allocator away from it, reserving it keeps it out of the
185 // callee-saved set, so it is never spilled and restored as an ordinary
186 // callee-saved register - which would leave the epilogue below reading the
187 // *caller's* shadow-stack slot. The spill stubs are handled separately in
188 // useSpillFunction()/useRestoreFunction().
189 Register SCSPReg = HST.getSCSPReg();
190 const auto &HRI = *HST.getRegisterInfo();
191 if (!HST.isRegisterReservedByUser(SCSPReg))
192 // Lower-cased to match the spelling of the -ffixed-<reg> flag the user
193 // needs to pass; TRI names the register "R18".
194 report_fatal_error(Twine("Must reserve ") +
195 StringRef(HRI.getName(SCSPReg)).lower() +
196 " to use shadow call stack on Hexagon");
197
198 const auto &HII = *HST.getInstrInfo();
199
200 // SCSPReg = add(SCSPReg, #4)
201 BuildMI(MBB, MI, DL, HII.get(Hexagon::A2_addi), SCSPReg)
202 .addReg(SCSPReg)
203 .addImm(4)
205 // memw(SCSPReg + #-4) = r31
206 BuildMI(MBB, MI, DL, HII.get(Hexagon::S2_storeri_io))
207 .addReg(SCSPReg)
208 .addImm(-4)
209 .addReg(Hexagon::R31)
211
212 MBB.addLiveIn(SCSPReg);
213
214 if (!MF.needsFrameMoves())
215 return;
216
217 // CFI: DW_CFA_val_expression for the SCS register, DW_OP_bregN -4
218 // Tells the unwinder that the SCS register at entry = current value - 4.
219 unsigned DwarfSCSReg = HRI.getDwarfRegNum(SCSPReg, /*IsEH=*/true);
220 // DW_OP_breg0..DW_OP_breg31 (0x70..0x8f) are 32 opcodes indexed by
221 // register number, so the register number must fit in [0, 31].
222 assert(DwarfSCSReg < 32 && "SCS register should be < 32");
223 const char CFIInst[] = {
224 (char)dwarf::DW_CFA_val_expression,
225 (char)DwarfSCSReg,
226 2, // expression length
227 (char)(unsigned)(dwarf::DW_OP_breg0 + DwarfSCSReg),
228 (char)(-4 & 0x7f), // SLEB128 -4
229 };
231 .buildEscape(StringRef(CFIInst, sizeof(CFIInst)));
232}
233
236 const DebugLoc &DL) {
237 if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
238 return;
239
240 // hasFP() is true at both call sites: the non-vararg path in
241 // insertEpilogueInBlock returns early when !hasFP(), and the vararg+musl
242 // path is inside the hasFP() branch. Check defensively.
244 report_fatal_error("SCS epilogue requires a frame");
245
246 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
247 Register SCSPReg = HST.getSCSPReg();
248 const auto &HII = *HST.getInstrInfo();
249
250 // r31 = memw(SCSPReg + #-4)
251 BuildMI(MBB, MI, DL, HII.get(Hexagon::L2_loadri_io), Hexagon::R31)
252 .addReg(SCSPReg)
253 .addImm(-4)
255 // SCSPReg = add(SCSPReg, #-4)
256 BuildMI(MBB, MI, DL, HII.get(Hexagon::A2_addi), SCSPReg)
257 .addReg(SCSPReg)
258 .addImm(-4)
260
261 if (MF.needsFrameMoves())
263}
264
265static cl::opt<bool> DisableDeallocRet("disable-hexagon-dealloc-ret",
266 cl::Hidden, cl::desc("Disable Dealloc Return for Hexagon target"));
267
269 NumberScavengerSlots("number-scavenger-slots", cl::Hidden,
270 cl::desc("Set the number of scavenger slots"),
271 cl::init(2));
272
273static cl::opt<int>
274 SpillFuncThreshold("spill-func-threshold", cl::Hidden,
275 cl::desc("Specify O2(not Os) spill func threshold"),
276 cl::init(6));
277
278static cl::opt<int>
279 SpillFuncThresholdOs("spill-func-threshold-Os", cl::Hidden,
280 cl::desc("Specify Os spill func threshold"),
281 cl::init(1));
282
284 "enable-stackovf-sanitizer", cl::Hidden,
285 cl::desc("Enable runtime checks for stack overflow."), cl::init(false));
286
287static cl::opt<bool>
288 EnableShrinkWrapping("hexagon-shrink-frame", cl::init(true), cl::Hidden,
289 cl::desc("Enable stack frame shrink wrapping"));
290
292 ShrinkLimit("shrink-frame-limit",
293 cl::init(std::numeric_limits<unsigned>::max()), cl::Hidden,
294 cl::desc("Max count of stack frame shrink-wraps"));
295
296static cl::opt<bool>
297 EnableSaveRestoreLong("enable-save-restore-long", cl::Hidden,
298 cl::desc("Enable long calls for save-restore stubs."),
299 cl::init(false));
300
302 "hlx-vector-stack-limit", cl::Hidden,
303 cl::init(std::numeric_limits<unsigned>::max()),
304 cl::desc("Maximum HLX vector (VTCM) stack frame size in bytes"));
305
306static cl::opt<bool> EliminateFramePointer("hexagon-fp-elim", cl::init(true),
307 cl::Hidden, cl::desc("Refrain from using FP whenever possible"));
308
309static cl::opt<bool> OptimizeSpillSlots("hexagon-opt-spill", cl::Hidden,
310 cl::init(true), cl::desc("Optimize spill slots"));
311
313 "hexagon-save-parent-vsp", cl::Hidden, cl::init(false),
314 cl::desc("Save the caller's VSP on the stack in functions with an HLX "
315 "vector stack frame, for debuggers"));
316
317#ifndef NDEBUG
319 cl::init(std::numeric_limits<unsigned>::max()));
320static unsigned SpillOptCount = 0;
321#endif
322
323namespace {
324
325 class HexagonCallFrameInformation : public MachineFunctionPass {
326 public:
327 static char ID;
328
329 HexagonCallFrameInformation() : MachineFunctionPass(ID) {}
330
331 bool runOnMachineFunction(MachineFunction &MF) override;
332
333 MachineFunctionProperties getRequiredProperties() const override {
334 return MachineFunctionProperties().setNoVRegs();
335 }
336 };
337
338 char HexagonCallFrameInformation::ID = 0;
339
340} // end anonymous namespace
341
342bool HexagonCallFrameInformation::runOnMachineFunction(MachineFunction &MF) {
343 auto &HFI = *MF.getSubtarget<HexagonSubtarget>().getFrameLowering();
344 bool NeedCFI = MF.needsFrameMoves();
345
346 if (!NeedCFI)
347 return false;
348 HFI.insertCFIInstructions(MF);
349 return true;
350}
351
352INITIALIZE_PASS(HexagonCallFrameInformation, "hexagon-cfi",
353 "Hexagon call frame information", false, false)
354
356 return new HexagonCallFrameInformation();
357}
358
359/// Map a register pair Reg to the subregister that has the greater "number",
360/// i.e. D3 (aka R7:6) will be mapped to R7, etc.
362 const TargetRegisterInfo &TRI,
363 bool hireg = true) {
364 if (Reg < Hexagon::D0 || Reg > Hexagon::D15)
365 return Reg;
366
367 Register RegNo = 0;
368 for (MCPhysReg SubReg : TRI.subregs(Reg)) {
369 if (hireg) {
370 if (SubReg > RegNo)
371 RegNo = SubReg;
372 } else {
373 if (!RegNo || SubReg < RegNo)
374 RegNo = SubReg;
375 }
376 }
377 return RegNo;
378}
379
380/// Returns the callee saved register with the largest id in the vector.
382 const TargetRegisterInfo &TRI) {
383 static_assert(Hexagon::R1 > 0,
384 "Assume physical registers are encoded as positive integers");
385 if (CSI.empty())
386 return 0;
387
388 Register Max = getMax32BitSubRegister(CSI[0].getReg(), TRI);
389 for (unsigned I = 1, E = CSI.size(); I < E; ++I) {
391 if (Reg > Max)
392 Max = Reg;
393 }
394 return Max;
395}
396
397/// Checks if the basic block contains any instruction that needs a stack
398/// frame to be already in place.
399static bool needsStackFrame(const MachineBasicBlock &MBB, const BitVector &CSR,
400 const HexagonRegisterInfo &HRI) {
401 const MachineFunction *MF = MBB.getParent();
402 if (&MBB == &MF->front() && MF->getInfo<HexagonMachineFunctionInfo>()
404 .isValid())
405 return true;
406
407 for (const MachineInstr &MI : MBB) {
408 if (MI.isCall())
409 return true;
410 unsigned Opc = MI.getOpcode();
411 switch (Opc) {
412 case Hexagon::PS_alloca:
413 return true;
414 default:
415 break;
416 }
417 // Check individual operands.
418 for (const MachineOperand &MO : MI.operands()) {
419 // While the presence of a frame index does not prove that a stack
420 // frame will be required, all frame indexes should be within alloc-
421 // frame/deallocframe. Otherwise, the code that translates a frame
422 // index into an offset would have to be aware of the placement of
423 // the frame creation/destruction instructions.
424 if (MO.isFI())
425 return true;
426 if (MO.isReg()) {
427 Register R = MO.getReg();
428 // Debug instructions may refer to $noreg.
429 if (!R)
430 continue;
431 // Virtual registers will need scavenging, which then may require
432 // a stack slot.
433 if (R.isVirtual())
434 return true;
435 for (MCPhysReg S : HRI.subregs_inclusive(R))
436 if (CSR[S])
437 return true;
438 continue;
439 }
440 if (MO.isRegMask()) {
441 // A regmask would normally have all callee-saved registers marked
442 // as preserved, so this check would not be needed, but in case of
443 // ever having other regmasks (for other calling conventions),
444 // make sure they would be processed correctly.
445 const uint32_t *BM = MO.getRegMask();
446 for (int x = CSR.find_first(); x >= 0; x = CSR.find_next(x)) {
447 unsigned R = x;
448 // If this regmask does not preserve a CSR, a frame will be needed.
449 if (!(BM[R / 32] & (1u << (R % 32))))
450 return true;
451 }
452 }
453 }
454 }
455 return false;
456}
457
458/// Returns true if MBB has a machine instructions that indicates a tail call
459/// in the block.
460static bool hasTailCall(const MachineBasicBlock &MBB) {
461 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
462 if (I == MBB.end())
463 return false;
464 unsigned RetOpc = I->getOpcode();
465 return RetOpc == Hexagon::PS_tailcall_i || RetOpc == Hexagon::PS_tailcall_r;
466}
467
468/// Returns true if MBB contains an instruction that returns.
469static bool hasReturn(const MachineBasicBlock &MBB) {
470 for (const MachineInstr &MI : MBB.terminators())
471 if (MI.isReturn())
472 return true;
473 return false;
474}
475
476/// Returns the "return" instruction from this block, or nullptr if there
477/// isn't any.
479 for (auto &I : MBB)
480 if (I.isReturn())
481 return &I;
482 return nullptr;
483}
484
485static bool isRestoreCall(unsigned Opc) {
486 switch (Opc) {
487 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
488 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
489 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT:
490 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT_PIC:
491 case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT:
492 case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC:
493 case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4:
494 case Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC:
495 return true;
496 }
497 return false;
498}
499
500static inline bool isOptNone(const MachineFunction &MF) {
501 return MF.getFunction().hasOptNone() ||
503}
504
505static inline bool isOptSize(const MachineFunction &MF) {
506 const Function &F = MF.getFunction();
507 return F.hasOptSize() && !F.hasMinSize();
508}
509
510static inline bool isMinSize(const MachineFunction &MF) {
511 return MF.getFunction().hasMinSize();
512}
513
514/// Implements shrink-wrapping of the stack frame. By default, stack frame
515/// is created in the function entry block, and is cleaned up in every block
516/// that returns. This function finds alternate blocks: one for the frame
517/// setup (prolog) and one for the cleanup (epilog).
518void HexagonFrameLowering::findShrunkPrologEpilog(MachineFunction &MF,
519 MachineBasicBlock *&PrologB, MachineBasicBlock *&EpilogB) const {
520 static unsigned ShrinkCounter = 0;
521
522 if (MF.getSubtarget<HexagonSubtarget>().isEnvironmentMusl() &&
523 MF.getFunction().isVarArg())
524 return;
525 // The caller's VSP is saved through R7, which is only known to be free
526 // in the entry block.
527 if (MF.getSubtarget<HexagonSubtarget>().useHLXOps())
528 return;
529 if (ShrinkLimit.getPosition()) {
530 if (ShrinkCounter >= ShrinkLimit)
531 return;
532 ShrinkCounter++;
533 }
534
535 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
536
537 MachineDominatorTree MDT;
538 MDT.recalculate(MF);
539 MachinePostDominatorTree MPT;
540 MPT.recalculate(MF);
541
542 using UnsignedMap = DenseMap<unsigned, unsigned>;
543 using RPOTType = ReversePostOrderTraversal<const MachineFunction *>;
544
545 UnsignedMap RPO;
546 RPOTType RPOT(&MF);
547 unsigned RPON = 0;
548 for (auto &I : RPOT)
549 RPO[I->getNumber()] = RPON++;
550
551 // Don't process functions that have loops, at least for now. Placement
552 // of prolog and epilog must take loop structure into account. For simpli-
553 // city don't do it right now.
554 for (auto &I : MF) {
555 unsigned BN = RPO[I.getNumber()];
556 for (MachineBasicBlock *Succ : I.successors())
557 // If found a back-edge, return.
558 if (RPO[Succ->getNumber()] <= BN)
559 return;
560 }
561
562 // Collect the set of blocks that need a stack frame to execute. Scan
563 // each block for uses/defs of callee-saved registers, calls, etc.
565 BitVector CSR(Hexagon::NUM_TARGET_REGS);
566 for (const MCPhysReg *P = HRI.getCalleeSavedRegs(&MF); *P; ++P)
567 for (MCPhysReg S : HRI.subregs_inclusive(*P))
568 CSR[S] = true;
569
570 for (auto &I : MF)
571 if (needsStackFrame(I, CSR, HRI))
572 SFBlocks.push_back(&I);
573
574 LLVM_DEBUG({
575 dbgs() << "Blocks needing SF: {";
576 for (auto &B : SFBlocks)
577 dbgs() << " " << printMBBReference(*B);
578 dbgs() << " }\n";
579 });
580 // No frame needed?
581 if (SFBlocks.empty())
582 return;
583
584 // Pick a common dominator and a common post-dominator.
585 MachineBasicBlock *DomB = SFBlocks[0];
586 for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) {
587 DomB = MDT.findNearestCommonDominator(DomB, SFBlocks[i]);
588 if (!DomB)
589 break;
590 }
591 MachineBasicBlock *PDomB = SFBlocks[0];
592 for (unsigned i = 1, n = SFBlocks.size(); i < n; ++i) {
593 PDomB = MPT.findNearestCommonDominator(PDomB, SFBlocks[i]);
594 if (!PDomB)
595 break;
596 }
597 LLVM_DEBUG({
598 dbgs() << "Computed dom block: ";
599 if (DomB)
600 dbgs() << printMBBReference(*DomB);
601 else
602 dbgs() << "<null>";
603 dbgs() << ", computed pdom block: ";
604 if (PDomB)
605 dbgs() << printMBBReference(*PDomB);
606 else
607 dbgs() << "<null>";
608 dbgs() << "\n";
609 });
610 if (!DomB || !PDomB)
611 return;
612
613 // Make sure that DomB dominates PDomB and PDomB post-dominates DomB.
614 if (!MDT.dominates(DomB, PDomB)) {
615 LLVM_DEBUG(dbgs() << "Dom block does not dominate pdom block\n");
616 return;
617 }
618 if (!MPT.dominates(PDomB, DomB)) {
619 LLVM_DEBUG(dbgs() << "PDom block does not post-dominate dom block\n");
620 return;
621 }
622
623 // Finally, everything seems right.
624 PrologB = DomB;
625 EpilogB = PDomB;
626}
627
628/// Perform most of the PEI work here:
629/// - saving/restoring of the callee-saved registers,
630/// - stack frame creation and destruction.
631/// Normally, this work is distributed among various functions, but doing it
632/// in one place allows shrink-wrapping of the stack frame.
634 MachineBasicBlock &MBB) const {
635 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
636
637 MachineFrameInfo &MFI = MF.getFrameInfo();
638 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
639
640 MachineBasicBlock *PrologB = &MF.front(), *EpilogB = nullptr;
642 findShrunkPrologEpilog(MF, PrologB, EpilogB);
643
644 bool PrologueStubs = false;
646 insertCSRSpillsInBlock(*PrologB, CSI, HRI, PrologueStubs);
647 // Insert PS_aligna after all CSR spills.
648 // PS_aligna initializes the AP register with an aligned
649 // value derived from FP. Since AP is a callee-saved register, its original
650 // value must be saved before it is overwritten, and it must be defined
651 // before any AP-relative stack accesses.
652 insertAlignaInBlock(*PrologB, AfterCSR);
653 insertPrologueInBlock(*PrologB, PrologueStubs);
654 // Insert the SCS prologue after all FrameSetup instructions so that it
655 // follows allocframe and any CSR spills in the instruction stream. The
656 // packetizer may still fuse the SCS store with the first call in the
657 // function, but because Hexagon packets use old-value reads the original
658 // R31 is always what is stored.
659 {
660 MachineBasicBlock::iterator AfterProlog = PrologB->begin();
661 while (AfterProlog != PrologB->end() &&
662 AfterProlog->getFlag(MachineInstr::FrameSetup))
663 ++AfterProlog;
664 DebugLoc PrologDL = PrologB->findDebugLoc(AfterProlog);
665 emitSCSPrologue(MF, *PrologB, AfterProlog, PrologDL);
666 }
667 updateEntryPaths(MF, *PrologB);
668
669 if (EpilogB) {
670 insertCSRRestoresInBlock(*EpilogB, CSI, HRI);
671 insertEpilogueInBlock(*EpilogB);
672 } else {
673 for (auto &B : MF)
674 if (B.isReturnBlock())
675 insertCSRRestoresInBlock(B, CSI, HRI);
676
677 for (auto &B : MF)
678 if (B.isReturnBlock())
679 insertEpilogueInBlock(B);
680
681 for (auto &B : MF) {
682 if (B.empty())
683 continue;
684 MachineInstr *RetI = getReturn(B);
685 if (!RetI || isRestoreCall(RetI->getOpcode()))
686 continue;
687 for (auto &R : CSI)
688 RetI->addOperand(MachineOperand::CreateReg(R.getReg(), false, true));
689 }
690 }
691
692 if (EpilogB) {
693 // If there is an epilog block, it may not have a return instruction.
694 // In such case, we need to add the callee-saved registers as live-ins
695 // in all blocks on all paths from the epilog to any return block.
696 unsigned MaxBN = MF.getNumBlockIDs();
697 BitVector DoneT(MaxBN+1), DoneF(MaxBN+1), Path(MaxBN+1);
698 updateExitPaths(*EpilogB, *EpilogB, DoneT, DoneF, Path);
699 }
700}
701
702/// Returns true if the target can safely skip saving callee-saved registers
703/// for noreturn nounwind functions.
705 const MachineFunction &MF) const {
706 const auto &F = MF.getFunction();
707 assert(F.hasFnAttribute(Attribute::NoReturn) &&
708 F.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
709 !F.getFunction().hasFnAttribute(Attribute::UWTable));
710 (void)F;
711
712 // No need to save callee saved registers if the function does not return.
713 return MF.getSubtarget<HexagonSubtarget>().noreturnStackElim();
714}
715
716// Helper function used to determine when to eliminate the stack frame for
717// functions marked as noreturn and when the noreturn-stack-elim options are
718// specified. When both these conditions are true, then a FP may not be needed
719// if the function makes a call. It is very similar to enableCalleeSaveSkip,
720// but it used to check if the allocframe can be eliminated as well.
721static bool enableAllocFrameElim(const MachineFunction &MF) {
722 const auto &F = MF.getFunction();
723 const auto &MFI = MF.getFrameInfo();
724 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
725 assert(!MFI.hasVarSizedObjects() &&
726 !HST.getRegisterInfo()->hasStackRealignment(MF));
727 return F.hasFnAttribute(Attribute::NoReturn) &&
728 F.hasFnAttribute(Attribute::NoUnwind) &&
729 !F.hasFnAttribute(Attribute::UWTable) && HST.noreturnStackElim() &&
730 MFI.getStackSize() == 0;
731}
732
733void HexagonFrameLowering::insertPrologueInBlock(MachineBasicBlock &MBB,
734 bool PrologueStubs) const {
736 MachineFrameInfo &MFI = MF.getFrameInfo();
737 auto &HST = MF.getSubtarget<HexagonSubtarget>();
738 auto &HII = *HST.getInstrInfo();
739 auto &HRI = *HST.getRegisterInfo();
740 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
741
742 Align MaxAlign = std::max(getMaxStackAlign(MF), getStackAlign());
743 uint64_t VTCMFrameSize = HMFI.getVTCMFrameInfo().getStackSize();
744
745 // Calculate the total stack frame size.
746 // Get the number of bytes to allocate from the FrameInfo.
747 unsigned FrameSize = MFI.getStackSize();
748 // Round up the max call frame size to the max alignment on the stack.
749 unsigned MaxCFA = alignTo(MFI.getMaxCallFrameSize(), MaxAlign);
750 MFI.setMaxCallFrameSize(MaxCFA);
751
752 FrameSize = MaxCFA + alignTo(FrameSize, MaxAlign);
753 MFI.setStackSize(FrameSize);
754
755 bool AlignStack = (MaxAlign > getStackAlign());
756
757 // Get the number of bytes to allocate from the FrameInfo.
758 unsigned NumBytes = MFI.getStackSize();
759 Register SP = HRI.getStackRegister();
760 unsigned MaxCF = MFI.getMaxCallFrameSize();
762
763 SmallVector<MachineInstr *, 4> AdjustRegs;
764 for (auto &MBB : MF)
765 for (auto &MI : MBB)
766 if (MI.getOpcode() == Hexagon::PS_alloca)
767 AdjustRegs.push_back(&MI);
768
769 for (auto *MI : AdjustRegs) {
770 assert((MI->getOpcode() == Hexagon::PS_alloca) && "Expected alloca");
771 expandAlloca(MI, MF, HII, SP, MaxCF);
772 MI->eraseFromParent();
773 }
774
775 DebugLoc dl = MBB.findDebugLoc(InsertPt);
776
777 if (MF.getFunction().isVarArg() &&
778 MF.getSubtarget<HexagonSubtarget>().isEnvironmentMusl()) {
779 // Calculate the size of register saved area.
780 int NumVarArgRegs = 6 - FirstVarArgSavedReg;
781 int RegisterSavedAreaSizePlusPadding = (NumVarArgRegs % 2 == 0)
782 ? NumVarArgRegs * 4
783 : NumVarArgRegs * 4 + 4;
784 if (RegisterSavedAreaSizePlusPadding > 0) {
785 // Decrement the stack pointer by size of register saved area plus
786 // padding if any.
787 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP)
788 .addReg(SP)
789 .addImm(-RegisterSavedAreaSizePlusPadding)
791
792 int NumBytes = 0;
793 // Copy all the named arguments below register saved area.
794 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
795 for (int i = HMFI.getFirstNamedArgFrameIndex(),
796 e = HMFI.getLastNamedArgFrameIndex(); i >= e; --i) {
797 // HLX arguments are on the VTCM stack.
799 continue;
800 uint64_t ObjSize = MFI.getObjectSize(i);
801 Align ObjAlign = MFI.getObjectAlign(i);
802
803 // Determine the kind of load/store that should be used.
804 unsigned LDOpc, STOpc;
805 uint64_t OpcodeChecker = ObjAlign.value();
806
807 // Handle cases where alignment of an object is > its size.
808 if (ObjAlign > ObjSize) {
809 if (ObjSize <= 1)
810 OpcodeChecker = 1;
811 else if (ObjSize <= 2)
812 OpcodeChecker = 2;
813 else if (ObjSize <= 4)
814 OpcodeChecker = 4;
815 else if (ObjSize > 4)
816 OpcodeChecker = 8;
817 }
818
819 switch (OpcodeChecker) {
820 case 1:
821 LDOpc = Hexagon::L2_loadrb_io;
822 STOpc = Hexagon::S2_storerb_io;
823 break;
824 case 2:
825 LDOpc = Hexagon::L2_loadrh_io;
826 STOpc = Hexagon::S2_storerh_io;
827 break;
828 case 4:
829 LDOpc = Hexagon::L2_loadri_io;
830 STOpc = Hexagon::S2_storeri_io;
831 break;
832 case 8:
833 default:
834 LDOpc = Hexagon::L2_loadrd_io;
835 STOpc = Hexagon::S2_storerd_io;
836 break;
837 }
838
839 Register RegUsed = LDOpc == Hexagon::L2_loadrd_io ? Hexagon::D3
840 : Hexagon::R6;
841 int LoadStoreCount = ObjSize / OpcodeChecker;
842
843 if (ObjSize % OpcodeChecker)
844 ++LoadStoreCount;
845
846 // Get the start location of the load. NumBytes is basically the
847 // offset from the stack pointer of previous function, which would be
848 // the caller in this case, as this function has variable argument
849 // list.
850 if (NumBytes != 0)
851 NumBytes = alignTo(NumBytes, ObjAlign);
852
853 int Count = 0;
854 while (Count < LoadStoreCount) {
855 // Load the value of the named argument on stack.
856 BuildMI(MBB, InsertPt, dl, HII.get(LDOpc), RegUsed)
857 .addReg(SP)
858 .addImm(RegisterSavedAreaSizePlusPadding +
859 ObjAlign.value() * Count + NumBytes)
861
862 // Store it below the register saved area plus padding.
863 BuildMI(MBB, InsertPt, dl, HII.get(STOpc))
864 .addReg(SP)
865 .addImm(ObjAlign.value() * Count + NumBytes)
866 .addReg(RegUsed)
868
869 Count++;
870 }
871 NumBytes += MFI.getObjectSize(i);
872 }
873
874 // Make NumBytes 8 byte aligned
875 NumBytes = alignTo(NumBytes, 8);
876
877 // If the number of registers having variable arguments is odd,
878 // leave 4 bytes of padding to get to the location where first
879 // variable argument which was passed through register was copied.
880 NumBytes = (NumVarArgRegs % 2 == 0) ? NumBytes : NumBytes + 4;
881
882 for (int j = FirstVarArgSavedReg, i = 0; j < 6; ++j, ++i) {
883 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_storeri_io))
884 .addReg(SP)
885 .addImm(NumBytes + 4 * i)
886 .addReg(Hexagon::R0 + j)
888 }
889 }
890 }
891
892 if (hasFP(MF)) {
893 insertAllocframe(MBB, InsertPt, NumBytes);
894 if (AlignStack) {
895 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_andir), SP)
896 .addReg(SP)
897 .addImm(-int64_t(MaxAlign.value()));
898 }
899 // If the stack-checking is enabled, and we spilled the callee-saved
900 // registers inline (i.e. did not use a spill function), then call
901 // the stack checker directly.
902 if (EnableStackOVFSanitizer && !PrologueStubs)
903 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::PS_call_stk))
904 .addExternalSymbol("__runtime_stack_check");
905 } else if (NumBytes > 0) {
906 assert(alignTo(NumBytes, 8) == NumBytes);
907 auto *TLI = HST.getTargetLowering();
908 bool NeedsProbing = TLI->hasInlineStackProbe(MF);
909 unsigned ProbeSize = 0;
910 if (NeedsProbing) {
911 Align StackAlign = getStackAlign();
912 ProbeSize = TLI->getStackProbeSize(MF, StackAlign);
913 }
914 if (NeedsProbing && NumBytes > ProbeSize) {
915 // Compute target SP in R28 (caller-saved scratch).
916 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), Hexagon::R28)
917 .addReg(SP)
918 .addImm(-int(NumBytes))
920 // Emit pseudo to be expanded by inlineStackProbe().
921 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::PS_probed_stackalloc))
922 .addReg(Hexagon::R28)
924 } else {
925 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP)
926 .addReg(SP)
927 .addImm(-int(NumBytes))
929 }
930 }
931 if (!VTCMFrameSize)
932 return;
933 if (int VSPFI = HMFI.getVSPFrameIndex(); VSPFI >= 0) {
934 // Save the caller's VSP before allocating the vector frame. R7 is not
935 // an argument register, and the prologue is in the entry block (see
936 // findShrunkPrologEpilog).
937 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_tfrcrr), Hexagon::R7)
938 .addReg(Hexagon::VSP)
940 HII.storeRegToStackSlot(MBB, InsertPt, Hexagon::R7, /*isKill=*/true, VSPFI,
941 &Hexagon::IntRegsRegClass, Register());
942 std::prev(InsertPt)->setFlag(MachineInstr::FrameSetup);
943 }
944 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::V2_vallocframe_imm))
945 .addImm(VTCMFrameSize / 2048)
947}
948
949void HexagonFrameLowering::insertEpilogueInBlock(MachineBasicBlock &MBB) const {
951 auto &HST = MF.getSubtarget<HexagonSubtarget>();
952 auto &HII = *HST.getInstrInfo();
953 auto &HRI = *HST.getRegisterInfo();
954 Register SP = HRI.getStackRegister();
955
957 DebugLoc dl = MBB.findDebugLoc(InsertPt);
958
959 const auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
960 uint64_t VTCMFrameSize = HMFI.getVTCMFrameInfo().getStackSize();
961 if (VTCMFrameSize)
962 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::V2_vdeallocframe_imm))
963 .addImm(VTCMFrameSize / 2048);
964
965 if (!hasFP(MF)) {
966 MachineFrameInfo &MFI = MF.getFrameInfo();
967 unsigned NumBytes = MFI.getStackSize();
968 if (MF.getFunction().isVarArg() &&
969 MF.getSubtarget<HexagonSubtarget>().isEnvironmentMusl()) {
970 // On Hexagon Linux, deallocate the stack for the register saved area.
971 int NumVarArgRegs = 6 - FirstVarArgSavedReg;
972 int RegisterSavedAreaSizePlusPadding = (NumVarArgRegs % 2 == 0) ?
973 (NumVarArgRegs * 4) : (NumVarArgRegs * 4 + 4);
974 NumBytes += RegisterSavedAreaSizePlusPadding;
975 }
976 if (NumBytes) {
977 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP)
978 .addReg(SP)
979 .addImm(NumBytes);
980 }
981 return;
982 }
983
984 MachineInstr *RetI = getReturn(MBB);
985 unsigned RetOpc = RetI ? RetI->getOpcode() : 0;
986
987 // Handle EH_RETURN.
988 if (RetOpc == Hexagon::EH_RETURN_JMPR) {
989 // EH paths overwrite R31 with a handler address; the shadow stack is
990 // not read on this path, so no SCS epilogue is needed.
991 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::L2_deallocframe))
992 .addDef(Hexagon::D15)
993 .addReg(Hexagon::R30);
994 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_add), SP)
995 .addReg(SP)
996 .addReg(Hexagon::R28);
997 return;
998 }
999
1000 // Check for RESTORE_DEALLOC_RET* tail call. Don't emit an extra dealloc-
1001 // frame instruction if we encounter it.
1002 // These are restore stubs, which useRestoreFunction() never selects when SCS
1003 // is active (they do deallocframe+jumpr, bypassing the SCS epilogue), so no
1004 // SCS epilogue is needed here.
1005 if (RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4 ||
1006 RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC ||
1007 RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT ||
1008 RetOpc == Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT_PIC) {
1010 ++It;
1011 // Delete all instructions after the RESTORE (except labels).
1012 while (It != MBB.end()) {
1013 if (!It->isLabel())
1014 It = MBB.erase(It);
1015 else
1016 ++It;
1017 }
1018 return;
1019 }
1020
1021 // It is possible that the restoring code is a call to a library function.
1022 // All of the restore* functions include "deallocframe", so we need to make
1023 // sure that we don't add an extra one.
1024 bool NeedsSCS = MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
1025 bool NeedsDeallocframe = true;
1026 unsigned PrevOpc = 0;
1027 if (!MBB.empty() && InsertPt != MBB.begin()) {
1028 MachineBasicBlock::iterator PrevIt = std::prev(InsertPt);
1029 PrevOpc = PrevIt->getOpcode();
1030 if (PrevOpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4 ||
1031 PrevOpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC ||
1032 PrevOpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT ||
1033 PrevOpc == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC ||
1034 PrevOpc == Hexagon::PS_call_nr || PrevOpc == Hexagon::PS_callr_nr)
1035 NeedsDeallocframe = false;
1036 }
1037
1038 if (!MF.getSubtarget<HexagonSubtarget>().isEnvironmentMusl() ||
1039 !MF.getFunction().isVarArg()) {
1040 if (!NeedsDeallocframe) {
1041 // RESTORE_DEALLOC_BEFORE_TAILCALL is a restore stub, which
1042 // useRestoreFunction() never selects when SCS is active.
1043 // PS_call_nr/PS_callr_nr are noreturn calls so the shadow stack entry
1044 // is never read - no SCS epilogue is needed on either path.
1045 if (NeedsSCS && PrevOpc != Hexagon::PS_call_nr &&
1046 PrevOpc != Hexagon::PS_callr_nr)
1047 report_fatal_error("SCS with RESTORE_DEALLOC stub: "
1048 "useRestoreFunction() should have prevented this");
1049 return;
1050 }
1051 // If the returning instruction is PS_jmpret, replace it with
1052 // dealloc_return, otherwise just add deallocframe. The function
1053 // could be returning via a tail call.
1054 if (RetOpc != Hexagon::PS_jmpret || DisableDeallocRet || NeedsSCS) {
1055 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::L2_deallocframe))
1056 .addDef(Hexagon::D15)
1057 .addReg(Hexagon::R30);
1058 // When shadow call stack is active, overwrite R31 restored by
1059 // deallocframe with the shadow-stack copy, then retract the pointer.
1060 if (NeedsSCS)
1061 emitSCSEpilogue(MF, MBB, InsertPt, dl);
1062 return;
1063 }
1064 unsigned NewOpc = Hexagon::L4_return;
1065 MachineInstrBuilder NewI = BuildMI(MBB, RetI, dl, HII.get(NewOpc))
1066 .addDef(Hexagon::D15)
1067 .addReg(Hexagon::R30);
1068 // Avoid duplicating the pc implicit-def.
1069 for (const MachineOperand &MO : drop_begin(RetI->implicit_operands()))
1070 NewI.add(MO);
1071 MBB.erase(RetI);
1072 } else {
1073 // L2_deallocframe instruction after it.
1074 // Calculate the size of register saved area.
1075 int NumVarArgRegs = 6 - FirstVarArgSavedReg;
1076 int RegisterSavedAreaSizePlusPadding = (NumVarArgRegs % 2 == 0) ?
1077 (NumVarArgRegs * 4) : (NumVarArgRegs * 4 + 4);
1078
1081 : std::prev(Term);
1082 bool HasRestoreStub =
1083 I != MBB.end() &&
1084 (I->getOpcode() == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT ||
1085 I->getOpcode() ==
1086 Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC ||
1087 I->getOpcode() == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4 ||
1088 I->getOpcode() == Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC);
1089 if (!HasRestoreStub)
1090 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::L2_deallocframe))
1091 .addDef(Hexagon::D15)
1092 .addReg(Hexagon::R30);
1093 if (RegisterSavedAreaSizePlusPadding != 0)
1094 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP)
1095 .addReg(SP)
1096 .addImm(RegisterSavedAreaSizePlusPadding);
1097 // RESTORE_DEALLOC stubs are never selected when SCS is active (see
1098 // useRestoreFunction()), so only emit the SCS epilogue when we emitted
1099 // our own deallocframe above.
1100 if (NeedsSCS && !HasRestoreStub)
1101 emitSCSEpilogue(MF, MBB, InsertPt, dl);
1102 }
1103}
1104
1105void HexagonFrameLowering::insertAllocframe(MachineBasicBlock &MBB,
1106 MachineBasicBlock::iterator InsertPt, unsigned NumBytes) const {
1107 MachineFunction &MF = *MBB.getParent();
1108 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1109 auto &HII = *HST.getInstrInfo();
1110 auto &HRI = *HST.getRegisterInfo();
1111
1112 // Check for overflow.
1113 // Hexagon_TODO: Ugh! hardcoding. Is there an API that can be used?
1114 const unsigned int ALLOCFRAME_MAX = 16384;
1115
1116 // Create a dummy memory operand to avoid allocframe from being treated as
1117 // a volatile memory reference.
1120
1121 DebugLoc dl = MBB.findDebugLoc(InsertPt);
1122 Register SP = HRI.getStackRegister();
1123
1124 auto *TLI = HST.getTargetLowering();
1125 bool NeedsProbing = TLI->hasInlineStackProbe(MF) && NumBytes > 0;
1126 unsigned ProbeSize = 0;
1127 if (NeedsProbing) {
1128 Align StackAlign = getStackAlign();
1129 ProbeSize = TLI->getStackProbeSize(MF, StackAlign);
1130 }
1131
1132 if (NeedsProbing && NumBytes > ProbeSize) {
1133 // Emit allocframe(#0) to save FP/LR only.
1134 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe))
1135 .addDef(SP)
1136 .addReg(SP)
1137 .addImm(0)
1138 .addMemOperand(MMO)
1140
1141 // Compute target SP in R28 (caller-saved scratch).
1142 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), Hexagon::R28)
1143 .addReg(SP)
1144 .addImm(-int(NumBytes))
1146
1147 // Emit pseudo to be expanded by inlineStackProbe().
1148 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::PS_probed_stackalloc))
1149 .addReg(Hexagon::R28)
1151 } else if (NumBytes >= ALLOCFRAME_MAX) {
1152 // Emit allocframe(#0).
1153 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe))
1154 .addDef(SP)
1155 .addReg(SP)
1156 .addImm(0)
1157 .addMemOperand(MMO)
1159
1160 // Subtract the size from the stack pointer.
1161 Register SP = HRI.getStackRegister();
1162 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::A2_addi), SP)
1163 .addReg(SP)
1164 .addImm(-int(NumBytes))
1166 } else {
1167 BuildMI(MBB, InsertPt, dl, HII.get(Hexagon::S2_allocframe))
1168 .addDef(SP)
1169 .addReg(SP)
1170 .addImm(NumBytes)
1171 .addMemOperand(MMO)
1173 }
1174}
1175
1177 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
1178 // Collect PS_probed_stackalloc pseudos to expand. Collecting first avoids
1179 // issues with modifying the block while iterating.
1181 for (MachineInstr &MI : PrologueMBB)
1182 if (MI.getOpcode() == Hexagon::PS_probed_stackalloc)
1183 ToReplace.push_back(&MI);
1184
1185 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1186 auto &HII = *HST.getInstrInfo();
1187 auto *TLI = HST.getTargetLowering();
1188 Align StackAlign = getStackAlign();
1189 unsigned ProbeSize = TLI->getStackProbeSize(MF, StackAlign);
1191
1192 for (MachineInstr *MI : ToReplace) {
1193 MachineBasicBlock::iterator MBBI = MI->getIterator();
1194 DebugLoc DL = PrologueMBB.findDebugLoc(MBBI);
1195 Register TargetReg = MI->getOperand(0).getReg();
1196
1197 // Split the block: everything after the pseudo goes into ExitMBB.
1198 MachineBasicBlock *MBB = MI->getParent();
1199 MachineFunction::iterator InsertPt = std::next(MBB->getIterator());
1200 MachineBasicBlock *LoopMBB =
1201 MF.CreateMachineBasicBlock(MBB->getBasicBlock());
1202 MF.insert(InsertPt, LoopMBB);
1203 MachineBasicBlock *ExitMBB =
1204 MF.CreateMachineBasicBlock(MBB->getBasicBlock());
1205 MF.insert(InsertPt, ExitMBB);
1206
1207 // Move everything after the pseudo into ExitMBB.
1208 ExitMBB->splice(ExitMBB->end(), MBB, std::next(MBBI), MBB->end());
1210
1211 // LoopMBB: probe each page by decrementing SP and storing zero.
1212 // When NumBytes is not an exact multiple of ProbeSize the loop
1213 // will overshoot by up to ProbeSize-1 bytes; the final r29 = r28
1214 // in ExitMBB corrects SP to the true target.
1215 //
1216 // The store is placed before the compare+branch so that the
1217 // packetizer can bundle them into a single VLIW packet. All
1218 // non-predicated instructions in a packet commit unconditionally,
1219 // so the probe store executes on every iteration including the
1220 // last (when the branch falls through).
1221 //
1222 // r29 = add(r29, #-ProbeSize)
1223 // memw(r29+#0) = #0
1224 // p0 = cmp.gtu(r29, r28)
1225 // if (p0) jump LoopMBB
1226 BuildMI(*LoopMBB, LoopMBB->end(), DL, HII.get(Hexagon::A2_addi),
1227 Hexagon::R29)
1228 .addReg(Hexagon::R29)
1229 .addImm(-int(ProbeSize))
1230 .setMIFlags(Flags);
1231
1232 BuildMI(*LoopMBB, LoopMBB->end(), DL, HII.get(Hexagon::S4_storeiri_io))
1233 .addReg(Hexagon::R29)
1234 .addImm(0)
1235 .addImm(0)
1236 .setMIFlags(Flags);
1237
1238 BuildMI(*LoopMBB, LoopMBB->end(), DL, HII.get(Hexagon::C2_cmpgtu),
1239 Hexagon::P0)
1240 .addReg(Hexagon::R29)
1241 .addReg(TargetReg)
1242 .setMIFlags(Flags);
1243
1244 BuildMI(*LoopMBB, LoopMBB->end(), DL, HII.get(Hexagon::J2_jumpt))
1245 .addReg(Hexagon::P0)
1246 .addMBB(LoopMBB)
1247 .setMIFlags(Flags);
1248
1249 // ExitMBB: set final SP.
1250 BuildMI(*ExitMBB, ExitMBB->begin(), DL, HII.get(Hexagon::A2_tfr),
1251 Hexagon::R29)
1252 .addReg(TargetReg)
1253 .setMIFlags(Flags);
1254
1255 // Set up CFG edges.
1256 MBB->addSuccessor(LoopMBB);
1257 LoopMBB->addSuccessor(LoopMBB);
1258 LoopMBB->addSuccessor(ExitMBB);
1259
1260 // Remove the pseudo.
1261 MI->eraseFromParent();
1262
1263 // Recompute live-ins for the new blocks.
1264 fullyRecomputeLiveIns({ExitMBB, LoopMBB});
1265 }
1266}
1267
1268void HexagonFrameLowering::insertAlignaInBlock(
1270 MachineFunction &MF = *MBB.getParent();
1271 Register AP =
1272 MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
1273 if (!AP.isValid())
1274 return;
1275
1276 assert(needsAligna(MF) && "Unexpected stack align base register");
1277
1278 auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
1279 Align MaxAlign = std::max(MF.getFrameInfo().getMaxAlign(), getStackAlign());
1280 DebugLoc DL = MBB.findDebugLoc(InsertPt);
1281 BuildMI(MBB, InsertPt, DL, HII.get(Hexagon::PS_aligna), AP)
1282 .addImm(MaxAlign.value());
1283}
1284
1285void HexagonFrameLowering::updateEntryPaths(MachineFunction &MF,
1286 MachineBasicBlock &SaveB) const {
1287 SetVector<unsigned> Worklist;
1288
1289 MachineBasicBlock &EntryB = MF.front();
1290 Worklist.insert(EntryB.getNumber());
1291
1292 unsigned SaveN = SaveB.getNumber();
1293 auto &CSI = MF.getFrameInfo().getCalleeSavedInfo();
1294
1295 for (unsigned i = 0; i < Worklist.size(); ++i) {
1296 unsigned BN = Worklist[i];
1298 for (auto &R : CSI)
1299 if (!MBB.isLiveIn(R.getReg()))
1300 MBB.addLiveIn(R.getReg());
1301 if (BN != SaveN)
1302 for (auto &SB : MBB.successors())
1303 Worklist.insert(SB->getNumber());
1304 }
1305}
1306
1307bool HexagonFrameLowering::updateExitPaths(MachineBasicBlock &MBB,
1308 MachineBasicBlock &RestoreB, BitVector &DoneT, BitVector &DoneF,
1309 BitVector &Path) const {
1310 assert(MBB.getNumber() >= 0);
1311 unsigned BN = MBB.getNumber();
1312 if (Path[BN] || DoneF[BN])
1313 return false;
1314 if (DoneT[BN])
1315 return true;
1316
1317 auto &CSI = MBB.getParent()->getFrameInfo().getCalleeSavedInfo();
1318
1319 Path[BN] = true;
1320 bool ReachedExit = false;
1321 for (auto &SB : MBB.successors())
1322 ReachedExit |= updateExitPaths(*SB, RestoreB, DoneT, DoneF, Path);
1323
1324 if (!MBB.empty() && MBB.back().isReturn()) {
1325 // Add implicit uses of all callee-saved registers to the reached
1326 // return instructions. This is to prevent the anti-dependency breaker
1327 // from renaming these registers.
1328 MachineInstr &RetI = MBB.back();
1329 if (!isRestoreCall(RetI.getOpcode()))
1330 for (auto &R : CSI)
1331 RetI.addOperand(MachineOperand::CreateReg(R.getReg(), false, true));
1332 ReachedExit = true;
1333 }
1334
1335 // We don't want to add unnecessary live-ins to the restore block: since
1336 // the callee-saved registers are being defined in it, the entry of the
1337 // restore block cannot be on the path from the definitions to any exit.
1338 if (ReachedExit && &MBB != &RestoreB) {
1339 for (auto &R : CSI)
1340 if (!MBB.isLiveIn(R.getReg()))
1341 MBB.addLiveIn(R.getReg());
1342 DoneT[BN] = true;
1343 }
1344 if (!ReachedExit)
1345 DoneF[BN] = true;
1346
1347 Path[BN] = false;
1348 return ReachedExit;
1349}
1350
1351static std::optional<MachineBasicBlock::iterator>
1353 // The CFI instructions need to be inserted right after allocframe.
1354 // An exception to this is a situation where allocframe is bundled
1355 // with a call: then the CFI instructions need to be inserted before
1356 // the packet with the allocframe+call (in case the call throws an
1357 // exception).
1358 auto End = B.instr_end();
1359
1360 for (MachineInstr &I : B) {
1361 MachineBasicBlock::iterator It = I.getIterator();
1362 if (!I.isBundle()) {
1363 if (I.getOpcode() == Hexagon::S2_allocframe)
1364 return std::next(It);
1365 continue;
1366 }
1367 // I is a bundle.
1368 bool HasCall = false, HasAllocFrame = false;
1369 auto T = It.getInstrIterator();
1370 while (++T != End && T->isBundled()) {
1371 if (T->getOpcode() == Hexagon::S2_allocframe)
1372 HasAllocFrame = true;
1373 else if (T->isCall())
1374 HasCall = true;
1375 }
1376 if (HasAllocFrame)
1377 return HasCall ? It : std::next(It);
1378 }
1379 return std::nullopt;
1380}
1381
1383 for (auto &B : MF)
1384 if (auto At = findCFILocation(B))
1385 insertCFIInstructionsAt(B, *At);
1386}
1387
1388void HexagonFrameLowering::insertCFIInstructionsAt(MachineBasicBlock &MBB,
1389 MachineBasicBlock::iterator At) const {
1390 MachineFunction &MF = *MBB.getParent();
1391 MachineFrameInfo &MFI = MF.getFrameInfo();
1392 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1393 auto &HII = *HST.getInstrInfo();
1394 auto &HRI = *HST.getRegisterInfo();
1395
1396 // If CFI instructions have debug information attached, something goes
1397 // wrong with the final assembly generation: the prolog_end is placed
1398 // in a wrong location.
1399 DebugLoc DL;
1400 const MCInstrDesc &CFID = HII.get(TargetOpcode::CFI_INSTRUCTION);
1401
1402 MCSymbol *FrameLabel = MF.getContext().createTempSymbol();
1403 bool HasFP = hasFP(MF);
1404
1405 if (HasFP) {
1406 unsigned DwFPReg = HRI.getDwarfRegNum(HRI.getFrameRegister(), true);
1407 unsigned DwRAReg = HRI.getDwarfRegNum(HRI.getRARegister(), true);
1408
1409 // Define CFA via an offset from the value of FP.
1410 //
1411 // -8 -4 0 (SP)
1412 // --+----+----+---------------------
1413 // | FP | LR | increasing addresses -->
1414 // --+----+----+---------------------
1415 // | +-- Old SP (before allocframe)
1416 // +-- New FP (after allocframe)
1417 //
1418 // MCCFIInstruction::cfiDefCfa adds the offset from the register.
1419 // MCCFIInstruction::createOffset takes the offset without sign change.
1420 auto DefCfa = MCCFIInstruction::cfiDefCfa(FrameLabel, DwFPReg, 8);
1421 BuildMI(MBB, At, DL, CFID)
1422 .addCFIIndex(MF.addFrameInst(DefCfa));
1423 // R31 (return addr) = CFA - 4
1424 auto OffR31 = MCCFIInstruction::createOffset(FrameLabel, DwRAReg, -4);
1425 BuildMI(MBB, At, DL, CFID)
1426 .addCFIIndex(MF.addFrameInst(OffR31));
1427 // R30 (frame ptr) = CFA - 8
1428 auto OffR30 = MCCFIInstruction::createOffset(FrameLabel, DwFPReg, -8);
1429 BuildMI(MBB, At, DL, CFID)
1430 .addCFIIndex(MF.addFrameInst(OffR30));
1431 }
1432
1433 static const MCPhysReg RegsToMove[] = {
1434 Hexagon::R1, Hexagon::R0, Hexagon::R3, Hexagon::R2,
1435 Hexagon::R17, Hexagon::R16, Hexagon::R19, Hexagon::R18,
1436 Hexagon::R21, Hexagon::R20, Hexagon::R23, Hexagon::R22,
1437 Hexagon::R25, Hexagon::R24, Hexagon::R27, Hexagon::R26,
1438 Hexagon::D0, Hexagon::D1, Hexagon::D8, Hexagon::D9,
1439 Hexagon::D10, Hexagon::D11, Hexagon::D12, Hexagon::D13
1440 };
1441
1442 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1443
1444 for (MCPhysReg Reg : RegsToMove) {
1445 auto IfR = [Reg] (const CalleeSavedInfo &C) -> bool {
1446 return C.getReg() == Reg;
1447 };
1448 auto F = find_if(CSI, IfR);
1449 if (F == CSI.end())
1450 continue;
1451
1452 int64_t Offset;
1453 if (HasFP) {
1454 // If the function has a frame pointer (i.e. has an allocframe),
1455 // then the CFA has been defined in terms of FP. Any offsets in
1456 // the following CFI instructions have to be defined relative
1457 // to FP, which points to the bottom of the stack frame.
1458 // The function getFrameIndexReference can still choose to use SP
1459 // for the offset calculation, so we cannot simply call it here.
1460 // Instead, get the offset (relative to the FP) directly.
1461 Offset = MFI.getObjectOffset(F->getFrameIdx());
1462 } else {
1463 Register FrameReg;
1464 Offset =
1465 getFrameIndexReference(MF, F->getFrameIdx(), FrameReg).getFixed();
1466 }
1467 // Subtract 8 to make room for R30 and R31, which are added above.
1468 Offset -= 8;
1469
1470 if (Reg < Hexagon::D0 || Reg > Hexagon::D15) {
1471 unsigned DwarfReg = HRI.getDwarfRegNum(Reg, true);
1472 auto OffReg = MCCFIInstruction::createOffset(FrameLabel, DwarfReg,
1473 Offset);
1474 BuildMI(MBB, At, DL, CFID)
1475 .addCFIIndex(MF.addFrameInst(OffReg));
1476 } else {
1477 // Split the double regs into subregs, and generate appropriate
1478 // cfi_offsets.
1479 // The only reason, we are split double regs is, llvm-mc does not
1480 // understand paired registers for cfi_offset.
1481 // Eg .cfi_offset r1:0, -64
1482
1483 Register HiReg = HRI.getSubReg(Reg, Hexagon::isub_hi);
1484 Register LoReg = HRI.getSubReg(Reg, Hexagon::isub_lo);
1485 unsigned HiDwarfReg = HRI.getDwarfRegNum(HiReg, true);
1486 unsigned LoDwarfReg = HRI.getDwarfRegNum(LoReg, true);
1487 auto OffHi = MCCFIInstruction::createOffset(FrameLabel, HiDwarfReg,
1488 Offset+4);
1489 BuildMI(MBB, At, DL, CFID)
1490 .addCFIIndex(MF.addFrameInst(OffHi));
1491 auto OffLo = MCCFIInstruction::createOffset(FrameLabel, LoDwarfReg,
1492 Offset);
1493 BuildMI(MBB, At, DL, CFID)
1494 .addCFIIndex(MF.addFrameInst(OffLo));
1495 }
1496 }
1497
1498 // HLX callee-saved registers are spilled to the VTCM stack, which is
1499 // addressed by VSP, not by the CFA. Describe them as
1500 // DW_CFA_expression <reg>, DW_OP_bregx <vsp>, <offset>
1501 // where the offset is relative to the VSP after vallocframe, which stays
1502 // the same in the function body. Preserved registers that are not spilled
1503 // keep their value.
1504 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
1505 unsigned DwVSPReg = HRI.getDwarfRegNum(Hexagon::VSP, true);
1506 int64_t VTCMFrameSize = HMFI.getVTCMFrameInfo().getStackSize();
1507 for (const MCPhysReg *R = HRI.getCalleeSavedRegs(&MF); *R; ++R) {
1508 if (!Hexagon::HlxLRRegClass.contains(*R))
1509 continue;
1510 unsigned DwarfReg = HRI.getDwarfRegNum(*R, true);
1511 auto F = find_if(
1512 CSI, [R](const CalleeSavedInfo &C) { return C.getReg() == *R; });
1513 if (F == CSI.end()) {
1514 auto Same = MCCFIInstruction::createSameValue(FrameLabel, DwarfReg);
1515 BuildMI(MBB, At, DL, CFID).addCFIIndex(MF.addFrameInst(Same));
1516 continue;
1517 }
1518 assert(HMFI.isVTCMStackIndex(F->getFrameIdx()) &&
1519 "HLX register not spilled to the VTCM stack");
1520 int64_t Offset =
1521 VTCMFrameSize + HMFI.getMappedVTCMObjectOffset(F->getFrameIdx());
1522 uint8_t Buf[16];
1523 SmallString<16> Expr, Vals;
1524 Expr.push_back(dwarf::DW_OP_bregx);
1525 Expr.append(Buf, Buf + encodeULEB128(DwVSPReg, Buf));
1526 Expr.append(Buf, Buf + encodeSLEB128(Offset, Buf));
1527 Vals.push_back(dwarf::DW_CFA_expression);
1528 Vals.append(Buf, Buf + encodeULEB128(DwarfReg, Buf));
1529 Vals.append(Buf, Buf + encodeULEB128(Expr.size(), Buf));
1530 Vals.append(Expr);
1531 std::string Comment =
1532 (Twine(HRI.getRegAsmName(*R)) + " at vsp + " + Twine(Offset)).str();
1533 auto Esc =
1534 MCCFIInstruction::createEscape(FrameLabel, Vals, SMLoc(), Comment);
1535 BuildMI(MBB, At, DL, CFID).addCFIIndex(MF.addFrameInst(Esc));
1536 }
1537
1538 // The caller's VSP, saved by insertPrologueInBlock.
1539 if (int VSPFI = HMFI.getVSPFrameIndex(); VSPFI >= 0) {
1540 assert(HasFP && "A VTCM frame requires a frame pointer");
1541 auto OffVSP = MCCFIInstruction::createOffset(
1542 FrameLabel, DwVSPReg, MFI.getObjectOffset(VSPFI) - 8);
1543 BuildMI(MBB, At, DL, CFID).addCFIIndex(MF.addFrameInst(OffVSP));
1544 }
1545}
1546
1548 auto &MFI = MF.getFrameInfo();
1549 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1550 bool HasExtraAlign = HRI.hasStackRealignment(MF);
1551 bool HasAlloca = MFI.hasVarSizedObjects();
1552
1553 // Insert ALLOCFRAME if we need to or at -O0 for the debugger. Think
1554 // that this shouldn't be required, but doing so now because gcc does and
1555 // gdb can't break at the start of the function without it. Will remove if
1556 // this turns out to be a gdb bug.
1557 //
1559 return true;
1560
1561 // By default we want to use SP (since it's always there). FP requires
1562 // some setup (i.e. ALLOCFRAME).
1563 // Both, alloca and stack alignment modify the stack pointer by an
1564 // undetermined value, so we need to save it at the entry to the function
1565 // (i.e. use allocframe).
1566 if (HasAlloca || HasExtraAlign)
1567 return true;
1568
1569 // If FP-elimination is disabled, we have to use FP. This must not be
1570 // gated on stack size: the user/ABI-requested frame pointer is needed
1571 // regardless of whether the function currently has a stack frame.
1572 // Every other target checks DisableFramePointerElim unconditionally.
1574 return true;
1575
1576 if (MFI.getStackSize() > 0) {
1578 return true;
1579 }
1580
1581 const auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
1582 if (HMFI.getVTCMFrameInfo().getStackSize())
1583 return true;
1584 if ((MFI.hasCalls() && !enableAllocFrameElim(MF)) || HMFI.hasClobberLR())
1585 return true;
1586
1587 return false;
1588}
1589
1595
1596static const char *getSpillFunctionFor(Register MaxReg, SpillKind SpillType,
1597 bool Stkchk = false) {
1598 const char * V4SpillToMemoryFunctions[] = {
1599 "__save_r16_through_r17",
1600 "__save_r16_through_r19",
1601 "__save_r16_through_r21",
1602 "__save_r16_through_r23",
1603 "__save_r16_through_r25",
1604 "__save_r16_through_r27" };
1605
1606 const char * V4SpillToMemoryStkchkFunctions[] = {
1607 "__save_r16_through_r17_stkchk",
1608 "__save_r16_through_r19_stkchk",
1609 "__save_r16_through_r21_stkchk",
1610 "__save_r16_through_r23_stkchk",
1611 "__save_r16_through_r25_stkchk",
1612 "__save_r16_through_r27_stkchk" };
1613
1614 const char * V4SpillFromMemoryFunctions[] = {
1615 "__restore_r16_through_r17_and_deallocframe",
1616 "__restore_r16_through_r19_and_deallocframe",
1617 "__restore_r16_through_r21_and_deallocframe",
1618 "__restore_r16_through_r23_and_deallocframe",
1619 "__restore_r16_through_r25_and_deallocframe",
1620 "__restore_r16_through_r27_and_deallocframe" };
1621
1622 const char * V4SpillFromMemoryTailcallFunctions[] = {
1623 "__restore_r16_through_r17_and_deallocframe_before_tailcall",
1624 "__restore_r16_through_r19_and_deallocframe_before_tailcall",
1625 "__restore_r16_through_r21_and_deallocframe_before_tailcall",
1626 "__restore_r16_through_r23_and_deallocframe_before_tailcall",
1627 "__restore_r16_through_r25_and_deallocframe_before_tailcall",
1628 "__restore_r16_through_r27_and_deallocframe_before_tailcall"
1629 };
1630
1631 const char **SpillFunc = nullptr;
1632
1633 switch(SpillType) {
1634 case SK_ToMem:
1635 SpillFunc = Stkchk ? V4SpillToMemoryStkchkFunctions
1636 : V4SpillToMemoryFunctions;
1637 break;
1638 case SK_FromMem:
1639 SpillFunc = V4SpillFromMemoryFunctions;
1640 break;
1641 case SK_FromMemTailcall:
1642 SpillFunc = V4SpillFromMemoryTailcallFunctions;
1643 break;
1644 }
1645 assert(SpillFunc && "Unknown spill kind");
1646
1647 // Spill all callee-saved registers up to the highest register used.
1648 switch (MaxReg) {
1649 case Hexagon::R17:
1650 return SpillFunc[0];
1651 case Hexagon::R19:
1652 return SpillFunc[1];
1653 case Hexagon::R21:
1654 return SpillFunc[2];
1655 case Hexagon::R23:
1656 return SpillFunc[3];
1657 case Hexagon::R25:
1658 return SpillFunc[4];
1659 case Hexagon::R27:
1660 return SpillFunc[5];
1661 default:
1662 llvm_unreachable("Unhandled maximum callee save register");
1663 }
1664 return nullptr;
1665}
1666
1667StackOffset
1669 Register &FrameReg) const {
1670 auto &MFI = MF.getFrameInfo();
1671 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1672
1673 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
1674 if (HMFI.isVTCMStackIndex(FI)) {
1675 const MachineFrameInfo &VTCMFrameInfo = HMFI.getVTCMFrameInfo();
1676 FrameReg = HRI.getVTCMStackRegister();
1677 return StackOffset::getFixed(VTCMFrameInfo.getStackSize() +
1678 HMFI.getMappedVTCMObjectOffset(FI));
1679 }
1680
1681 int Offset = MFI.getObjectOffset(FI);
1682 bool HasAlloca = MFI.hasVarSizedObjects();
1683 bool HasExtraAlign = HRI.hasStackRealignment(MF);
1684 bool NoOpt = MF.getTarget().getOptLevel() == CodeGenOptLevel::None;
1685
1686 unsigned FrameSize = MFI.getStackSize();
1687 Register SP = HRI.getStackRegister();
1688 Register FP = HRI.getFrameRegister();
1689 Register AP = HMFI.getStackAlignBaseReg();
1690 // It may happen that AP will be absent even HasAlloca && HasExtraAlign
1691 // is true. HasExtraAlign may be set because of vector spills, without
1692 // aligned locals or aligned outgoing function arguments. Since vector
1693 // spills will ultimately be "unaligned", it is safe to use FP as the
1694 // base register.
1695 // In fact, in such a scenario the stack is actually not required to be
1696 // aligned, although it may end up being aligned anyway, since this
1697 // particular case is not easily detectable. The alignment will be
1698 // unnecessary, but not incorrect.
1699 // Unfortunately there is no quick way to verify that the above is
1700 // indeed the case (and that it's not a result of an error), so just
1701 // assume that missing AP will be replaced by FP.
1702 // (A better fix would be to rematerialize AP from FP and always align
1703 // vector spills.)
1704 bool UseFP = false, UseAP = false; // Default: use SP (except at -O0).
1705 // Use FP at -O0, except when there are objects with extra alignment.
1706 // That additional alignment requirement may cause a pad to be inserted,
1707 // which will make it impossible to use FP to access objects located
1708 // past the pad.
1709 if (NoOpt && !HasExtraAlign)
1710 UseFP = true;
1711 if (MFI.isFixedObjectIndex(FI) || MFI.isObjectPreAllocated(FI)) {
1712 // Fixed and preallocated objects will be located before any padding
1713 // so FP must be used to access them.
1714 UseFP |= (HasAlloca || HasExtraAlign);
1715 } else {
1716 if (HasAlloca) {
1717 if (HasExtraAlign)
1718 UseAP = true;
1719 else
1720 UseFP = true;
1721 }
1722 }
1723
1724 // If FP was picked, then there had better be FP.
1725 bool HasFP = hasFP(MF);
1726 assert((HasFP || !UseFP) && "This function must have frame pointer");
1727
1728 // Having FP implies allocframe. Allocframe will store extra 8 bytes:
1729 // FP/LR. If the base register is used to access an object across these
1730 // 8 bytes, then the offset will need to be adjusted by 8.
1731 //
1732 // After allocframe:
1733 // HexagonISelLowering adds 8 to ---+
1734 // the offsets of all stack-based |
1735 // arguments (*) |
1736 // |
1737 // getObjectOffset < 0 0 8 getObjectOffset >= 8
1738 // ------------------------+-----+------------------------> increasing
1739 // <local objects> |FP/LR| <input arguments> addresses
1740 // -----------------+------+-----+------------------------>
1741 // | |
1742 // SP/AP point --+ +-- FP points here (**)
1743 // somewhere on
1744 // this side of FP/LR
1745 //
1746 // (*) See LowerFormalArguments. The FP/LR is assumed to be present.
1747 // (**) *FP == old-FP. FP+0..7 are the bytes of FP/LR.
1748
1749 // The lowering assumes that FP/LR is present, and so the offsets of
1750 // the formal arguments start at 8. If FP/LR is not there we need to
1751 // reduce the offset by 8.
1752 if (Offset > 0 && !HasFP)
1753 Offset -= 8;
1754
1755 if (UseFP)
1756 FrameReg = FP;
1757 else if (UseAP)
1758 FrameReg = AP;
1759 else
1760 FrameReg = SP;
1761
1762 // Calculate the actual offset in the instruction. If there is no FP
1763 // (in other words, no allocframe), then SP will not be adjusted (i.e.
1764 // there will be no SP -= FrameSize), so the frame size should not be
1765 // added to the calculated offset.
1766 int RealOffset = Offset;
1767 if (!UseFP && !UseAP)
1768 RealOffset = FrameSize+Offset;
1769 return StackOffset::getFixed(RealOffset);
1770}
1771
1772MachineBasicBlock::iterator HexagonFrameLowering::insertCSRSpillsInBlock(
1773 MachineBasicBlock &MBB, const CSIVect &CSI, const HexagonRegisterInfo &HRI,
1774 bool &PrologueStubs) const {
1776 if (CSI.empty())
1777 return MI;
1778
1779 PrologueStubs = false;
1780 MachineFunction &MF = *MBB.getParent();
1781 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1782 auto &HII = *HST.getInstrInfo();
1783
1784 if (useSpillFunction(MF, CSI)) {
1785 PrologueStubs = true;
1786 Register MaxReg = getMaxCalleeSavedReg(CSI, HRI);
1787 bool StkOvrFlowEnabled = EnableStackOVFSanitizer;
1788 const char *SpillFun = getSpillFunctionFor(MaxReg, SK_ToMem,
1789 StkOvrFlowEnabled);
1790 auto &HTM = static_cast<const HexagonTargetMachine&>(MF.getTarget());
1791 bool IsPIC = HTM.isPositionIndependent();
1792 bool LongCalls = HST.useLongCalls() || EnableSaveRestoreLong;
1793
1794 // Call spill function.
1795 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
1796 unsigned SpillOpc;
1797 if (StkOvrFlowEnabled) {
1798 if (LongCalls)
1799 SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4STK_EXT_PIC
1800 : Hexagon::SAVE_REGISTERS_CALL_V4STK_EXT;
1801 else
1802 SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4STK_PIC
1803 : Hexagon::SAVE_REGISTERS_CALL_V4STK;
1804 } else {
1805 if (LongCalls)
1806 SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC
1807 : Hexagon::SAVE_REGISTERS_CALL_V4_EXT;
1808 else
1809 SpillOpc = IsPIC ? Hexagon::SAVE_REGISTERS_CALL_V4_PIC
1810 : Hexagon::SAVE_REGISTERS_CALL_V4;
1811 }
1812
1813 MachineInstr *SaveRegsCall =
1814 BuildMI(MBB, MI, DL, HII.get(SpillOpc))
1815 .addExternalSymbol(SpillFun);
1816
1817 // Add callee-saved registers as use.
1818 addCalleeSaveRegistersAsImpOperand(SaveRegsCall, CSI, false, true);
1819 // Add live in registers.
1820 for (const CalleeSavedInfo &I : CSI)
1821 MBB.addLiveIn(I.getReg());
1822 } else {
1823 for (const CalleeSavedInfo &I : CSI) {
1824 MCRegister Reg = I.getReg();
1825 // Add live in registers. We treat eh_return callee saved register r0 - r3
1826 // specially. They are not really callee saved registers as they are not
1827 // supposed to be killed.
1828 bool IsKill = !HRI.isEHReturnCalleeSaveReg(Reg);
1829 int FI = I.getFrameIdx();
1830 const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg);
1831 HII.storeRegToStackSlot(MBB, MI, Reg, IsKill, FI, RC, Register());
1832 if (IsKill)
1833 MBB.addLiveIn(Reg);
1834 }
1835 }
1836
1837 return MI;
1838}
1839
1840bool HexagonFrameLowering::insertCSRRestoresInBlock(MachineBasicBlock &MBB,
1841 const CSIVect &CSI, const HexagonRegisterInfo &HRI) const {
1842 if (CSI.empty())
1843 return false;
1844
1846 MachineFunction &MF = *MBB.getParent();
1847 auto &HST = MF.getSubtarget<HexagonSubtarget>();
1848 auto &HII = *HST.getInstrInfo();
1849
1850 if (useRestoreFunction(MF, CSI)) {
1851 bool HasTC = hasTailCall(MBB) || !hasReturn(MBB);
1852 Register MaxR = getMaxCalleeSavedReg(CSI, HRI);
1854 const char *RestoreFn = getSpillFunctionFor(MaxR, Kind);
1855 auto &HTM = static_cast<const HexagonTargetMachine&>(MF.getTarget());
1856 bool IsPIC = HTM.isPositionIndependent();
1857 bool LongCalls = HST.useLongCalls() || EnableSaveRestoreLong;
1858
1859 // Call spill function.
1860 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc()
1861 : MBB.findDebugLoc(MBB.end());
1862 MachineInstr *DeallocCall = nullptr;
1863
1864 if (HasTC) {
1865 unsigned RetOpc;
1866 if (LongCalls)
1867 RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT_PIC
1868 : Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_EXT;
1869 else
1870 RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4_PIC
1871 : Hexagon::RESTORE_DEALLOC_BEFORE_TAILCALL_V4;
1872 DeallocCall = BuildMI(MBB, MI, DL, HII.get(RetOpc))
1873 .addExternalSymbol(RestoreFn);
1874 } else {
1875 // The block has a return.
1877 assert(It->isReturn() && std::next(It) == MBB.end());
1878 unsigned RetOpc;
1879 if (LongCalls)
1880 RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT_PIC
1881 : Hexagon::RESTORE_DEALLOC_RET_JMP_V4_EXT;
1882 else
1883 RetOpc = IsPIC ? Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC
1884 : Hexagon::RESTORE_DEALLOC_RET_JMP_V4;
1885 MachineInstrBuilder MIB =
1886 BuildMI(MBB, It, DL, HII.get(RetOpc)).addExternalSymbol(RestoreFn);
1887 // Transfer the function live-out registers, avoiding duplicating the pc
1888 // implicit-def.
1889 for (const MachineOperand &MO : drop_begin(It->implicit_operands()))
1890 MIB.add(MO);
1891 DeallocCall = MIB;
1892 }
1893 addCalleeSaveRegistersAsImpOperand(DeallocCall, CSI, true, false);
1894 return true;
1895 }
1896
1897 for (const CalleeSavedInfo &I : CSI) {
1898 MCRegister Reg = I.getReg();
1899 const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg);
1900 int FI = I.getFrameIdx();
1901 HII.loadRegFromStackSlot(MBB, MI, Reg, FI, RC, Register());
1902 }
1903
1904 return true;
1905}
1906
1910 MachineInstr &MI = *I;
1911 unsigned Opc = MI.getOpcode();
1912 (void)Opc; // Silence compiler warning.
1913 assert((Opc == Hexagon::ADJCALLSTACKDOWN || Opc == Hexagon::ADJCALLSTACKUP) &&
1914 "Cannot handle this call frame pseudo instruction");
1915 return MBB.erase(I);
1916}
1917
1918void HexagonFrameLowering::computeVTCMStackOffsets(MachineFunction &MF) const {
1919 MachineFrameInfo &MFI = MF.getFrameInfo();
1920 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
1921 const auto &HRI = *HST.getRegisterInfo();
1922 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
1923 MachineFrameInfo &VTCMFrameInfo = HMFI.getVTCMFrameInfo();
1924 const int64_t HlxLRSize = HRI.getSpillSize(Hexagon::HlxLRRegClass);
1925 const int64_t HlxLLRSize = HRI.getSpillSize(Hexagon::HlxLLRRegClass);
1926
1927 // Incoming HLX stack arguments are just above the VTCM frame. NoAlloc
1928 // fixed objects are incoming HLX stack arguments (see LowerFormalArguments).
1929 for (int I = MFI.getObjectIndexBegin(); I != 0; ++I) {
1931 continue;
1932 HMFI.moveToVTCMStack(I, VTCMFrameInfo.CreateFixedObject(
1933 MFI.getObjectSize(I), MFI.getObjectOffset(I),
1934 /*IsImmutable=*/true));
1935 }
1936
1937 int64_t Offset = 0;
1938 for (int I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) {
1939 bool IsHlxSpill =
1940 MFI.isSpillSlotObjectIndex(I) && (MFI.getObjectSize(I) == HlxLRSize ||
1941 MFI.getObjectSize(I) == HlxLLRSize);
1942 if (MFI.isDeadObjectIndex(I) || MFI.isFixedObjectIndex(I) ||
1943 (!IsHlxSpill && MFI.isSpillSlotObjectIndex(I)) ||
1945 continue;
1946 const AllocaInst *AI = MFI.getObjectAllocation(I);
1947 if (!IsHlxSpill && (!AI || !containsHLXObject(HST, AI->getAllocatedType())))
1948 continue;
1949
1950 int64_t Size = MFI.getObjectSize(I);
1951 Align Alignment = MFI.getObjectAlign(I);
1952 int VTCMIndex =
1953 IsHlxSpill ? VTCMFrameInfo.CreateSpillStackObject(Size, Alignment)
1954 : VTCMFrameInfo.CreateStackObject(
1955 Size, Alignment, false, MFI.getObjectAllocation(I));
1956 Offset += Size;
1957 VTCMFrameInfo.setObjectOffset(VTCMIndex, -Offset);
1958 HMFI.moveToVTCMStack(I, VTCMIndex);
1959 MFI.RemoveStackObject(I);
1960 }
1961 // Outgoing HLX stack arguments are stored at VSP+0 and up, below the
1962 // objects above.
1963 uint64_t StackSize = Offset + HMFI.getMaxVTCMCallFrameSize();
1964 VTCMFrameInfo.setStackSize(StackSize);
1965
1966 // The "hlx-vector-stack-limit" function attribute overrides the option.
1967 const Function &F = MF.getFunction();
1968 uint64_t Limit = F.getFnAttributeAsParsedInteger("hlx-vector-stack-limit",
1970 if (StackSize > Limit)
1971 F.getContext().diagnose(DiagnosticInfoResourceLimit(
1972 F, "HLX vector stack size", StackSize, Limit, DS_Error));
1973}
1974
1976 MachineFunction &MF, RegScavenger *RS) const {
1977 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
1978 if (!HST.hasV85OpsOnly() || !HST.useHLXOps())
1979 return;
1980 computeVTCMStackOffsets(MF);
1981 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
1982 if (SaveParentVSP && HMFI.getVTCMFrameInfo().getStackSize())
1983 HMFI.setVSPFrameIndex(
1985}
1986
1987Align HexagonFrameLowering::getMaxStackAlign(const MachineFunction &MF) const {
1988 const MachineFrameInfo &MFI = MF.getFrameInfo();
1989 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
1990 if (!HST.hasV85OpsOnly() || !HST.useHLXOps())
1991 return MFI.getMaxAlign();
1992
1993 Align MaxAlign(1);
1994 for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd(); I != E;
1995 ++I) {
1996 if (MFI.isDeadObjectIndex(I))
1997 continue;
1998 MaxAlign = std::max(MaxAlign, MFI.getObjectAlign(I));
1999 }
2000 return MaxAlign;
2001}
2002
2003/// Returns true if there are no caller-saved registers available in class RC.
2005 const HexagonRegisterInfo &HRI, const TargetRegisterClass *RC) {
2007
2008 auto IsUsed = [&HRI,&MRI] (Register Reg) -> bool {
2009 for (MCRegAliasIterator AI(Reg, &HRI, true); AI.isValid(); ++AI)
2010 if (MRI.isPhysRegUsed(*AI))
2011 return true;
2012 return false;
2013 };
2014
2015 // Check for an unused caller-saved register. Callee-saved registers
2016 // have become pristine by now.
2017 for (const MCPhysReg *P = HRI.getCallerSavedRegs(&MF, RC); *P; ++P)
2018 if (!IsUsed(*P))
2019 return false;
2020
2021 // All caller-saved registers are used.
2022 return true;
2023}
2024
2025#ifndef NDEBUG
2027 dbgs() << '{';
2028 for (int x = Regs.find_first(); x >= 0; x = Regs.find_next(x)) {
2029 Register R = x;
2030 dbgs() << ' ' << printReg(R, &TRI);
2031 }
2032 dbgs() << " }";
2033}
2034#endif
2035
2037 const TargetRegisterInfo *TRI, std::vector<CalleeSavedInfo> &CSI) const {
2038 LLVM_DEBUG(dbgs() << __func__ << " on " << MF.getName() << '\n');
2039 MachineFrameInfo &MFI = MF.getFrameInfo();
2040 BitVector SRegs(Hexagon::NUM_TARGET_REGS);
2041
2042 // Generate a set of unique, callee-saved registers (SRegs), where each
2043 // register in the set is maximal in terms of sub-/super-register relation,
2044 // i.e. for each R in SRegs, no proper super-register of R is also in SRegs.
2045
2046 // (1) For each callee-saved register, add that register and all of its
2047 // sub-registers to SRegs.
2048 LLVM_DEBUG(dbgs() << "Initial CS registers: {");
2049 for (const CalleeSavedInfo &I : CSI) {
2050 Register R = I.getReg();
2051 LLVM_DEBUG(dbgs() << ' ' << printReg(R, TRI));
2052 for (MCPhysReg SR : TRI->subregs_inclusive(R))
2053 SRegs[SR] = true;
2054 }
2055 LLVM_DEBUG(dbgs() << " }\n");
2056 LLVM_DEBUG(dbgs() << "SRegs.1: "; dump_registers(SRegs, *TRI);
2057 dbgs() << "\n");
2058
2059 // (2) For each reserved register, remove that register and all of its
2060 // sub- and super-registers from SRegs.
2061 BitVector Reserved = TRI->getReservedRegs(MF);
2062 // Unreserve the stack align register: it is reserved for this function
2063 // only, it still needs to be saved/restored.
2064 Register AP =
2065 MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
2066 assert((!needsAligna(MF) || AP.isValid()) &&
2067 "AP must be assigned before register allocation");
2068 if (AP.isValid()) {
2069 Reserved[AP] = false;
2070 // Unreserve super-regs if no other subregisters are reserved.
2071 for (MCPhysReg SP : TRI->superregs(AP)) {
2072 bool HasResSub = false;
2073 for (MCPhysReg SB : TRI->subregs(SP)) {
2074 if (!Reserved[SB])
2075 continue;
2076 HasResSub = true;
2077 break;
2078 }
2079 if (!HasResSub)
2080 Reserved[SP] = false;
2081 }
2082 }
2083
2084 for (int x = Reserved.find_first(); x >= 0; x = Reserved.find_next(x)) {
2085 Register R = x;
2086 for (MCPhysReg SR : TRI->superregs_inclusive(R))
2087 SRegs[SR] = false;
2088 }
2089 LLVM_DEBUG(dbgs() << "Res: "; dump_registers(Reserved, *TRI);
2090 dbgs() << "\n");
2091 LLVM_DEBUG(dbgs() << "SRegs.2: "; dump_registers(SRegs, *TRI);
2092 dbgs() << "\n");
2093
2094 // (3) Collect all registers that have at least one sub-register in SRegs,
2095 // and also have no sub-registers that are reserved. These will be the can-
2096 // didates for saving as a whole instead of their individual sub-registers.
2097 // (Saving R17:16 instead of R16 is fine, but only if R17 was not reserved.)
2098 // HLX registers are saved individually: there is no HLX pair spill.
2099 BitVector TmpSup(Hexagon::NUM_TARGET_REGS);
2100 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
2101 Register R = x;
2102 if (Hexagon::HlxLRRegClass.contains(R))
2103 continue;
2104 for (MCPhysReg SR : TRI->superregs(R))
2105 TmpSup[SR] = true;
2106 }
2107 for (int x = TmpSup.find_first(); x >= 0; x = TmpSup.find_next(x)) {
2108 Register R = x;
2109 for (MCPhysReg SR : TRI->subregs_inclusive(R)) {
2110 if (!Reserved[SR])
2111 continue;
2112 TmpSup[R] = false;
2113 break;
2114 }
2115 }
2116 LLVM_DEBUG(dbgs() << "TmpSup: "; dump_registers(TmpSup, *TRI);
2117 dbgs() << "\n");
2118
2119 // (4) Include all super-registers found in (3) into SRegs.
2120 SRegs |= TmpSup;
2121 LLVM_DEBUG(dbgs() << "SRegs.4: "; dump_registers(SRegs, *TRI);
2122 dbgs() << "\n");
2123
2124 // (5) For each register R in SRegs, if any super-register of R is in SRegs,
2125 // remove R from SRegs.
2126 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
2127 Register R = x;
2128 for (MCPhysReg SR : TRI->superregs(R)) {
2129 if (!SRegs[SR])
2130 continue;
2131 SRegs[R] = false;
2132 break;
2133 }
2134 }
2135 LLVM_DEBUG(dbgs() << "SRegs.5: "; dump_registers(SRegs, *TRI);
2136 dbgs() << "\n");
2137
2138 // Now, for each register that has a fixed stack slot, create the stack
2139 // object for it.
2140 CSI.clear();
2141
2143
2144 unsigned NumFixed;
2145 int64_t MinOffset = 0; // CS offsets are negative.
2146 const SpillSlot *FixedSlots = getCalleeSavedSpillSlots(NumFixed);
2147 for (const SpillSlot *S = FixedSlots; S != FixedSlots+NumFixed; ++S) {
2148 if (!SRegs[S->Reg])
2149 continue;
2150 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(S->Reg);
2151 int FI = MFI.CreateFixedSpillStackObject(TRI->getSpillSize(*RC), S->Offset);
2152 MinOffset = std::min(MinOffset, S->Offset);
2153 CSI.push_back(CalleeSavedInfo(S->Reg, FI));
2154 SRegs[S->Reg] = false;
2155 }
2156
2157 // There can be some registers that don't have fixed slots. For example,
2158 // we need to store R0-R3 in functions with exception handling. For each
2159 // such register, create a non-fixed stack object.
2160 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
2161 Register R = x;
2162 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(R);
2163 unsigned Size = TRI->getSpillSize(*RC);
2164 // HLX registers are saved in spill slots on the VTCM stack.
2165 if (Hexagon::HlxLRRegClass.contains(R)) {
2166 int FI = MFI.CreateSpillStackObject(Size, TRI->getSpillAlign(*RC));
2167 CSI.push_back(CalleeSavedInfo(R, FI));
2168 SRegs[R] = false;
2169 continue;
2170 }
2171 int64_t Off = MinOffset - Size;
2172 Align Alignment = std::min(TRI->getSpillAlign(*RC), getStackAlign());
2173 Off &= -Alignment.value();
2174 int FI = MFI.CreateFixedSpillStackObject(Size, Off);
2175 MinOffset = std::min(MinOffset, Off);
2176 CSI.push_back(CalleeSavedInfo(R, FI));
2177 SRegs[R] = false;
2178 }
2179
2180 LLVM_DEBUG({
2181 dbgs() << "CS information: {";
2182 for (const CalleeSavedInfo &I : CSI) {
2183 int FI = I.getFrameIdx();
2184 int Off = MFI.getObjectOffset(FI);
2185 dbgs() << ' ' << printReg(I.getReg(), TRI) << ":fi#" << FI << ":sp";
2186 if (Off >= 0)
2187 dbgs() << '+';
2188 dbgs() << Off;
2189 }
2190 dbgs() << " }\n";
2191 });
2192
2193#ifndef NDEBUG
2194 // Verify that all registers were handled.
2195 bool MissedReg = false;
2196 for (int x = SRegs.find_first(); x >= 0; x = SRegs.find_next(x)) {
2197 Register R = x;
2198 dbgs() << printReg(R, TRI) << ' ';
2199 MissedReg = true;
2200 }
2201 if (MissedReg)
2202 llvm_unreachable("...there are unhandled callee-saved registers!");
2203#endif
2204
2205 return true;
2206}
2207
2208bool HexagonFrameLowering::expandCopy(MachineBasicBlock &B,
2210 const HexagonInstrInfo &HII, SmallVectorImpl<Register> &NewRegs) const {
2211 MachineInstr *MI = &*It;
2212 DebugLoc DL = MI->getDebugLoc();
2213 Register DstR = MI->getOperand(0).getReg();
2214 Register SrcR = MI->getOperand(1).getReg();
2215 if (!Hexagon::ModRegsRegClass.contains(DstR) ||
2216 !Hexagon::ModRegsRegClass.contains(SrcR))
2217 return false;
2218
2219 Register TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2220 BuildMI(B, It, DL, HII.get(TargetOpcode::COPY), TmpR).add(MI->getOperand(1));
2221 BuildMI(B, It, DL, HII.get(TargetOpcode::COPY), DstR)
2222 .addReg(TmpR, RegState::Kill);
2223
2224 NewRegs.push_back(TmpR);
2225 B.erase(It);
2226 return true;
2227}
2228
2229bool HexagonFrameLowering::expandStoreInt(MachineBasicBlock &B,
2231 const HexagonInstrInfo &HII, SmallVectorImpl<Register> &NewRegs) const {
2232 MachineInstr *MI = &*It;
2233 if (!MI->getOperand(0).isFI())
2234 return false;
2235
2236 DebugLoc DL = MI->getDebugLoc();
2237 unsigned Opc = MI->getOpcode();
2238 Register SrcR = MI->getOperand(2).getReg();
2239 bool IsKill = MI->getOperand(2).isKill();
2240 int FI = MI->getOperand(0).getIndex();
2241
2242 // TmpR = C2_tfrpr SrcR if SrcR is a predicate register
2243 // TmpR = A2_tfrcrr SrcR if SrcR is a modifier register
2244 Register TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2245 unsigned TfrOpc = (Opc == Hexagon::STriw_pred) ? Hexagon::C2_tfrpr
2246 : Hexagon::A2_tfrcrr;
2247 BuildMI(B, It, DL, HII.get(TfrOpc), TmpR)
2248 .addReg(SrcR, getKillRegState(IsKill));
2249
2250 // S2_storeri_io FI, 0, TmpR
2251 BuildMI(B, It, DL, HII.get(Hexagon::S2_storeri_io))
2252 .addFrameIndex(FI)
2253 .addImm(0)
2254 .addReg(TmpR, RegState::Kill)
2255 .cloneMemRefs(*MI);
2256
2257 NewRegs.push_back(TmpR);
2258 B.erase(It);
2259 return true;
2260}
2261
2262bool HexagonFrameLowering::expandStoreCirc(
2263 MachineBasicBlock &B, MachineBasicBlock::iterator It,
2264 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
2265 SmallVectorImpl<Register> &NewRegs) const {
2266 MachineFunction &MF = *B.getParent();
2267 const auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
2268 MachineInstr *MI = &*It;
2269 if (!MI->getOperand(0).isFI())
2270 return false;
2271
2272 DebugLoc DL = MI->getDebugLoc();
2273 Register SrcR = MI->getOperand(1).getReg();
2274 bool IsKill = MI->getOperand(1).isKill();
2275 int FI = MI->getOperand(0).getIndex();
2276
2277 unsigned Offset = 0;
2278 for (auto SubReg : {Hexagon::isub_lo, Hexagon::isub_hi}) {
2279 Register SrcSubReg = HRI.getSubReg(SrcR, SubReg);
2280
2281 // TmpR = A2_tfrcrr SrcSubReg.
2282 Register TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2283 BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrcrr), TmpR)
2284 .addReg(SrcSubReg, getKillRegState(IsKill))
2285 ->copyImplicitOps(MF, *It);
2286
2287 // S2_storeri_io FI/FI+offset, TmpR.
2288 BuildMI(B, It, DL, HII.get(Hexagon::S2_storeri_io))
2289 .addFrameIndex(FI)
2290 .addImm(Offset)
2291 .addReg(TmpR, RegState::Kill)
2292 .cloneMemRefs(*MI);
2293 NewRegs.push_back(TmpR);
2294 Offset += HRI.getSpillSize(Hexagon::IntRegsRegClass);
2295 }
2296
2297 B.erase(It);
2298 return true;
2299}
2300
2301bool HexagonFrameLowering::expandLoadCirc(
2302 MachineBasicBlock &B, MachineBasicBlock::iterator It,
2303 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
2304 SmallVectorImpl<Register> &NewRegs) const {
2305 MachineFunction &MF = *B.getParent();
2306 const auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
2307 MachineInstr *MI = &*It;
2308 if (!MI->getOperand(1).isFI())
2309 return false;
2310
2311 DebugLoc DL = MI->getDebugLoc();
2312 Register DstR = MI->getOperand(0).getReg();
2313 int FI = MI->getOperand(1).getIndex();
2314
2315 unsigned Offset = 0;
2316 for (auto SubReg : {Hexagon::isub_lo, Hexagon::isub_hi}) {
2317 Register DstSubReg = HRI.getSubReg(DstR, SubReg);
2318
2319 // TmpR = L2_loadri_io FI, Offset.
2320 Register TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2321 BuildMI(B, It, DL, HII.get(Hexagon::L2_loadri_io), TmpR)
2322 .addFrameIndex(FI)
2323 .addImm(Offset)
2324 .cloneMemRefs(*MI);
2325
2326 // DstR = A2_tfrrcr TmpR.
2327 BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrrcr), DstSubReg)
2328 .addReg(TmpR, RegState::Kill)
2329 ->copyImplicitOps(MF, *It);
2330
2331 NewRegs.push_back(TmpR);
2332 Offset += HRI.getSpillSize(Hexagon::IntRegsRegClass);
2333 }
2334
2335 B.erase(It);
2336 return true;
2337}
2338
2339bool HexagonFrameLowering::expandLoadInt(
2340 MachineBasicBlock &B, MachineBasicBlock::iterator It,
2341 MachineRegisterInfo &MRI, const HexagonInstrInfo &HII,
2342 SmallVectorImpl<Register> &NewRegs) const {
2343 MachineInstr *MI = &*It;
2344 if (!MI->getOperand(1).isFI())
2345 return false;
2346
2347 DebugLoc DL = MI->getDebugLoc();
2348 unsigned Opc = MI->getOpcode();
2349 Register DstR = MI->getOperand(0).getReg();
2350 int FI = MI->getOperand(1).getIndex();
2351
2352 // TmpR = L2_loadri_io FI, 0
2353 Register TmpR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2354 BuildMI(B, It, DL, HII.get(Hexagon::L2_loadri_io), TmpR)
2355 .addFrameIndex(FI)
2356 .addImm(0)
2357 .cloneMemRefs(*MI);
2358
2359 // DstR = C2_tfrrp TmpR if DstR is a predicate register
2360 // DstR = A2_tfrrcr TmpR if DstR is a modifier register
2361 unsigned TfrOpc = (Opc == Hexagon::LDriw_pred) ? Hexagon::C2_tfrrp
2362 : Hexagon::A2_tfrrcr;
2363 BuildMI(B, It, DL, HII.get(TfrOpc), DstR)
2364 .addReg(TmpR, RegState::Kill);
2365
2366 NewRegs.push_back(TmpR);
2367 B.erase(It);
2368 return true;
2369}
2370
2371bool HexagonFrameLowering::expandStoreVecPred(MachineBasicBlock &B,
2372 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
2373 const HexagonInstrInfo &HII, SmallVectorImpl<Register> &NewRegs) const {
2374 MachineInstr *MI = &*It;
2375 if (!MI->getOperand(0).isFI())
2376 return false;
2377
2378 DebugLoc DL = MI->getDebugLoc();
2379 Register SrcR = MI->getOperand(2).getReg();
2380 bool IsKill = MI->getOperand(2).isKill();
2381 int FI = MI->getOperand(0).getIndex();
2382 auto *RC = &Hexagon::HvxVRRegClass;
2383
2384 // Insert transfer to general vector register.
2385 // TmpR0 = A2_tfrsi 0x01010101
2386 // TmpR1 = V6_vandqrt Qx, TmpR0
2387 // store FI, 0, TmpR1
2388 Register TmpR0 = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2390
2391 BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrsi), TmpR0)
2392 .addImm(0x01010101);
2393
2394 BuildMI(B, It, DL, HII.get(Hexagon::V6_vandqrt), TmpR1)
2395 .addReg(SrcR, getKillRegState(IsKill))
2396 .addReg(TmpR0, RegState::Kill);
2397
2398 HII.storeRegToStackSlot(B, It, TmpR1, true, FI, RC, Register());
2399 expandStoreVec(B, std::prev(It), MRI, HII, NewRegs);
2400
2401 NewRegs.push_back(TmpR0);
2402 NewRegs.push_back(TmpR1);
2403 B.erase(It);
2404 return true;
2405}
2406
2407bool HexagonFrameLowering::expandLoadVecPred(MachineBasicBlock &B,
2408 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
2409 const HexagonInstrInfo &HII, SmallVectorImpl<Register> &NewRegs) const {
2410 MachineInstr *MI = &*It;
2411 if (!MI->getOperand(1).isFI())
2412 return false;
2413
2414 DebugLoc DL = MI->getDebugLoc();
2415 Register DstR = MI->getOperand(0).getReg();
2416 int FI = MI->getOperand(1).getIndex();
2417 auto *RC = &Hexagon::HvxVRRegClass;
2418
2419 // TmpR0 = A2_tfrsi 0x01010101
2420 // TmpR1 = load FI, 0
2421 // DstR = V6_vandvrt TmpR1, TmpR0
2422 Register TmpR0 = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2424
2425 BuildMI(B, It, DL, HII.get(Hexagon::A2_tfrsi), TmpR0)
2426 .addImm(0x01010101);
2427 HII.loadRegFromStackSlot(B, It, TmpR1, FI, RC, Register());
2428 expandLoadVec(B, std::prev(It), MRI, HII, NewRegs);
2429
2430 BuildMI(B, It, DL, HII.get(Hexagon::V6_vandvrt), DstR)
2431 .addReg(TmpR1, RegState::Kill)
2432 .addReg(TmpR0, RegState::Kill);
2433
2434 NewRegs.push_back(TmpR0);
2435 NewRegs.push_back(TmpR1);
2436 B.erase(It);
2437 return true;
2438}
2439
2440bool HexagonFrameLowering::expandStoreVec2(MachineBasicBlock &B,
2442 MachineRegisterInfo &MRI,
2443 const HexagonInstrInfo &HII,
2444 SmallVectorImpl<Register> &NewRegs,
2445 bool IsHLX) const {
2446 MachineFunction &MF = *B.getParent();
2447 auto &MFI = MF.getFrameInfo();
2448 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
2449 MachineInstr *MI = &*It;
2450 if (!MI->getOperand(0).isFI())
2451 return false;
2452
2453 // It is possible that the double vector being stored is only partially
2454 // defined. From the point of view of the liveness tracking, it is ok to
2455 // store it as a whole, but if we break it up we may end up storing a
2456 // register that is entirely undefined.
2457 LivePhysRegs LPR(HRI);
2458 LPR.addLiveIns(B);
2460 for (auto R = B.begin(); R != It; ++R) {
2461 Clobbers.clear();
2462 LPR.stepForward(*R, Clobbers);
2463 }
2464
2465 DebugLoc DL = MI->getDebugLoc();
2466 Register SrcR = MI->getOperand(2).getReg();
2467 Register SrcLo = HRI.getSubReg(SrcR, Hexagon::vsub_lo);
2468 Register SrcHi = HRI.getSubReg(SrcR, Hexagon::vsub_hi);
2469 bool IsKill = MI->getOperand(2).isKill();
2470 int FI = MI->getOperand(0).getIndex();
2471
2472 if (IsHLX) {
2473 const MachineOperand &SrcOp = MI->getOperand(2);
2474 unsigned Size = HRI.getSpillSize(Hexagon::HlxLRRegClass);
2475 assert(MI->hasOneMemOperand() && "HLX pair spill must have one MMO");
2476 const MachineMemOperand *PairMMO = *MI->memoperands_begin();
2477 MachineMemOperand *LoMMO = MF.getMachineMemOperand(PairMMO, 0, Size);
2478 MachineMemOperand *HiMMO = MF.getMachineMemOperand(PairMMO, Size, Size);
2479
2480 if (LPR.contains(SrcLo)) {
2481 BuildMI(B, It, DL, HII.get(Hexagon::LX_S16b_pseudo))
2482 .addFrameIndex(FI)
2483 .addReg(SrcLo, getRegState(SrcOp))
2484 .addReg(Hexagon::VSP, RegState::Implicit)
2485 .addMemOperand(LoMMO);
2486 }
2487
2488 if (LPR.contains(SrcHi)) {
2489 Register StackReg = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2490 BuildMI(B, It, DL, HII.get(Hexagon::A2_addi), StackReg)
2491 .addFrameIndex(FI)
2492 .addImm(Size);
2493 BuildMI(B, It, DL, HII.get(Hexagon::LX_S16b_pseudo))
2494 .addReg(StackReg, RegState::Kill)
2495 .addReg(SrcHi, getRegState(SrcOp))
2496 .addReg(Hexagon::VSP, RegState::Implicit)
2497 .addMemOperand(HiMMO);
2498 }
2499
2500 B.erase(It);
2501 return true;
2502 }
2503
2504 unsigned Size = HRI.getSpillSize(Hexagon::HvxVRRegClass);
2505 Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
2506 Align HasAlign = MFI.getObjectAlign(FI);
2507 unsigned StoreOpc;
2508
2509 // Store low part.
2510 if (LPR.contains(SrcLo)) {
2511 StoreOpc = NeedAlign <= HasAlign ? Hexagon::V6_vS32b_ai
2512 : Hexagon::V6_vS32Ub_ai;
2513 BuildMI(B, It, DL, HII.get(StoreOpc))
2514 .addFrameIndex(FI)
2515 .addImm(0)
2516 .addReg(SrcLo, getKillRegState(IsKill))
2517 .cloneMemRefs(*MI);
2518 }
2519
2520 // Store high part.
2521 if (LPR.contains(SrcHi)) {
2522 StoreOpc = NeedAlign <= HasAlign ? Hexagon::V6_vS32b_ai
2523 : Hexagon::V6_vS32Ub_ai;
2524 BuildMI(B, It, DL, HII.get(StoreOpc))
2525 .addFrameIndex(FI)
2526 .addImm(Size)
2527 .addReg(SrcHi, getKillRegState(IsKill))
2528 .cloneMemRefs(*MI);
2529 }
2530
2531 B.erase(It);
2532 return true;
2533}
2534
2535bool HexagonFrameLowering::expandLoadVec2(MachineBasicBlock &B,
2537 MachineRegisterInfo &MRI,
2538 const HexagonInstrInfo &HII,
2539 SmallVectorImpl<Register> &NewRegs,
2540 bool IsHLX) const {
2541 MachineFunction &MF = *B.getParent();
2542 auto &MFI = MF.getFrameInfo();
2543 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
2544 MachineInstr *MI = &*It;
2545 if (!MI->getOperand(1).isFI())
2546 return false;
2547
2548 DebugLoc DL = MI->getDebugLoc();
2549 Register DstR = MI->getOperand(0).getReg();
2550 Register DstHi = HRI.getSubReg(DstR, Hexagon::vsub_hi);
2551 Register DstLo = HRI.getSubReg(DstR, Hexagon::vsub_lo);
2552 int FI = MI->getOperand(1).getIndex();
2553
2554 if (IsHLX) {
2555 const MachineOperand &DstOp = MI->getOperand(0);
2556 unsigned Size = HRI.getSpillSize(Hexagon::HlxLRRegClass);
2557 assert(MI->hasOneMemOperand() && "HLX pair reload must have one MMO");
2558 const MachineMemOperand *PairMMO = *MI->memoperands_begin();
2559 MachineMemOperand *LoMMO = MF.getMachineMemOperand(PairMMO, 0, Size);
2560 MachineMemOperand *HiMMO = MF.getMachineMemOperand(PairMMO, Size, Size);
2561
2562 BuildMI(B, It, DL, HII.get(Hexagon::LX_L16b_pseudo))
2563 .addReg(DstLo, getRegState(DstOp))
2564 .addFrameIndex(FI)
2565 .addReg(Hexagon::VSP, RegState::Implicit)
2566 .addMemOperand(LoMMO);
2567
2568 Register StackReg = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2569 BuildMI(B, It, DL, HII.get(Hexagon::A2_addi), StackReg)
2570 .addFrameIndex(FI)
2571 .addImm(Size);
2572 BuildMI(B, It, DL, HII.get(Hexagon::LX_L16b_pseudo))
2573 .addReg(DstHi, getRegState(DstOp))
2574 .addReg(StackReg, RegState::Kill)
2575 .addReg(Hexagon::VSP, RegState::Implicit)
2576 .addMemOperand(HiMMO);
2577
2578 B.erase(It);
2579 return true;
2580 }
2581
2582 unsigned Size = HRI.getSpillSize(Hexagon::HvxVRRegClass);
2583 Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
2584 Align HasAlign = MFI.getObjectAlign(FI);
2585 unsigned LoadOpc;
2586
2587 // Load low part.
2588 LoadOpc = NeedAlign <= HasAlign ? Hexagon::V6_vL32b_ai
2589 : Hexagon::V6_vL32Ub_ai;
2590 BuildMI(B, It, DL, HII.get(LoadOpc), DstLo)
2591 .addFrameIndex(FI)
2592 .addImm(0)
2593 .cloneMemRefs(*MI);
2594
2595 // Load high part.
2596 LoadOpc = NeedAlign <= HasAlign ? Hexagon::V6_vL32b_ai
2597 : Hexagon::V6_vL32Ub_ai;
2598 BuildMI(B, It, DL, HII.get(LoadOpc), DstHi)
2599 .addFrameIndex(FI)
2600 .addImm(Size)
2601 .cloneMemRefs(*MI);
2602
2603 B.erase(It);
2604 return true;
2605}
2606
2607bool HexagonFrameLowering::expandStoreVec(MachineBasicBlock &B,
2608 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
2609 const HexagonInstrInfo &HII, SmallVectorImpl<Register> &NewRegs) const {
2610 MachineFunction &MF = *B.getParent();
2611 auto &MFI = MF.getFrameInfo();
2612 MachineInstr *MI = &*It;
2613 if (!MI->getOperand(0).isFI())
2614 return false;
2615
2616 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
2617 DebugLoc DL = MI->getDebugLoc();
2618 Register SrcR = MI->getOperand(2).getReg();
2619 bool IsKill = MI->getOperand(2).isKill();
2620 int FI = MI->getOperand(0).getIndex();
2621
2622 Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
2623 Align HasAlign = MFI.getObjectAlign(FI);
2624 unsigned StoreOpc = NeedAlign <= HasAlign ? Hexagon::V6_vS32b_ai
2625 : Hexagon::V6_vS32Ub_ai;
2626 BuildMI(B, It, DL, HII.get(StoreOpc))
2627 .addFrameIndex(FI)
2628 .addImm(0)
2629 .addReg(SrcR, getKillRegState(IsKill))
2630 .cloneMemRefs(*MI);
2631
2632 B.erase(It);
2633 return true;
2634}
2635
2636bool HexagonFrameLowering::expandLoadVec(MachineBasicBlock &B,
2637 MachineBasicBlock::iterator It, MachineRegisterInfo &MRI,
2638 const HexagonInstrInfo &HII, SmallVectorImpl<Register> &NewRegs) const {
2639 MachineFunction &MF = *B.getParent();
2640 auto &MFI = MF.getFrameInfo();
2641 MachineInstr *MI = &*It;
2642 if (!MI->getOperand(1).isFI())
2643 return false;
2644
2645 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
2646 DebugLoc DL = MI->getDebugLoc();
2647 Register DstR = MI->getOperand(0).getReg();
2648 int FI = MI->getOperand(1).getIndex();
2649
2650 Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
2651 Align HasAlign = MFI.getObjectAlign(FI);
2652 unsigned LoadOpc = NeedAlign <= HasAlign ? Hexagon::V6_vL32b_ai
2653 : Hexagon::V6_vL32Ub_ai;
2654 BuildMI(B, It, DL, HII.get(LoadOpc), DstR)
2655 .addFrameIndex(FI)
2656 .addImm(0)
2657 .cloneMemRefs(*MI);
2658
2659 B.erase(It);
2660 return true;
2661}
2662
2663bool HexagonFrameLowering::expandSpillMacros(MachineFunction &MF,
2664 SmallVectorImpl<Register> &NewRegs) const {
2665 auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
2666 MachineRegisterInfo &MRI = MF.getRegInfo();
2667 bool Changed = false;
2668
2669 for (auto &B : MF) {
2670 // Traverse the basic block.
2672 for (auto I = B.begin(), E = B.end(); I != E; I = NextI) {
2673 MachineInstr *MI = &*I;
2674 NextI = std::next(I);
2675 unsigned Opc = MI->getOpcode();
2676
2677 switch (Opc) {
2678 case TargetOpcode::COPY:
2679 Changed |= expandCopy(B, I, MRI, HII, NewRegs);
2680 break;
2681 case Hexagon::STriw_pred:
2682 case Hexagon::STriw_ctr:
2683 Changed |= expandStoreInt(B, I, MRI, HII, NewRegs);
2684 break;
2685 case Hexagon::STriw_circ:
2686 Changed |= expandStoreCirc(B, I, MRI, HII, NewRegs);
2687 break;
2688 case Hexagon::LDriw_pred:
2689 case Hexagon::LDriw_ctr:
2690 Changed |= expandLoadInt(B, I, MRI, HII, NewRegs);
2691 break;
2692 case Hexagon::LDriw_circ:
2693 Changed |= expandLoadCirc(B, I, MRI, HII, NewRegs);
2694 break;
2695 case Hexagon::PS_vstorerq_ai:
2696 Changed |= expandStoreVecPred(B, I, MRI, HII, NewRegs);
2697 break;
2698 case Hexagon::PS_vloadrq_ai:
2699 Changed |= expandLoadVecPred(B, I, MRI, HII, NewRegs);
2700 break;
2701 case Hexagon::PS_vloadrw_ai:
2702 Changed |= expandLoadVec2(B, I, MRI, HII, NewRegs);
2703 break;
2704 case Hexagon::PS_vstorerw_ai:
2705 Changed |= expandStoreVec2(B, I, MRI, HII, NewRegs);
2706 break;
2707 case Hexagon::PS_loadrll:
2708 Changed |= expandLoadVec2(B, I, MRI, HII, NewRegs, /*IsHLX=*/true);
2709 break;
2710 case Hexagon::PS_storerll:
2711 Changed |= expandStoreVec2(B, I, MRI, HII, NewRegs, /*IsHLX=*/true);
2712 break;
2713 }
2714 }
2715 }
2716
2717 return Changed;
2718}
2719
2721 BitVector &SavedRegs,
2722 RegScavenger *RS) const {
2723 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
2724
2725 SavedRegs.resize(HRI.getNumRegs());
2726
2727 // If we have a function containing __builtin_eh_return we want to spill and
2728 // restore all callee saved registers. Pretend that they are used.
2730 for (const MCPhysReg *R = HRI.getCalleeSavedRegs(&MF); *R; ++R)
2731 SavedRegs.set(*R);
2732
2733 // If the function needs dynamic stack realignment, AP is a callee-saved
2734 // register that gets overwritten by the PS_aligna emitted in the prologue
2735 // but PS_aligna is created during emitPrologue, which runs after this hook.
2736 if (needsAligna(MF)) {
2737 Register AP =
2738 MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
2739 assert(AP.isValid() && "AP must be assigned before register allocation");
2740 SavedRegs.set(AP);
2741 }
2742
2743 // Replace predicate register pseudo spill code.
2745 expandSpillMacros(MF, NewRegs);
2746 if (OptimizeSpillSlots && !isOptNone(MF))
2747 optimizeSpillSlots(MF, NewRegs);
2748
2749 // We need to reserve a spill slot if scavenging could potentially require
2750 // spilling a scavenged register.
2751 if (!NewRegs.empty() || mayOverflowFrameOffset(MF)) {
2752 MachineFrameInfo &MFI = MF.getFrameInfo();
2755 // Reserve an int register in any case, because it could be used to hold
2756 // the stack offset in case it does not fit into a spill instruction.
2757 SpillRCs.insert(&Hexagon::IntRegsRegClass);
2758
2759 for (Register VR : NewRegs)
2760 SpillRCs.insert(MRI.getRegClass(VR));
2761
2762 for (const auto *RC : SpillRCs) {
2763 if (!needToReserveScavengingSpillSlots(MF, HRI, RC))
2764 continue;
2765 unsigned Num = 1;
2766 switch (RC->getID()) {
2767 case Hexagon::IntRegsRegClassID:
2769 break;
2770 case Hexagon::HvxQRRegClassID:
2771 Num = 2; // Vector predicate spills also need a vector register.
2772 break;
2773 }
2774 unsigned S = HRI.getSpillSize(*RC);
2775 Align A = HRI.getSpillAlign(*RC);
2776 for (unsigned i = 0; i < Num; i++) {
2777 int NewFI = MFI.CreateSpillStackObject(S, A);
2778 RS->addScavengingFrameIndex(NewFI);
2779 }
2780 }
2781 }
2782
2784}
2785
2786Register HexagonFrameLowering::findPhysReg(MachineFunction &MF,
2790 const TargetRegisterClass *RC) const {
2791 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
2792 auto &MRI = MF.getRegInfo();
2793
2794 auto isDead = [&FIR,&DeadMap] (Register Reg) -> bool {
2795 auto F = DeadMap.find({Reg,0});
2796 if (F == DeadMap.end())
2797 return false;
2798 for (auto &DR : F->second)
2799 if (DR.contains(FIR))
2800 return true;
2801 return false;
2802 };
2803
2804 for (Register Reg : HRI.getRawAllocationOrder(*RC, MF)) {
2805 bool Dead = true;
2806 for (auto R : HexagonBlockRanges::expandToSubRegs({Reg,0}, MRI, HRI)) {
2807 if (isDead(R.Reg))
2808 continue;
2809 Dead = false;
2810 break;
2811 }
2812 if (Dead)
2813 return Reg;
2814 }
2815 return 0;
2816}
2817
2818void HexagonFrameLowering::optimizeSpillSlots(MachineFunction &MF,
2819 SmallVectorImpl<Register> &VRegs) const {
2820 auto &HST = MF.getSubtarget<HexagonSubtarget>();
2821 auto &HII = *HST.getInstrInfo();
2822 auto &HRI = *HST.getRegisterInfo();
2823 auto &MRI = MF.getRegInfo();
2824 HexagonBlockRanges HBR(MF);
2825
2826 using BlockIndexMap =
2827 std::map<MachineBasicBlock *, HexagonBlockRanges::InstrIndexMap>;
2828 using BlockRangeMap =
2829 std::map<MachineBasicBlock *, HexagonBlockRanges::RangeList>;
2830 using IndexType = HexagonBlockRanges::IndexType;
2831
2832 struct SlotInfo {
2833 BlockRangeMap Map;
2834 unsigned Size = 0;
2835 const TargetRegisterClass *RC = nullptr;
2836
2837 SlotInfo() = default;
2838 };
2839
2840 BlockIndexMap BlockIndexes;
2841 SmallSet<int,4> BadFIs;
2842 std::map<int,SlotInfo> FIRangeMap;
2843
2844 // Accumulate register classes: get a common class for a pre-existing
2845 // class HaveRC and a new class NewRC. Return nullptr if a common class
2846 // cannot be found, otherwise return the resulting class. If HaveRC is
2847 // nullptr, assume that it is still unset.
2848 auto getCommonRC =
2849 [](const TargetRegisterClass *HaveRC,
2850 const TargetRegisterClass *NewRC) -> const TargetRegisterClass * {
2851 if (HaveRC == nullptr || HaveRC == NewRC)
2852 return NewRC;
2853 // Different classes, both non-null. Pick the more general one.
2854 if (HaveRC->hasSubClassEq(NewRC))
2855 return HaveRC;
2856 if (NewRC->hasSubClassEq(HaveRC))
2857 return NewRC;
2858 return nullptr;
2859 };
2860
2861 // Scan all blocks in the function. Check all occurrences of frame indexes,
2862 // and collect relevant information.
2863 for (auto &B : MF) {
2864 std::map<int,IndexType> LastStore, LastLoad;
2865 auto P = BlockIndexes.emplace(&B, HexagonBlockRanges::InstrIndexMap(B));
2866 auto &IndexMap = P.first->second;
2867 LLVM_DEBUG(dbgs() << "Index map for " << printMBBReference(B) << "\n"
2868 << IndexMap << '\n');
2869
2870 for (auto &In : B) {
2871 // Debug instructions do not generate any code, and their operands
2872 // (including frame index operands) must not affect the decisions made
2873 // by this optimization.
2874 if (In.isDebugInstr())
2875 continue;
2876 int LFI, SFI;
2877 bool Load = HII.isLoadFromStackSlot(In, LFI) && !HII.isPredicated(In);
2878 bool Store = HII.isStoreToStackSlot(In, SFI) && !HII.isPredicated(In);
2879 if (Load && Store) {
2880 // If it's both a load and a store, then we won't handle it.
2881 BadFIs.insert(LFI);
2882 BadFIs.insert(SFI);
2883 continue;
2884 }
2885 // Check for register classes of the register used as the source for
2886 // the store, and the register used as the destination for the load.
2887 // Also, only accept base+imm_offset addressing modes. Other addressing
2888 // modes can have side-effects (post-increments, etc.). For stack
2889 // slots they are very unlikely, so there is not much loss due to
2890 // this restriction.
2891 if (Load || Store) {
2892 int TFI = Load ? LFI : SFI;
2893 unsigned AM = HII.getAddrMode(In);
2894 SlotInfo &SI = FIRangeMap[TFI];
2895 bool Bad = (AM != HexagonII::BaseImmOffset);
2896 if (!Bad) {
2897 // If the addressing mode is ok, check the register class.
2898 unsigned OpNum = Load ? 0 : 2;
2899 auto *RC = HII.getRegClass(In.getDesc(), OpNum);
2900 RC = getCommonRC(SI.RC, RC);
2901 if (RC == nullptr)
2902 Bad = true;
2903 else
2904 SI.RC = RC;
2905 }
2906 if (!Bad) {
2907 // Check sizes.
2908 unsigned S = HII.getMemAccessSize(In);
2909 if (SI.Size != 0 && SI.Size != S)
2910 Bad = true;
2911 else
2912 SI.Size = S;
2913 }
2914 if (!Bad) {
2915 for (auto *Mo : In.memoperands()) {
2916 if (!Mo->isVolatile() && !Mo->isAtomic())
2917 continue;
2918 Bad = true;
2919 break;
2920 }
2921 }
2922 if (Bad)
2923 BadFIs.insert(TFI);
2924 }
2925
2926 // Locate uses of frame indices.
2927 for (unsigned i = 0, n = In.getNumOperands(); i < n; ++i) {
2928 const MachineOperand &Op = In.getOperand(i);
2929 if (!Op.isFI())
2930 continue;
2931 int FI = Op.getIndex();
2932 // Make sure that the following operand is an immediate and that
2933 // it is 0. This is the offset in the stack object.
2934 if (i+1 >= n || !In.getOperand(i+1).isImm() ||
2935 In.getOperand(i+1).getImm() != 0)
2936 BadFIs.insert(FI);
2937 if (BadFIs.count(FI))
2938 continue;
2939
2940 IndexType Index = IndexMap.getIndex(&In);
2941 auto &LS = LastStore[FI];
2942 auto &LL = LastLoad[FI];
2943 if (Load) {
2944 if (LS == IndexType::None)
2945 LS = IndexType::Entry;
2946 LL = Index;
2947 } else if (Store) {
2948 HexagonBlockRanges::RangeList &RL = FIRangeMap[FI].Map[&B];
2949 if (LS != IndexType::None)
2950 RL.add(LS, LL, false, false);
2951 else if (LL != IndexType::None)
2952 RL.add(IndexType::Entry, LL, false, false);
2953 LL = IndexType::None;
2954 LS = Index;
2955 } else {
2956 BadFIs.insert(FI);
2957 }
2958 }
2959 }
2960
2961 for (auto &I : LastLoad) {
2962 IndexType LL = I.second;
2963 if (LL == IndexType::None)
2964 continue;
2965 auto &RL = FIRangeMap[I.first].Map[&B];
2966 IndexType &LS = LastStore[I.first];
2967 if (LS != IndexType::None)
2968 RL.add(LS, LL, false, false);
2969 else
2970 RL.add(IndexType::Entry, LL, false, false);
2971 LS = IndexType::None;
2972 }
2973 for (auto &I : LastStore) {
2974 IndexType LS = I.second;
2975 if (LS == IndexType::None)
2976 continue;
2977 auto &RL = FIRangeMap[I.first].Map[&B];
2978 RL.add(LS, IndexType::None, false, false);
2979 }
2980 }
2981
2982 LLVM_DEBUG({
2983 for (auto &P : FIRangeMap) {
2984 dbgs() << "fi#" << P.first;
2985 if (BadFIs.count(P.first))
2986 dbgs() << " (bad)";
2987 dbgs() << " RC: ";
2988 if (P.second.RC != nullptr)
2989 dbgs() << HRI.getRegClassName(P.second.RC) << '\n';
2990 else
2991 dbgs() << "<null>\n";
2992 for (auto &R : P.second.Map)
2993 dbgs() << " " << printMBBReference(*R.first) << " { " << R.second
2994 << "}\n";
2995 }
2996 });
2997
2998 // When a slot is loaded from in a block without being stored to in the
2999 // same block, it is live-on-entry to this block. To avoid CFG analysis,
3000 // consider this slot to be live-on-exit from all blocks.
3001 SmallSet<int,4> LoxFIs;
3002
3003 std::map<MachineBasicBlock*,std::vector<int>> BlockFIMap;
3004
3005 for (auto &P : FIRangeMap) {
3006 // P = pair(FI, map: BB->RangeList)
3007 if (BadFIs.count(P.first))
3008 continue;
3009 for (auto &B : MF) {
3010 auto F = P.second.Map.find(&B);
3011 // F = pair(BB, RangeList)
3012 if (F == P.second.Map.end() || F->second.empty())
3013 continue;
3014 HexagonBlockRanges::IndexRange &IR = F->second.front();
3015 if (IR.start() == IndexType::Entry)
3016 LoxFIs.insert(P.first);
3017 BlockFIMap[&B].push_back(P.first);
3018 }
3019 }
3020
3021 LLVM_DEBUG({
3022 dbgs() << "Block-to-FI map (* -- live-on-exit):\n";
3023 for (auto &P : BlockFIMap) {
3024 auto &FIs = P.second;
3025 if (FIs.empty())
3026 continue;
3027 dbgs() << " " << printMBBReference(*P.first) << ": {";
3028 for (auto I : FIs) {
3029 dbgs() << " fi#" << I;
3030 if (LoxFIs.count(I))
3031 dbgs() << '*';
3032 }
3033 dbgs() << " }\n";
3034 }
3035 });
3036
3037#ifndef NDEBUG
3038 bool HasOptLimit = SpillOptMax.getPosition();
3039#endif
3040
3041 // eliminate loads, when all loads eliminated, eliminate all stores.
3042 for (auto &B : MF) {
3043 auto F = BlockIndexes.find(&B);
3044 assert(F != BlockIndexes.end());
3045 HexagonBlockRanges::InstrIndexMap &IM = F->second;
3046 HexagonBlockRanges::RegToRangeMap LM = HBR.computeLiveMap(IM);
3047 HexagonBlockRanges::RegToRangeMap DM = HBR.computeDeadMap(IM, LM);
3048 LLVM_DEBUG(dbgs() << printMBBReference(B) << " dead map\n"
3049 << HexagonBlockRanges::PrintRangeMap(DM, HRI));
3050
3051 for (auto FI : BlockFIMap[&B]) {
3052 if (BadFIs.count(FI))
3053 continue;
3054 LLVM_DEBUG(dbgs() << "Working on fi#" << FI << '\n');
3055 HexagonBlockRanges::RangeList &RL = FIRangeMap[FI].Map[&B];
3056 for (auto &Range : RL) {
3057 LLVM_DEBUG(dbgs() << "--Examining range:" << RL << '\n');
3058 if (!IndexType::isInstr(Range.start()) ||
3059 !IndexType::isInstr(Range.end()))
3060 continue;
3061 MachineInstr &SI = *IM.getInstr(Range.start());
3062 MachineInstr &EI = *IM.getInstr(Range.end());
3063 assert(SI.mayStore() && "Unexpected start instruction");
3064 assert(EI.mayLoad() && "Unexpected end instruction");
3065 MachineOperand &SrcOp = SI.getOperand(2);
3066
3067 HexagonBlockRanges::RegisterRef SrcRR = { SrcOp.getReg(),
3068 SrcOp.getSubReg() };
3069 auto *RC = HII.getRegClass(SI.getDesc(), 2);
3070 // The this-> is needed to unconfuse MSVC.
3071 Register FoundR = this->findPhysReg(MF, Range, IM, DM, RC);
3072 LLVM_DEBUG(dbgs() << "Replacement reg:" << printReg(FoundR, &HRI)
3073 << '\n');
3074 if (FoundR == 0)
3075 continue;
3076#ifndef NDEBUG
3077 if (HasOptLimit) {
3079 return;
3080 SpillOptCount++;
3081 }
3082#endif
3083
3084 // Generate the copy-in: "FoundR = COPY SrcR" at the store location.
3085 MachineBasicBlock::iterator StartIt = SI.getIterator(), NextIt;
3086 MachineInstr *CopyIn = nullptr;
3087 if (SrcRR.Reg != FoundR || SrcRR.Sub != 0) {
3088 const DebugLoc &DL = SI.getDebugLoc();
3089 CopyIn = BuildMI(B, StartIt, DL, HII.get(TargetOpcode::COPY), FoundR)
3090 .add(SrcOp);
3091 }
3092
3093 ++StartIt;
3094 // Check if this is a last store and the FI is live-on-exit.
3095 if (LoxFIs.count(FI) && (&Range == &RL.back())) {
3096 // Update store's source register.
3097 if (unsigned SR = SrcOp.getSubReg())
3098 SrcOp.setReg(HRI.getSubReg(FoundR, SR));
3099 else
3100 SrcOp.setReg(FoundR);
3101 SrcOp.setSubReg(0);
3102 // We are keeping this register live.
3103 SrcOp.setIsKill(false);
3104 } else {
3105 B.erase(&SI);
3106 IM.replaceInstr(&SI, CopyIn);
3107 }
3108
3109 auto EndIt = std::next(EI.getIterator());
3110 for (auto It = StartIt; It != EndIt; It = NextIt) {
3111 MachineInstr &MI = *It;
3112 NextIt = std::next(It);
3113 int TFI;
3114 if (!HII.isLoadFromStackSlot(MI, TFI) || TFI != FI)
3115 continue;
3116 Register DstR = MI.getOperand(0).getReg();
3117 assert(MI.getOperand(0).getSubReg() == 0);
3118 MachineInstr *CopyOut = nullptr;
3119 if (DstR != FoundR) {
3120 DebugLoc DL = MI.getDebugLoc();
3121 unsigned MemSize = HII.getMemAccessSize(MI);
3122 assert(HII.getAddrMode(MI) == HexagonII::BaseImmOffset);
3123 unsigned CopyOpc = TargetOpcode::COPY;
3124 if (HII.isSignExtendingLoad(MI))
3125 CopyOpc = (MemSize == 1) ? Hexagon::A2_sxtb : Hexagon::A2_sxth;
3126 else if (HII.isZeroExtendingLoad(MI))
3127 CopyOpc = (MemSize == 1) ? Hexagon::A2_zxtb : Hexagon::A2_zxth;
3128 CopyOut = BuildMI(B, It, DL, HII.get(CopyOpc), DstR)
3129 .addReg(FoundR, getKillRegState(&MI == &EI));
3130 }
3131 IM.replaceInstr(&MI, CopyOut);
3132 B.erase(It);
3133 }
3134
3135 // Update the dead map.
3136 HexagonBlockRanges::RegisterRef FoundRR = { FoundR, 0 };
3137 for (auto RR : HexagonBlockRanges::expandToSubRegs(FoundRR, MRI, HRI))
3138 DM[RR].subtract(Range);
3139 } // for Range in range list
3140 }
3141 }
3142}
3143
3144void HexagonFrameLowering::expandAlloca(MachineInstr *AI, MachineFunction &MF,
3145 const HexagonInstrInfo &HII,
3146 Register SP, unsigned CF) const {
3147 MachineBasicBlock &MB = *AI->getParent();
3148 DebugLoc DL = AI->getDebugLoc();
3149 unsigned A = AI->getOperand(2).getImm();
3150
3151 MachineOperand &RdOp = AI->getOperand(0);
3152 MachineOperand &RsOp = AI->getOperand(1);
3153 Register Rd = RdOp.getReg(), Rs = RsOp.getReg();
3154
3155 auto &HST = MF.getSubtarget<HexagonSubtarget>();
3156 auto *TLI = HST.getTargetLowering();
3157 bool NeedsProbing = TLI->hasInlineStackProbe(MF);
3158
3159 if (!NeedsProbing) {
3160 // Have
3161 // Rd = alloca Rs, #A
3162 //
3163 // If Rs and Rd are different registers, use this sequence:
3164 // Rd = sub(r29, Rs)
3165 // r29 = sub(r29, Rs)
3166 // Rd = and(Rd, #-A) ; if necessary
3167 // r29 = and(r29, #-A) ; if necessary
3168 // Rd = add(Rd, #CF) ; CF size aligned to at most A
3169 // otherwise, do
3170 // Rd = sub(r29, Rs)
3171 // Rd = and(Rd, #-A) ; if necessary
3172 // r29 = Rd
3173 // Rd = add(Rd, #CF) ; CF size aligned to at most A
3174
3175 // Rd = sub(r29, Rs)
3176 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), Rd).addReg(SP).addReg(Rs);
3177 if (Rs != Rd) {
3178 // r29 = sub(r29, Rs)
3179 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), SP).addReg(SP).addReg(Rs);
3180 }
3181 if (A > 8) {
3182 // Rd = and(Rd, #-A)
3183 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), Rd)
3184 .addReg(Rd)
3185 .addImm(-int64_t(A));
3186 if (Rs != Rd)
3187 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), SP)
3188 .addReg(SP)
3189 .addImm(-int64_t(A));
3190 }
3191 if (Rs == Rd) {
3192 // r29 = Rd
3193 BuildMI(MB, AI, DL, HII.get(TargetOpcode::COPY), SP).addReg(Rd);
3194 }
3195 if (CF > 0) {
3196 // Rd = add(Rd, #CF)
3197 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_addi), Rd).addReg(Rd).addImm(CF);
3198 }
3199 return;
3200 }
3201
3202 // Stack probing for dynamic allocation. The size Rs is a runtime value
3203 // so the probe loop is always emitted; it is a no-op when Rs is small.
3204 //
3205 // Compute the target SP into Rd (with optional alignment), then probe
3206 // each page on the way down:
3207 //
3208 // Rd = sub(r29, Rs)
3209 // [Rd = and(Rd, #-A)] ; if alignment > 8
3210 // LoopMBB:
3211 // r29 = add(r29, #-ProbeSize)
3212 // memw(r29+#0) = #0
3213 // p0 = cmp.gtu(r29, Rd)
3214 // if (p0.new) jump:t LoopMBB
3215 // ExitMBB:
3216 // r29 = Rd
3217 // [Rd = add(Rd, #CF)] ; if CF > 0
3218 // <rest of original block>
3219 //
3220 // Rd holds the exact (aligned) target SP throughout the loop, so the
3221 // final "r29 = Rd" snaps SP to the correct value even when Rs is not
3222 // a multiple of ProbeSize.
3223
3224 Align StackAlign = getStackAlign();
3225 unsigned ProbeSize = TLI->getStackProbeSize(MF, StackAlign);
3227
3228 // Emit target-SP computation into Rd before splitting the block.
3229 // Rd = sub(r29, Rs)
3230 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_sub), Rd)
3231 .addReg(SP)
3232 .addReg(Rs)
3233 .setMIFlags(Flags);
3234 if (A > 8) {
3235 // Rd = and(Rd, #-A)
3236 BuildMI(MB, AI, DL, HII.get(Hexagon::A2_andir), Rd)
3237 .addReg(Rd)
3238 .addImm(-int64_t(A))
3239 .setMIFlags(Flags);
3240 }
3241
3242 // Split the block: everything after AI goes into ExitMBB.
3243 MachineFunction::iterator InsertPt = std::next(MB.getIterator());
3244 MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(MB.getBasicBlock());
3245 MF.insert(InsertPt, LoopMBB);
3246 MachineBasicBlock *ExitMBB = MF.CreateMachineBasicBlock(MB.getBasicBlock());
3247 MF.insert(InsertPt, ExitMBB);
3248
3249 // Move instructions after AI (exclusive) into ExitMBB.
3250 ExitMBB->splice(ExitMBB->end(), &MB, std::next(AI->getIterator()), MB.end());
3251 ExitMBB->transferSuccessorsAndUpdatePHIs(&MB);
3252
3253 // LoopMBB: probe each page.
3254 // r29 = add(r29, #-ProbeSize)
3255 // memw(r29+#0) = #0
3256 // p0 = cmp.gtu(r29, Rd)
3257 // if (p0.new) jump:t LoopMBB
3258 BuildMI(*LoopMBB, LoopMBB->end(), DL, HII.get(Hexagon::A2_addi), Hexagon::R29)
3259 .addReg(Hexagon::R29)
3260 .addImm(-int(ProbeSize))
3261 .setMIFlags(Flags);
3262
3263 BuildMI(*LoopMBB, LoopMBB->end(), DL, HII.get(Hexagon::S4_storeiri_io))
3264 .addReg(Hexagon::R29)
3265 .addImm(0)
3266 .addImm(0)
3267 .setMIFlags(Flags);
3268
3269 BuildMI(*LoopMBB, LoopMBB->end(), DL, HII.get(Hexagon::C2_cmpgtu),
3270 Hexagon::P0)
3271 .addReg(Hexagon::R29)
3272 .addReg(Rd)
3273 .setMIFlags(Flags);
3274
3275 BuildMI(*LoopMBB, LoopMBB->end(), DL, HII.get(Hexagon::J2_jumpt))
3276 .addReg(Hexagon::P0)
3277 .addMBB(LoopMBB)
3278 .setMIFlags(Flags);
3279
3280 // ExitMBB: snap SP to exact target, then apply CF offset to Rd.
3281 // r29 = Rd
3282 // [Rd = add(Rd, #CF)]
3283 MachineBasicBlock::iterator ExitIt = ExitMBB->begin();
3284 BuildMI(*ExitMBB, ExitIt, DL, HII.get(Hexagon::A2_tfr), Hexagon::R29)
3285 .addReg(Rd)
3286 .setMIFlags(Flags);
3287 if (CF > 0) {
3288 BuildMI(*ExitMBB, ExitIt, DL, HII.get(Hexagon::A2_addi), Rd)
3289 .addReg(Rd)
3290 .addImm(CF)
3291 .setMIFlags(Flags);
3292 }
3293
3294 // Wire up CFG edges.
3295 MB.addSuccessor(LoopMBB);
3296 LoopMBB->addSuccessor(LoopMBB);
3297 LoopMBB->addSuccessor(ExitMBB);
3298
3299 // Recompute live-ins for the new blocks. AI is still in MB at this
3300 // point; the caller erases it after expandAlloca returns.
3301 fullyRecomputeLiveIns({ExitMBB, LoopMBB});
3302}
3303
3305 const MachineFrameInfo &MFI = MF.getFrameInfo();
3306 if (!MFI.hasVarSizedObjects())
3307 return false;
3308 // Do not check for max stack object alignment here, because the stack
3309 // may not be complete yet. Assume that we will need PS_aligna if there
3310 // are variable-sized objects.
3311 return true;
3312}
3313
3314/// Adds all callee-saved registers as implicit uses or defs to the
3315/// instruction.
3316void HexagonFrameLowering::addCalleeSaveRegistersAsImpOperand(MachineInstr *MI,
3317 const CSIVect &CSI, bool IsDef, bool IsKill) const {
3318 // Add the callee-saved registers as implicit uses.
3319 for (auto &R : CSI)
3320 MI->addOperand(MachineOperand::CreateReg(R.getReg(), IsDef, true, IsKill));
3321}
3322
3323/// Determine whether the callee-saved register saves and restores should
3324/// be generated via inline code. If this function returns "true", inline
3325/// code will be generated. If this function returns "false", additional
3326/// checks are performed, which may still lead to the inline code.
3327bool HexagonFrameLowering::shouldInlineCSR(const MachineFunction &MF,
3328 const CSIVect &CSI) const {
3330 return true;
3332 return true;
3333 if (!hasFP(MF))
3334 return true;
3335 if (!isOptSize(MF) && !isMinSize(MF))
3337 return true;
3338
3339 // Check if CSI only has double registers, and if the registers form
3340 // a contiguous block starting from D8.
3341 BitVector Regs(Hexagon::NUM_TARGET_REGS);
3342 for (const CalleeSavedInfo &I : CSI) {
3343 MCRegister R = I.getReg();
3344 if (!Hexagon::DoubleRegsRegClass.contains(R))
3345 return true;
3346 Regs[R] = true;
3347 }
3348 int F = Regs.find_first();
3349 if (F != Hexagon::D8)
3350 return true;
3351 while (F >= 0) {
3352 int N = Regs.find_next(F);
3353 if (N >= 0 && N != F+1)
3354 return true;
3355 F = N;
3356 }
3357
3358 return false;
3359}
3360
3361bool HexagonFrameLowering::useSpillFunction(const MachineFunction &MF,
3362 const CSIVect &CSI) const {
3363 if (shouldInlineCSR(MF, CSI))
3364 return false;
3365 unsigned NumCSI = CSI.size();
3366 if (NumCSI <= 1)
3367 return false;
3368
3369 // Every spill stub saves the whole range starting at R16
3370 // (__save_r16_through_rNN), so a stub whose range reached the shadow call
3371 // stack pointer register would spill it along with the real callee-saved
3372 // registers - and since the SCS register is reserved it is absent from CSI,
3373 // so the stub's fixed frame layout would not match the one the compiler
3374 // assigned.
3375 //
3376 // shouldInlineCSR() above already makes this unreachable: it only lets a
3377 // stub through when CSI is a contiguous run of double registers starting at
3378 // D8, and reserving the SCS register always breaks the double it belongs
3379 // to, leaving its partner in CSI as a lone single register. This is a
3380 // cheap safety net so the guarantee does not rest on that reasoning alone.
3381 if (MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)) {
3382 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
3383 Register MaxReg = getMaxCalleeSavedReg(CSI, *HST.getRegisterInfo());
3384 if (HST.getSCSPReg().id() <= MaxReg.id())
3385 return false;
3386 }
3387
3388 unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs
3390 return Threshold < NumCSI;
3391}
3392
3393bool HexagonFrameLowering::useRestoreFunction(const MachineFunction &MF,
3394 const CSIVect &CSI) const {
3395 if (shouldInlineCSR(MF, CSI))
3396 return false;
3397 // The returning restore stubs do jumpr r31, this breaks ShadowCallStack:
3398 if (MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
3399 return false;
3400 // The restore functions do a bit more than just restoring registers.
3401 // The non-returning versions will go back directly to the caller's
3402 // caller, others will clean up the stack frame in preparation for
3403 // a tail call. Using them can still save code size even if only one
3404 // register is getting restores. Make the decision based on -Oz:
3405 // using -Os will use inline restore for a single register.
3406 if (isMinSize(MF))
3407 return true;
3408 unsigned NumCSI = CSI.size();
3409 if (NumCSI <= 1)
3410 return false;
3411
3412 unsigned Threshold = isOptSize(MF) ? SpillFuncThresholdOs-1
3414 return Threshold < NumCSI;
3415}
3416
3417bool HexagonFrameLowering::mayOverflowFrameOffset(MachineFunction &MF) const {
3418 unsigned StackSize = MF.getFrameInfo().estimateStackSize(MF);
3419 auto &HST = MF.getSubtarget<HexagonSubtarget>();
3420 // A fairly simplistic guess as to whether a potential load/store to a
3421 // stack location could require an extra register.
3422 if (HST.useHVXOps() && StackSize > 256)
3423 return true;
3424
3425 // Check if the function has store-immediate instructions that access
3426 // the stack. Since the offset field is not extendable, if the stack
3427 // size exceeds the offset limit (6 bits, shifted), the stores will
3428 // require a new base register.
3429 bool HasImmStack = false;
3430 unsigned MinLS = ~0u; // Log_2 of the memory access size.
3431
3432 for (const MachineBasicBlock &B : MF) {
3433 for (const MachineInstr &MI : B) {
3434 unsigned LS = 0;
3435 switch (MI.getOpcode()) {
3436 case Hexagon::S4_storeirit_io:
3437 case Hexagon::S4_storeirif_io:
3438 case Hexagon::S4_storeiri_io:
3439 ++LS;
3440 [[fallthrough]];
3441 case Hexagon::S4_storeirht_io:
3442 case Hexagon::S4_storeirhf_io:
3443 case Hexagon::S4_storeirh_io:
3444 ++LS;
3445 [[fallthrough]];
3446 case Hexagon::S4_storeirbt_io:
3447 case Hexagon::S4_storeirbf_io:
3448 case Hexagon::S4_storeirb_io:
3449 if (MI.getOperand(0).isFI())
3450 HasImmStack = true;
3451 MinLS = std::min(MinLS, LS);
3452 break;
3453 }
3454 }
3455 }
3456
3457 if (HasImmStack)
3458 return !isUInt<6>(StackSize >> MinLS);
3459
3460 return false;
3461}
3462
3463namespace {
3464// Struct used by orderFrameObjects to help sort the stack objects.
3465struct HexagonFrameSortingObject {
3466 bool IsValid = false;
3467 unsigned Index = 0; // Index of Object into MFI list.
3468 unsigned Size = 0;
3469 Align ObjectAlignment = Align(1); // Alignment of Object in bytes.
3470};
3471
3472struct HexagonFrameSortingComparator {
3473 inline bool operator()(const HexagonFrameSortingObject &A,
3474 const HexagonFrameSortingObject &B) const {
3475 return std::make_tuple(!A.IsValid, A.ObjectAlignment, A.Size) <
3476 std::make_tuple(!B.IsValid, B.ObjectAlignment, B.Size);
3477 }
3478};
3479} // namespace
3480
3481// Sort objects on the stack by alignment value and then by size to minimize
3482// padding.
3484 const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const {
3485
3486 if (ObjectsToAllocate.empty())
3487 return;
3488
3489 const MachineFrameInfo &MFI = MF.getFrameInfo();
3490 int NObjects = ObjectsToAllocate.size();
3491
3492 // Create an array of all MFI objects.
3494 MFI.getObjectIndexEnd());
3495
3496 for (int i = 0, j = 0, e = MFI.getObjectIndexEnd(); i < e && j != NObjects;
3497 ++i) {
3498 if (i != ObjectsToAllocate[j])
3499 continue;
3500 j++;
3501
3502 // A variable size object has size equal to 0. Since Hexagon sets
3503 // getUseLocalStackAllocationBlock() to true, a local block is allocated
3504 // earlier. This case is not handled here for now.
3505 int Size = MFI.getObjectSize(i);
3506 if (Size == 0)
3507 return;
3508
3509 SortingObjects[i].IsValid = true;
3510 SortingObjects[i].Index = i;
3511 SortingObjects[i].Size = Size;
3512 SortingObjects[i].ObjectAlignment = MFI.getObjectAlign(i);
3513 }
3514
3515 // Sort objects by alignment and then by size.
3516 llvm::stable_sort(SortingObjects, HexagonFrameSortingComparator());
3517
3518 // Modify the original list to represent the final order.
3519 int i = NObjects;
3520 for (auto &Obj : SortingObjects) {
3521 if (i == 0)
3522 break;
3523 ObjectsToAllocate[--i] = Obj.Index;
3524 }
3525}
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
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< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static RegisterPass< DebugifyModulePass > DM("debugify", "Attach debug info to everything")
This file defines the DenseMap class.
This file contains constants used for implementing Dwarf debug support.
static MachineInstr * getReturn(MachineBasicBlock &MBB)
Returns the "return" instruction from this block, or nullptr if there isn't any.
static cl::opt< unsigned > ShrinkLimit("shrink-frame-limit", cl::init(std::numeric_limits< unsigned >::max()), cl::Hidden, cl::desc("Max count of stack frame shrink-wraps"))
static bool isOptNone(const MachineFunction &MF)
static cl::opt< int > SpillFuncThreshold("spill-func-threshold", cl::Hidden, cl::desc("Specify O2(not Os) spill func threshold"), cl::init(6))
static std::optional< MachineBasicBlock::iterator > findCFILocation(MachineBasicBlock &B)
static cl::opt< bool > EliminateFramePointer("hexagon-fp-elim", cl::init(true), cl::Hidden, cl::desc("Refrain from using FP whenever possible"))
static bool enableAllocFrameElim(const MachineFunction &MF)
static const char * getSpillFunctionFor(Register MaxReg, SpillKind SpillType, bool Stkchk=false)
static bool hasReturn(const MachineBasicBlock &MBB)
Returns true if MBB contains an instruction that returns.
static cl::opt< bool > EnableSaveRestoreLong("enable-save-restore-long", cl::Hidden, cl::desc("Enable long calls for save-restore stubs."), cl::init(false))
static bool needToReserveScavengingSpillSlots(MachineFunction &MF, const HexagonRegisterInfo &HRI, const TargetRegisterClass *RC)
Returns true if there are no caller-saved registers available in class RC.
static bool isOptSize(const MachineFunction &MF)
static Register getMax32BitSubRegister(Register Reg, const TargetRegisterInfo &TRI, bool hireg=true)
Map a register pair Reg to the subregister that has the greater "number", i.e.
static cl::opt< int > SpillFuncThresholdOs("spill-func-threshold-Os", cl::Hidden, cl::desc("Specify Os spill func threshold"), cl::init(1))
static bool needsStackFrame(const MachineBasicBlock &MBB, const BitVector &CSR, const HexagonRegisterInfo &HRI)
Checks if the basic block contains any instruction that needs a stack frame to be already in place.
static cl::opt< unsigned > HLXVectorStackLimit("hlx-vector-stack-limit", cl::Hidden, cl::init(std::numeric_limits< unsigned >::max()), cl::desc("Maximum HLX vector (VTCM) stack frame size in bytes"))
static cl::opt< bool > DisableDeallocRet("disable-hexagon-dealloc-ret", cl::Hidden, cl::desc("Disable Dealloc Return for Hexagon target"))
static cl::opt< bool > EnableShrinkWrapping("hexagon-shrink-frame", cl::init(true), cl::Hidden, cl::desc("Enable stack frame shrink wrapping"))
static bool hasTailCall(const MachineBasicBlock &MBB)
Returns true if MBB has a machine instructions that indicates a tail call in the block.
static cl::opt< unsigned > NumberScavengerSlots("number-scavenger-slots", cl::Hidden, cl::desc("Set the number of scavenger slots"), cl::init(2))
static Register getMaxCalleeSavedReg(ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo &TRI)
Returns the callee saved register with the largest id in the vector.
static bool isMinSize(const MachineFunction &MF)
static cl::opt< unsigned > SpillOptMax("spill-opt-max", cl::Hidden, cl::init(std::numeric_limits< unsigned >::max()))
static bool containsHLXObject(const llvm::HexagonSubtarget &HST, llvm::Type *Ty)
static unsigned SpillOptCount
static void dump_registers(BitVector &Regs, const TargetRegisterInfo &TRI)
static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
static bool isRestoreCall(unsigned Opc)
static cl::opt< bool > OptimizeSpillSlots("hexagon-opt-spill", cl::Hidden, cl::init(true), cl::desc("Optimize spill slots"))
static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL)
static cl::opt< bool > EnableStackOVFSanitizer("enable-stackovf-sanitizer", cl::Hidden, cl::desc("Enable runtime checks for stack overflow."), cl::init(false))
static cl::opt< bool > SaveParentVSP("hexagon-save-parent-vsp", cl::Hidden, cl::init(false), cl::desc("Save the caller's VSP on the stack in functions with an HLX " "vector stack frame, for debuggers"))
IRTranslator LLVM IR MI
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:85
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
MachineRegisterInfo & MRI
This file declares the machine register scavenger class.
bool isDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
an instruction to allocate memory on the stack
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
int find_first() const
Returns the index of the first set bit, -1 if none of the bits are set.
Definition BitVector.h:318
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
Definition BitVector.h:356
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:367
int find_next(unsigned Prev) const
Returns the index of the next set bit following the "Prev" bit.
Definition BitVector.h:325
Helper class for creating CFI instructions and inserting them into MIR.
void buildEscape(StringRef Bytes, StringRef Comment="") const
void buildRestore(MCRegister Reg) const
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
A debug info location.
Definition DebugLoc.h:126
NodeT * findNearestCommonDominator(NodeT *A, NodeT *B) const
Find nearest common dominator basic block for basic block A and B.
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:705
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:686
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition Function.h:230
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
void replaceInstr(MachineInstr *OldMI, MachineInstr *NewMI)
IndexType getIndex(MachineInstr *MI) const
MachineInstr * getInstr(IndexType Idx) const
void add(IndexType Start, IndexType End, bool Fixed, bool TiedEnd)
void insertCFIInstructions(MachineFunction &MF) const
bool hasFPImpl(const MachineFunction &MF) const override
bool enableCalleeSaveSkip(const MachineFunction &MF) const override
Returns true if the target can safely skip saving callee-saved registers for noreturn nounwind functi...
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
Perform most of the PEI work here:
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
void orderFrameObjects(const MachineFunction &MF, SmallVectorImpl< int > &ObjectsToAllocate) const override
Order the symbols in the local stack frame.
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS=nullptr) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
void inlineStackProbe(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const override
Replace a StackProbe stub (if any) with the actual probe code inline.
const SpillSlot * getCalleeSavedSpillSlots(unsigned &NumEntries) const override
getCalleeSavedSpillSlots - This method returns a pointer to an array of pairs, that contains an entry...
bool needsAligna(const MachineFunction &MF) const
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const override
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Store the specified register of the given register class to the specified stack frame index.
const HexagonRegisterInfo & getRegisterInfo() const
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Load the specified register of the given register class from the specified stack frame index.
Hexagon target-specific information for each MachineFunction.
bool isEHReturnCalleeSaveReg(Register Reg) const
const MCPhysReg * getCallerSavedRegs(const MachineFunction *MF, const TargetRegisterClass *RC) const
bool isTypeForHLX(Type *VecTy) const
const HexagonInstrInfo * getInstrInfo() const override
const HexagonFrameLowering * getFrameLowering() const override
const HexagonRegisterInfo * getRegisterInfo() const override
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
Definition MCDwarf.h:631
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
Definition MCDwarf.h:673
static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals, SMLoc Loc={}, StringRef Comment="")
.cfi_escape Allows the user to add arbitrary bytes to the unwind info.
Definition MCDwarf.h:759
static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_same_value Current value of Register is the same as in the previous frame.
Definition MCDwarf.h:742
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Describe properties that are true of each instruction in the target description file.
MCRegAliasIterator enumerates all registers aliasing Reg.
unsigned getID() const
getID() - Return the register class ID number.
bool hasSubClassEq(const MCRegisterClass *RC) const
Returns true if RC is a sub-class of or equal to this class.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
MachineInstrBundleIterator< const MachineInstr > const_iterator
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
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.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
iterator_range< succ_iterator > successors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
bool dominates(const MachineInstr *A, const MachineInstr *B) const
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setMaxCallFrameSize(uint64_t S)
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
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.
const AllocaInst * getObjectAllocation(int ObjectIdx) const
Return the underlying Alloca of the specified stack object if it exists.
bool hasCalls() const
Return true if the current function has any function calls.
Align getMaxAlign() const
Return alignment of this function's frame.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
LLVM_ABI uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
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.
LLVM_ABI int CreateSpillStackObject(uint64_t Size, Align Alignment, TargetStackID::Value StackID=TargetStackID::Default)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
bool isVariableSizedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a variable sized object.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
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.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
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...
Properties which a MachineFunction may have at a given point in time.
unsigned addFrameInst(const MCCFIInstruction &Inst)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
BasicBlockListType::iterator iterator
bool disableFramePointerElim() const
Returns true if frame pointer elimination should be disabled for this function.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addCFIIndex(unsigned CFIIndex) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isReturn(QueryType Type=AnyInBundle) const
const MachineBasicBlock * getParent() const
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
mop_range implicit_operands()
LLVM_ABI void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI)
Copy implicit register operands from specified instruction to this instruction.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
int64_t getImm() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
LLVM_ABI MachineBasicBlock * findNearestCommonDominator(ArrayRef< MachineBasicBlock * > Blocks) const
Returns the nearest common dominator of the given blocks.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
A vector that has set insertion semantics.
Definition SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:176
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
void append(StringRef RHS)
Append from a StringRef.
Definition SmallString.h:70
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:47
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM_ABI std::string lower() const
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
const TargetRegisterInfo & getRegisterInfo() const
virtual bool hasInlineStackProbe(const MachineFunction &MF) const
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
bool isPositionIndependent() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr MCPhysReg SP
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:577
void stable_sort(R &&Range)
Definition STLExtras.h:2132
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Dead
Unused definition.
@ Kill
The last use of a register.
constexpr RegState getKillRegState(bool B)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionPass * createHexagonCallFrameInformation()
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:149
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
@ Default
-O2, -Os, -Oz
Definition CodeGen.h:283
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
RegState getRegState(const MachineOperand &RegOp)
Get all register state flags from machine operand RegOp.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition LEB128.h:24
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
MaybeAlign getStackAlign(const Function &F, unsigned Index)
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition LEB128.h:79
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:82
static RegisterSet expandToSubRegs(RegisterRef R, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI)
std::map< RegisterRef, RangeList > RegToRangeMap
static LLVM_ABI MachinePointerInfo getStack(MachineFunction &MF, int64_t Offset, uint8_t ID=0)
Stack pointer relative access.