LLVM 24.0.0git
LoongArchFrameLowering.cpp
Go to the documentation of this file.
1//===-- LoongArchFrameLowering.cpp - LoongArch Frame Information -*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the LoongArch implementation of TargetFrameLowering class.
10//
11//===----------------------------------------------------------------------===//
12
15#include "LoongArchSubtarget.h"
26#include "llvm/MC/MCDwarf.h"
27
28using namespace llvm;
29
30#define DEBUG_TYPE "loongarch-frame-lowering"
31
32// Return true if the specified function should have a dedicated frame
33// pointer register. This is true if frame pointer elimination is
34// disabled, if it needs dynamic stack realignment, if the function has
35// variable sized allocas, or if the frame address is taken.
37 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
38
39 const MachineFrameInfo &MFI = MF.getFrameInfo();
40 return MF.disableFramePointerElim() || RegInfo->hasStackRealignment(MF) ||
42}
43
45 const MachineFrameInfo &MFI = MF.getFrameInfo();
46 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
47
48 return MFI.hasVarSizedObjects() && TRI->hasStackRealignment(MF);
49}
50
51void LoongArchFrameLowering::adjustReg(MachineBasicBlock &MBB,
53 const DebugLoc &DL, Register DestReg,
54 Register SrcReg, int64_t Val,
55 MachineInstr::MIFlag Flag) const {
56 const LoongArchInstrInfo *TII = STI.getInstrInfo();
57 bool IsLA64 = STI.is64Bit();
58 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
59
60 if (DestReg == SrcReg && Val == 0)
61 return;
62
63 if (isInt<12>(Val)) {
64 // addi.w/d $DstReg, $SrcReg, Val
65 BuildMI(MBB, MBBI, DL, TII->get(Addi), DestReg)
66 .addReg(SrcReg)
67 .addImm(Val)
68 .setMIFlag(Flag);
69 return;
70 }
71
72 // Try to split the offset across two ADDIs. We need to keep the stack pointer
73 // aligned after each ADDI. We need to determine the maximum value we can put
74 // in each ADDI. In the negative direction, we can use -2048 which is always
75 // sufficiently aligned. In the positive direction, we need to find the
76 // largest 12-bit immediate that is aligned. Exclude -4096 since it can be
77 // created with LU12I.W.
78 assert(getStackAlign().value() < 2048 && "Stack alignment too large");
79 int64_t MaxPosAdjStep = 2048 - getStackAlign().value();
80 if (Val > -4096 && Val <= (2 * MaxPosAdjStep)) {
81 int64_t FirstAdj = Val < 0 ? -2048 : MaxPosAdjStep;
82 Val -= FirstAdj;
83 BuildMI(MBB, MBBI, DL, TII->get(Addi), DestReg)
84 .addReg(SrcReg)
85 .addImm(FirstAdj)
86 .setMIFlag(Flag);
87 BuildMI(MBB, MBBI, DL, TII->get(Addi), DestReg)
88 .addReg(DestReg, RegState::Kill)
89 .addImm(Val)
90 .setMIFlag(Flag);
91 return;
92 }
93
94 unsigned Opc = IsLA64 ? LoongArch::ADD_D : LoongArch::ADD_W;
95 if (Val < 0) {
96 Val = -Val;
97 Opc = IsLA64 ? LoongArch::SUB_D : LoongArch::SUB_W;
98 }
99
100 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
101 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
102 TII->movImm(MBB, MBBI, DL, ScratchReg, Val, Flag);
103 BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
104 .addReg(SrcReg)
105 .addReg(ScratchReg, RegState::Kill)
106 .setMIFlag(Flag);
107}
108
109// Determine the size of the frame and maximum call frame size.
110void LoongArchFrameLowering::determineFrameLayout(MachineFunction &MF) const {
111 MachineFrameInfo &MFI = MF.getFrameInfo();
112
113 // Get the number of bytes to allocate from the FrameInfo.
114 uint64_t FrameSize = MFI.getStackSize();
115
116 // Make sure the frame is aligned.
117 FrameSize = alignTo(FrameSize, getStackAlign());
118
119 // Update frame info.
120 MFI.setStackSize(FrameSize);
121}
122
124 const MachineFunction &MF) {
125 uint64_t FuncSize = 0;
126 for (auto &MBB : MF)
127 for (auto &MI : MBB)
128 FuncSize += TII->getInstSizeInBytes(MI);
129 return FuncSize;
130}
131
133 if (!MF.getSubtarget<LoongArchSubtarget>().hasBasicF())
134 return false;
135 for (auto &MBB : MF)
136 for (auto &MI : MBB)
137 if (MI.getOpcode() == LoongArch::PseudoST_CFR)
138 return true;
139 return false;
140}
141
143 MachineFunction &MF, RegScavenger *RS) const {
144 const LoongArchRegisterInfo *RI = STI.getRegisterInfo();
145 const TargetRegisterClass &RC = LoongArch::GPRRegClass;
146 const LoongArchInstrInfo *TII = STI.getInstrInfo();
149 MachineFrameInfo &MFI = MF.getFrameInfo();
150
151 unsigned ScavSlotsNum = 0;
152
153 // Far branches beyond 27-bit offset require a spill slot for scratch
154 // register.
155 bool IsLargeFunction = !isInt<27>(estimateFunctionSizeInBytes(TII, MF));
156 if (IsLargeFunction)
157 ScavSlotsNum = 1;
158
159 // estimateStackSize has been observed to under-estimate the final stack
160 // size, so give ourselves wiggle-room by checking for stack size
161 // representable an 11-bit signed field rather than 12-bits.
162 // For [x]vstelm.{b/h/w/d} memory instructions with 8 imm offset, 7-bit
163 // signed field is fine.
164 unsigned EstimateStackSize = MFI.estimateStackSize(MF);
165 if (!isInt<11>(EstimateStackSize) ||
166 (MF.getSubtarget<LoongArchSubtarget>().hasExtLSX() &&
167 !isInt<7>(EstimateStackSize)))
168 ScavSlotsNum = std::max(ScavSlotsNum, 1u);
169
170 // For CFR spill.
171 if (needScavSlotForCFR(MF))
172 ++ScavSlotsNum;
173
174 // Create emergency spill slots.
175 for (unsigned i = 0; i < ScavSlotsNum; ++i) {
176 int FI =
177 MFI.CreateSpillStackObject(RI->getSpillSize(RC), RI->getSpillAlign(RC));
178 RS->addScavengingFrameIndex(FI);
179 if (IsLargeFunction && LAFI->getBranchRelaxationSpillFrameIndex() == -1)
181 LLVM_DEBUG(dbgs() << "Allocated FI(" << FI
182 << ") as the emergency spill slot.\n");
183 }
184}
185
186// Allocate stack space and probe it if necessary.
187void LoongArchFrameLowering::allocateStack(MachineBasicBlock &MBB,
189 MachineFunction &MF, uint64_t Offset,
190 uint64_t RealStackSize, bool EmitCFI,
191 bool NeedProbe, uint64_t ProbeSize,
192 bool DynAllocation,
193 MachineInstr::MIFlag Flag) const {
194 DebugLoc DL;
195 const LoongArchInstrInfo *TII = STI.getInstrInfo();
196 const bool IsLA64 = STI.is64Bit();
197 const Register SPReg = LoongArch::R3;
199
200 // Simply allocate the stack if it's not big enough to require a probe.
201 if (!NeedProbe || Offset <= ProbeSize) {
202 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -Offset, Flag);
203 if (EmitCFI)
204 CFIBuilder.buildDefCFAOffset(RealStackSize);
205
206 if (NeedProbe && DynAllocation) {
207 // st.{w/d} $zero, $sp, 0
208 BuildMI(MBB, MBBI, DL,
209 TII->get(IsLA64 ? LoongArch::ST_D : LoongArch::ST_W))
210 .addReg(LoongArch::R0)
211 .addReg(SPReg)
212 .addImm(0)
213 .setMIFlag(Flag);
214 }
215
216 return;
217 }
218
219 // Unroll the probe loop depending on the number of iterations.
220 if (Offset < ProbeSize * 5) {
221 const uint64_t CFAAdjust = RealStackSize - Offset;
222
223 uint64_t CurrentOffset = 0;
224 while (CurrentOffset + ProbeSize <= Offset) {
225 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -ProbeSize, Flag);
226 // st.{w/d} $zero, $sp, 0
227 BuildMI(MBB, MBBI, DL,
228 TII->get(IsLA64 ? LoongArch::ST_D : LoongArch::ST_W))
229 .addReg(LoongArch::R0)
230 .addReg(SPReg)
231 .addImm(0)
232 .setMIFlag(Flag);
233
234 CurrentOffset += ProbeSize;
235 if (EmitCFI)
236 CFIBuilder.buildDefCFAOffset(CurrentOffset + CFAAdjust);
237 }
238
239 const uint64_t Residual = Offset - CurrentOffset;
240 if (Residual) {
241 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -Residual, Flag);
242 if (EmitCFI)
243 CFIBuilder.buildDefCFAOffset(RealStackSize);
244
245 if (DynAllocation) {
246 // st.{w/d} $zero, $sp, 0
247 BuildMI(MBB, MBBI, DL,
248 TII->get(IsLA64 ? LoongArch::ST_D : LoongArch::ST_W))
249 .addReg(LoongArch::R0)
250 .addReg(SPReg)
251 .addImm(0)
252 .setMIFlag(Flag);
253 }
254 }
255 return;
256 }
257
258 // Emit a variable-length allocation probing loop.
259 const uint64_t RoundedSize = alignDown(Offset, ProbeSize);
260 const uint64_t Residual = Offset - RoundedSize;
261 const uint64_t CFAAdjust = RealStackSize - Offset;
262
263 const Register TargetReg = LoongArch::R13;
264 // SUB TargetReg, $sp, RoundedSize
265 adjustReg(MBB, MBBI, DL, TargetReg, SPReg, -RoundedSize, Flag);
266
267 if (EmitCFI) {
268 // Set the CFA register to TargetReg.
269 CFIBuilder.buildDefCFA(TargetReg, RoundedSize + CFAAdjust);
270 }
271
272 // It will be expanded to a probe loop in inlineStackProbe().
273 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PROBED_STACKALLOC))
274 .addReg(TargetReg);
275
276 if (EmitCFI) {
277 // Set the CFA register back to SP.
278 CFIBuilder.buildDefCFARegister(SPReg);
279 }
280
281 if (Residual) {
282 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -Residual, Flag);
283 if (DynAllocation) {
284 // st.{w/d} $zero, $sp, 0
285 BuildMI(MBB, MBBI, DL,
286 TII->get(IsLA64 ? LoongArch::ST_D : LoongArch::ST_W))
287 .addReg(LoongArch::R0)
288 .addReg(SPReg)
289 .addImm(0)
290 .setMIFlag(Flag);
291 }
292 }
293
294 if (EmitCFI)
295 CFIBuilder.buildDefCFAOffset(RealStackSize);
296}
297
299 MachineBasicBlock &MBB) const {
300 MachineFrameInfo &MFI = MF.getFrameInfo();
301 auto *LoongArchFI = MF.getInfo<LoongArchMachineFunctionInfo>();
302 const LoongArchRegisterInfo *RI = STI.getRegisterInfo();
303 const LoongArchInstrInfo *TII = STI.getInstrInfo();
305 bool IsLA64 = STI.is64Bit();
306
307 Register SPReg = LoongArch::R3;
308 Register FPReg = LoongArch::R22;
309
310 // Debug location must be unknown since the first debug location is used
311 // to determine the end of the prologue.
312 DebugLoc DL;
313 // All calls are tail calls in GHC calling conv, and functions have no
314 // prologue/epilogue.
316 return;
317 // Determine the correct frame layout
318 determineFrameLayout(MF);
319
320 // First, compute final stack size.
321 uint64_t StackSize = MFI.getStackSize();
322 uint64_t RealStackSize = StackSize;
323
324 // Early exit if there is no need to allocate space in the stack.
325 if (StackSize == 0 && !MFI.adjustsStack())
326 return;
327
328 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
329 // Split the SP adjustment to reduce the offsets of callee saved spill.
330 if (FirstSPAdjustAmount)
331 StackSize = FirstSPAdjustAmount;
332
333 // Adjust stack.
334 const LoongArchTargetLowering *TLI = STI.getTargetLowering();
335 const bool NeedProbe = TLI->hasInlineStackProbe(MF);
336 const uint64_t ProbeSize = TLI->getStackProbeSize(MF, getStackAlign());
337 const bool DynAllocation =
338 MF.getInfo<LoongArchMachineFunctionInfo>()->hasDynamicAllocation();
339 if (StackSize != 0)
340 allocateStack(MBB, MBBI, MF, StackSize, StackSize,
341 /*EmitCFI=*/true, NeedProbe, ProbeSize, DynAllocation,
343
344 const auto &CSI = MFI.getCalleeSavedInfo();
345
346 // The frame pointer is callee-saved, and code has been generated for us to
347 // save it to the stack. We need to skip over the storing of callee-saved
348 // registers as the frame pointer must be modified after it has been saved
349 // to the stack, not before.
350 std::advance(MBBI, CSI.size());
351
352 // Iterate over list of callee-saved registers and emit .cfi_offset
353 // directives.
354 for (const auto &Entry : CSI) {
355 int64_t Offset = MFI.getObjectOffset(Entry.getFrameIdx());
356 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
357 nullptr, RI->getDwarfRegNum(Entry.getReg(), true), Offset));
358 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
359 .addCFIIndex(CFIIndex)
361 }
362
363 // Generate new FP.
364 if (hasFP(MF)) {
365 adjustReg(MBB, MBBI, DL, FPReg, SPReg,
366 StackSize - LoongArchFI->getVarArgsSaveSize(),
368
369 // Emit ".cfi_def_cfa $fp, LoongArchFI->getVarArgsSaveSize()"
370 unsigned CFIIndex = MF.addFrameInst(
371 MCCFIInstruction::cfiDefCfa(nullptr, RI->getDwarfRegNum(FPReg, true),
372 LoongArchFI->getVarArgsSaveSize()));
373 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
374 .addCFIIndex(CFIIndex)
376 }
377
378 // Emit the second SP adjustment after saving callee saved registers.
379 if (FirstSPAdjustAmount) {
380 uint64_t SecondSPAdjustAmount = RealStackSize - FirstSPAdjustAmount;
381 assert(SecondSPAdjustAmount > 0 &&
382 "SecondSPAdjustAmount should be greater than zero");
383 allocateStack(MBB, MBBI, MF, SecondSPAdjustAmount, RealStackSize,
384 !hasFP(MF), NeedProbe, ProbeSize, DynAllocation,
386 }
387
388 if (hasFP(MF)) {
389 // Realign stack.
390 if (RI->hasStackRealignment(MF)) {
391 unsigned Align = Log2(MFI.getMaxAlign());
392 assert(Align > 0 && "The stack realignment size is invalid!");
393 BuildMI(MBB, MBBI, DL,
394 TII->get(IsLA64 ? LoongArch::BSTRINS_D : LoongArch::BSTRINS_W),
395 SPReg)
396 .addReg(SPReg)
397 .addReg(LoongArch::R0)
398 .addImm(Align - 1)
399 .addImm(0)
401 // FP will be used to restore the frame in the epilogue, so we need
402 // another base register BP to record SP after re-alignment. SP will
403 // track the current stack after allocating variable sized objects.
404 if (hasBP(MF)) {
405 // move BP, $sp
406 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::OR),
408 .addReg(SPReg)
409 .addReg(LoongArch::R0)
411 }
412 }
413 }
414}
415
417 MachineBasicBlock &MBB) const {
418 const LoongArchRegisterInfo *RI = STI.getRegisterInfo();
419 MachineFrameInfo &MFI = MF.getFrameInfo();
420 auto *LoongArchFI = MF.getInfo<LoongArchMachineFunctionInfo>();
421 Register SPReg = LoongArch::R3;
422 // All calls are tail calls in GHC calling conv, and functions have no
423 // prologue/epilogue.
425 return;
426 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
427 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
428
429 const auto &CSI = MFI.getCalleeSavedInfo();
430 // Skip to before the restores of callee-saved registers.
431 auto LastFrameDestroy = MBBI;
432 if (!CSI.empty())
433 LastFrameDestroy = std::prev(MBBI, CSI.size());
434
435 // Get the number of bytes from FrameInfo.
436 uint64_t StackSize = MFI.getStackSize();
437
438 // Restore the stack pointer.
439 if (RI->hasStackRealignment(MF) || MFI.hasVarSizedObjects()) {
440 assert(hasFP(MF) && "frame pointer should not have been eliminated");
441 adjustReg(MBB, LastFrameDestroy, DL, SPReg, LoongArch::R22,
442 -StackSize + LoongArchFI->getVarArgsSaveSize(),
444 }
445
446 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
447 if (FirstSPAdjustAmount) {
448 uint64_t SecondSPAdjustAmount = StackSize - FirstSPAdjustAmount;
449 assert(SecondSPAdjustAmount > 0 &&
450 "SecondSPAdjustAmount should be greater than zero");
451
452 adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg, SecondSPAdjustAmount,
454 StackSize = FirstSPAdjustAmount;
455 }
456
457 // Deallocate stack
458 adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackSize, MachineInstr::FrameDestroy);
459}
460
461// Synthesize the probe loop.
463 Register TargetReg) {
464 assert(TargetReg != LoongArch::R3 &&
465 "New top of stack cannot already be in $sp");
466
467 MachineBasicBlock &MBB = *MBBI->getParent();
468 MachineFunction &MF = *MBB.getParent();
469
471 const LoongArchInstrInfo *TII = STI.getInstrInfo();
472 const bool IsLA64 = STI.is64Bit();
473 const Align StackAlign = STI.getFrameLowering()->getStackAlign();
475 const uint64_t ProbeSize = TLI->getStackProbeSize(MF, StackAlign);
476
477 MachineFunction::iterator MBBInsertPoint = std::next(MBB.getIterator());
478 MachineBasicBlock *LoopTestMBB =
479 MF.CreateMachineBasicBlock(MBB.getBasicBlock());
480 MF.insert(MBBInsertPoint, LoopTestMBB);
481 MachineBasicBlock *ExitMBB = MF.CreateMachineBasicBlock(MBB.getBasicBlock());
482 MF.insert(MBBInsertPoint, ExitMBB);
483 const Register SPReg = LoongArch::R3;
484 const Register ScratchReg = LoongArch::R14;
486
487 // ScratchReg = ProbeSize
488 TII->movImm(MBB, MBBI, DL, ScratchReg, ProbeSize, Flags);
489
490 // LoopTest:
491 // sub.{w/d} $sp, $sp, ScratchReg
492 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL,
493 TII->get(IsLA64 ? LoongArch::SUB_D : LoongArch::SUB_W), SPReg)
494 .addReg(SPReg)
495 .addReg(ScratchReg)
496 .setMIFlag(Flags);
497
498 // st.{w/d} $zero, $sp, 0
499 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL,
500 TII->get(IsLA64 ? LoongArch::ST_D : LoongArch::ST_W))
501 .addReg(LoongArch::R0)
502 .addReg(SPReg)
503 .addImm(0)
504 .setMIFlag(Flags);
505
506 // bne $sp, TargetReg, LoopTest
507 BuildMI(*LoopTestMBB, LoopTestMBB->end(), DL, TII->get(LoongArch::BNE))
508 .addReg(SPReg)
509 .addReg(TargetReg)
510 .addMBB(LoopTestMBB)
511 .setMIFlag(Flags);
512
513 ExitMBB->splice(ExitMBB->end(), &MBB, std::next(MBBI), MBB.end());
515
516 LoopTestMBB->addSuccessor(ExitMBB);
517 LoopTestMBB->addSuccessor(LoopTestMBB);
518 MBB.addSuccessor(LoopTestMBB);
519 // Update liveins.
520 fullyRecomputeLiveIns({ExitMBB, LoopTestMBB});
521}
522
524 MachineBasicBlock &MBB) const {
525 // Get the instructions that need to be replaced. We emit at most two of
526 // these. Remember them in order to avoid complications coming from the need
527 // to traverse the block while potentially creating more blocks.
529 for (MachineInstr &MI : MBB) {
530 if (MI.getOpcode() == LoongArch::PROBED_STACKALLOC) {
531 ToReplace.push_back(&MI);
532 }
533 }
534
535 for (MachineInstr *MI : ToReplace) {
536 MachineBasicBlock::iterator MBBI = MI->getIterator();
537 DebugLoc DL = MBB.findDebugLoc(MBBI);
538 Register TargetReg = MI->getOperand(0).getReg();
539 emitStackProbeInline(MBBI, DL, TargetReg);
540 MBBI->eraseFromParent();
541 }
542}
543
544// We would like to split the SP adjustment to reduce prologue/epilogue
545// as following instructions. In this way, the offset of the callee saved
546// register could fit in a single store.
547// e.g.
548// addi.d $sp, $sp, -2032
549// st.d $ra, $sp, 2024
550// st.d $fp, $sp, 2016
551// addi.d $sp, $sp, -16
553 const MachineFunction &MF) const {
554 const MachineFrameInfo &MFI = MF.getFrameInfo();
555 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
556
557 // Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
558 // 12-bit and there exists a callee-saved register needing to be pushed.
559 if (!isInt<12>(MFI.getStackSize()) && (CSI.size() > 0)) {
560 // FirstSPAdjustAmount is chosen as (2048 - StackAlign) because 2048 will
561 // cause sp = sp + 2048 in the epilogue to be split into multiple
562 // instructions. Offsets smaller than 2048 can fit in a single load/store
563 // instruction, and we have to stick with the stack alignment.
564 // So (2048 - StackAlign) will satisfy the stack alignment.
565 return 2048 - getStackAlign().value();
566 }
567 return 0;
568}
569
571 BitVector &SavedRegs,
572 RegScavenger *RS) const {
574 // Unconditionally spill RA and FP only if the function uses a frame
575 // pointer.
576 if (hasFP(MF)) {
577 SavedRegs.set(LoongArch::R1);
578 SavedRegs.set(LoongArch::R22);
579 }
580 // Mark BP as used if function has dedicated base pointer.
581 if (hasBP(MF))
582 SavedRegs.set(LoongArchABI::getBPReg());
583}
584
585// Do not preserve stack space within prologue for outgoing variables if the
586// function contains variable size objects.
587// Let eliminateCallFramePseudoInstr preserve stack space for it.
592
593// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
598 Register SPReg = LoongArch::R3;
599 DebugLoc DL = MI->getDebugLoc();
600
601 if (!hasReservedCallFrame(MF)) {
602 // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
603 // ADJCALLSTACKUP must be converted to instructions manipulating the stack
604 // pointer. This is necessary when there is a variable length stack
605 // allocation (e.g. alloca), which means it's not possible to allocate
606 // space for outgoing arguments from within the function prologue.
607 int64_t Amount = MI->getOperand(0).getImm();
608
609 if (Amount != 0) {
610 // Ensure the stack remains aligned after adjustment.
611 Amount = alignSPAdjust(Amount);
612
613 if (MI->getOpcode() == LoongArch::ADJCALLSTACKDOWN)
614 Amount = -Amount;
615
616 const LoongArchTargetLowering *TLI =
617 MF.getSubtarget<LoongArchSubtarget>().getTargetLowering();
618 const int64_t ProbeSize = TLI->getStackProbeSize(MF, getStackAlign());
619 if (TLI->hasInlineStackProbe(MF) && -Amount >= ProbeSize) {
620 // When stack probing is enabled, the decrement of SP may need to be
621 // probed. We can handle both the decrement and the probing in
622 // allocateStack.
623 const bool DynAllocation =
624 MF.getInfo<LoongArchMachineFunctionInfo>()->hasDynamicAllocation();
625 allocateStack(MBB, MI, MF, -Amount, -Amount,
626 MF.needsFrameMoves() && !hasFP(MF),
627 /*NeedProbe=*/true, ProbeSize, DynAllocation,
630 } else {
631 adjustReg(MBB, MI, DL, SPReg, SPReg, Amount, MachineInstr::NoFlags);
632 }
633 }
634 }
635
636 return MBB.erase(MI);
637}
638
642 if (CSI.empty())
643 return true;
644
645 MachineFunction *MF = MBB.getParent();
647
648 // Insert the spill to the stack frame.
649 for (auto &CS : CSI) {
650 MCRegister Reg = CS.getReg();
651 // If the register is RA and the return address is taken by method
652 // LoongArchTargetLowering::lowerRETURNADDR, don't set kill flag.
653 bool IsKill =
654 !(Reg == LoongArch::R1 && MF->getFrameInfo().isReturnAddressTaken());
655 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
656 TII.storeRegToStackSlot(MBB, MI, Reg, IsKill, CS.getFrameIdx(), RC,
657 Register());
658 }
659
660 return true;
661}
662
664 const MachineFunction &MF, int FI, Register &FrameReg) const {
665 const MachineFrameInfo &MFI = MF.getFrameInfo();
667 auto *LoongArchFI = MF.getInfo<LoongArchMachineFunctionInfo>();
668 uint64_t StackSize = MFI.getStackSize();
669 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
670
671 // Callee-saved registers should be referenced relative to the stack
672 // pointer (positive offset), otherwise use the frame pointer (negative
673 // offset).
674 const auto &CSI = MFI.getCalleeSavedInfo();
675 int MinCSFI = 0;
676 int MaxCSFI = -1;
679 MFI.getOffsetAdjustment());
680
681 if (CSI.size()) {
682 MinCSFI = CSI[0].getFrameIdx();
683 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
684 }
685
686 if (FI >= MinCSFI && FI <= MaxCSFI) {
687 FrameReg = LoongArch::R3;
688 if (FirstSPAdjustAmount)
689 Offset += StackOffset::getFixed(FirstSPAdjustAmount);
690 else
691 Offset += StackOffset::getFixed(StackSize);
692 } else if (RI->hasStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) {
693 // If the stack was realigned, the frame pointer is set in order to allow
694 // SP to be restored, so we need another base register to record the stack
695 // after realignment.
696 FrameReg = hasBP(MF) ? LoongArchABI::getBPReg() : LoongArch::R3;
697 Offset += StackOffset::getFixed(StackSize);
698 } else {
699 FrameReg = RI->getFrameRegister(MF);
700 if (hasFP(MF))
701 Offset += StackOffset::getFixed(LoongArchFI->getVarArgsSaveSize());
702 else
703 Offset += StackOffset::getFixed(StackSize);
704 }
705
706 return Offset;
707}
708
710 const MachineFunction &MF) const {
711 // Keep the conventional code flow when not optimizing.
712 if (MF.getFunction().hasOptNone())
713 return false;
714
715 return true;
716}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
static uint64_t estimateFunctionSizeInBytes(const LoongArchInstrInfo *TII, const MachineFunction &MF)
static void emitStackProbeInline(MachineBasicBlock::iterator MBBI, DebugLoc DL, Register TargetReg)
static bool needScavSlotForCFR(MachineFunction &MF)
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static constexpr MCPhysReg FPReg
static constexpr MCPhysReg SPReg
This file declares the machine register scavenger class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
Helper class for creating CFI instructions and inserting them into MIR.
A debug info location.
Definition DebugLoc.h:126
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:686
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...
uint64_t getFirstSPAdjustAmount(const MachineFunction &MF) const
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
void inlineStackProbe(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const override
Replace a StackProbe stub (if any) with the actual probe code inline.
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
bool enableShrinkWrapping(const MachineFunction &MF) const override
Returns true if the target will correctly handle shrink wrapping.
bool hasFPImpl(const MachineFunction &MF) const override
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
bool hasBP(const MachineFunction &MF) const
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
LoongArchMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private Lo...
const LoongArchTargetLowering * getTargetLowering() const override
const LoongArchInstrInfo * getInstrInfo() const override
const LoongArchFrameLowering * getFrameLowering() const override
bool hasInlineStackProbe(const MachineFunction &MF) const override
True if stack clash protection is enabled for this function.
unsigned getStackProbeSize(const MachineFunction &MF, Align StackAlign) const
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:628
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:670
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
Align getMaxAlign() const
Return alignment of this function's frame.
int64_t getOffsetAdjustment() const
Return the correction for frame offsets.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
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.
unsigned addFrameInst(const MCCFIInstruction &Inst)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
LLVM_ABI 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...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
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 & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
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
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:30
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:46
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...
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
int alignSPAdjust(int SPAdj) const
alignSPAdjust - This method aligns the stack adjustment to the correct alignment.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition CallingConv.h:50
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
@ Kill
The last use of a register.
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:541
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77