LLVM: lib/Target/LoongArch/LoongArchFrameLowering.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

25

26using namespace llvm;

27

28#define DEBUG_TYPE "loongarch-frame-lowering"

29

30

31

32

33

36

41}

42

46

48}

49

56 bool IsLA64 = STI.is64Bit();

57 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;

58

59 if (DestReg == SrcReg && Val == 0)

60 return;

61

62 if (isInt<12>(Val)) {

63

68 return;

69 }

70

71

72

73

74

75

76

79 if (Val > -4096 && Val <= (2 * MaxPosAdjStep)) {

80 int64_t FirstAdj = Val < 0 ? -2048 : MaxPosAdjStep;

81 Val -= FirstAdj;

90 return;

91 }

92

93 unsigned Opc = IsLA64 ? LoongArch::ADD_D : LoongArch::ADD_W;

94 if (Val < 0) {

95 Val = -Val;

96 Opc = IsLA64 ? LoongArch::SUB_D : LoongArch::SUB_W;

97 }

98

100 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);

101 TII->movImm(MBB, MBBI, DL, ScratchReg, Val, Flag);

106}

107

108

109void LoongArchFrameLowering::determineFrameLayout(MachineFunction &MF) const {

111

112

114

115

117

118

120}

121

125 for (auto &MBB : MF)

126 for (auto &MI : MBB)

127 FuncSize += TII->getInstSizeInBytes(MI);

128 return FuncSize;

129}

130

133 return false;

134 for (auto &MBB : MF)

135 for (auto &MI : MBB)

136 if (MI.getOpcode() == LoongArch::PseudoST_CFR)

137 return true;

138 return false;

139}

140

149

150 unsigned ScavSlotsNum = 0;

151

152

153

155 if (IsLargeFunction)

156 ScavSlotsNum = 1;

157

158

159

160

161 if (!isInt<11>(MFI.estimateStackSize(MF)))

162 ScavSlotsNum = std::max(ScavSlotsNum, 1u);

163

164

166 ++ScavSlotsNum;

167

168

169 for (unsigned i = 0; i < ScavSlotsNum; ++i) {

170 int FI =

171 MFI.CreateSpillStackObject(RI->getSpillSize(RC), RI->getSpillAlign(RC));

176 << ") as the emergency spill slot.\n");

177 }

178}

179

187 bool IsLA64 = STI.is64Bit();

188

191

192

193

195

196

198 return;

199

200 determineFrameLayout(MF);

201

202

204 uint64_t RealStackSize = StackSize;

205

206

208 return;

209

211

212 if (FirstSPAdjustAmount)

213 StackSize = FirstSPAdjustAmount;

214

215

217

218 unsigned CFIIndex =

223

225

226

227

228

229

230 std::advance(MBBI, CSI.size());

231

232

233

234 for (const auto &Entry : CSI) {

237 nullptr, RI->getDwarfRegNum(Entry.getReg(), true), Offset));

241 }

242

243

246 StackSize - LoongArchFI->getVarArgsSaveSize(),

248

249

252 LoongArchFI->getVarArgsSaveSize()));

256 }

257

258

259 if (FirstSPAdjustAmount) {

260 uint64_t SecondSPAdjustAmount = RealStackSize - FirstSPAdjustAmount;

261 assert(SecondSPAdjustAmount > 0 &&

262 "SecondSPAdjustAmount should be greater than zero");

265

266 if (hasFP(MF)) {

267

268

269

275 }

276 }

277

279

280 if (RI->hasStackRealignment(MF)) {

282 assert(Align > 0 && "The stack realignment size is invalid!");

284 TII->get(IsLA64 ? LoongArch::BSTRINS_D : LoongArch::BSTRINS_W),

287 .addReg(LoongArch::R0)

291

292

293

295

299 .addReg(LoongArch::R0)

301 }

302 }

303 }

304}

305

312

313

315 return;

318

320

321 auto LastFrameDestroy = MBBI;

322 if (!CSI.empty())

323 LastFrameDestroy = std::prev(MBBI, CSI.size());

324

325

327

328

330 assert(hasFP(MF) && "frame pointer should not have been eliminated");

331 adjustReg(MBB, LastFrameDestroy, DL, SPReg, LoongArch::R22,

332 -StackSize + LoongArchFI->getVarArgsSaveSize(),

334 }

335

337 if (FirstSPAdjustAmount) {

338 uint64_t SecondSPAdjustAmount = StackSize - FirstSPAdjustAmount;

339 assert(SecondSPAdjustAmount > 0 &&

340 "SecondSPAdjustAmount should be greater than zero");

341

342 adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg, SecondSPAdjustAmount,

344 StackSize = FirstSPAdjustAmount;

345 }

346

347

349}

350

351

352

353

354

355

356

357

358

363

364

365

366 if (!isInt<12>(MFI.getStackSize()) && (CSI.size() > 0)) {

367

368

369

370

371

373 }

374 return 0;

375}

376

381

382

384 SavedRegs.set(LoongArch::R1);

385 SavedRegs.set(LoongArch::R22);

386 }

387

390}

391

392

393

394

398}

399

400

407

409

410

411

412

413

414 int64_t Amount = MI->getOperand(0).getImm();

415

416 if (Amount != 0) {

417

419

420 if (MI->getOpcode() == LoongArch::ADJCALLSTACKDOWN)

421 Amount = -Amount;

422

424 }

425 }

426

428}

429

434 return true;

435

438

439

440 for (auto &CS : CSI) {

442

443

444 bool IsKill =

449 }

450

451 return true;

452}

453

461

462

463

464

466 int MinCSFI = 0;

467 int MaxCSFI = -1;

471

472 if (CSI.size()) {

473 MinCSFI = CSI[0].getFrameIdx();

474 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();

475 }

476

477 if (FI >= MinCSFI && FI <= MaxCSFI) {

478 FrameReg = LoongArch::R3;

479 if (FirstSPAdjustAmount)

481 else

484

485

486

489 } else {

493 else

495 }

496

498}

499

502

504 return false;

505

506 return true;

507}

unsigned const MachineRegisterInfo * MRI

MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL

MachineBasicBlock MachineBasicBlock::iterator MBBI

Given that RA is a live value

const HexagonInstrInfo * TII

static uint64_t estimateFunctionSizeInBytes(const LoongArchInstrInfo *TII, const MachineFunction &MF)

static bool needScavSlotForCFR(MachineFunction &MF)

unsigned const TargetRegisterInfo * TRI

static constexpr Register SPReg

static constexpr Register FPReg

This file declares the machine register scavenger class.

assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

bool empty() const

empty - Check if the array is empty.

CallingConv::ID getCallingConv() const

getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...

bool hasOptNone() const

Do not optimize this function (-O0).

void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override

Store the specified register of the given register class to the specified stack frame index.

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

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...

int getBranchRelaxationSpillFrameIndex()

void setBranchRelaxationSpillFrameIndex(int Index)

const LoongArchRegisterInfo * getRegisterInfo() const override

const LoongArchInstrInfo * getInstrInfo() 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.

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.

static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})

.cfi_def_cfa_offset modifies a rule for computing CFA.

iterator getFirstTerminator()

Returns an iterator to the first terminator instruction of this basic block.

const MachineFunction * getParent() const

Return the MachineFunction containing this basic block.

instr_iterator erase(instr_iterator I)

Remove an instruction from the instruction list and delete it.

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 isReturnAddressTaken() const

This method may be called any time after instruction selection is complete to determine if there is a...

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 the alignment in bytes that this function must be aligned to, which is greater than the defaul...

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.

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.

Ty * getInfo()

getInfo - Keep track of various per-function pieces of information for backends that would like to do...

const TargetMachine & getTarget() const

getTarget - Return the target machine this machine code is compiled with

const MachineInstrBuilder & addCFIIndex(unsigned CFIIndex) const

const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const

const MachineInstrBuilder & addImm(int64_t Val) const

Add a new immediate operand.

const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const

Add a new virtual register operand.

MachineRegisterInfo - Keep track of information for virtual and physical registers,...

void addScavengingFrameIndex(int FI)

Add a scavenging frame index.

Wrapper class representing virtual and physical registers.

StackOffset holds a fixed and a scalable offset in bytes.

int64_t getFixed() const

Returns the fixed component of the stack.

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.

bool DisableFramePointerElim(const MachineFunction &MF) const

DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...

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 TargetRegisterInfo * getRegisterInfo() const

getRegisterInfo - If register information is available, return it.

virtual const TargetInstrInfo * getInstrInfo() const

@ GHC

Used by the Glasgow Haskell Compiler (GHC).

@ Kill

The last use of a register.

This is an optimization pass for GlobalISel generic memory operations.

MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)

Builder interface. Specify how to create the initial instruction itself.

raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

uint64_t alignTo(uint64_t Size, Align A)

Returns a multiple of A needed to store Size bytes.

unsigned Log2(Align A)

Returns the log2 of the alignment.

This struct is a compact representation of a valid (non-zero power of two) alignment.

uint64_t value() const

This is a hole in the type system and should not be abused.