LLVM 24.0.0git
MipsSEFrameLowering.cpp
Go to the documentation of this file.
1//===- MipsSEFrameLowering.cpp - Mips32/64 Frame Information --------------===//
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 Mips32/64 implementation of TargetFrameLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MipsSEFrameLowering.h"
15#include "MipsMachineFunction.h"
16#include "MipsRegisterInfo.h"
17#include "MipsSEInstrInfo.h"
18#include "MipsSubtarget.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/StringRef.h"
36#include "llvm/IR/DebugLoc.h"
37#include "llvm/IR/Function.h"
41#include <cassert>
42#include <cstdint>
43#include <utility>
44#include <vector>
45
46using namespace llvm;
47
48static std::pair<unsigned, unsigned> getMFHiLoOpc(unsigned Src) {
49 if (Mips::ACC64RegClass.contains(Src))
50 return std::make_pair((unsigned)Mips::PseudoMFHI,
51 (unsigned)Mips::PseudoMFLO);
52
53 if (Mips::ACC64DSPRegClass.contains(Src))
54 return std::make_pair((unsigned)Mips::MFHI_DSP, (unsigned)Mips::MFLO_DSP);
55
56 if (Mips::ACC128RegClass.contains(Src))
57 return std::make_pair((unsigned)Mips::PseudoMFHI64,
58 (unsigned)Mips::PseudoMFLO64);
59
60 return std::make_pair(0, 0);
61}
62
63/// Return the number of instructions that save (or restore) the callee-saved
64/// registers in \p CSI. On MIPS I an AFGR64 register has no sdc1/ldc1 and is
65/// saved and restored with two swc1/lwc1.
66static unsigned getNumCSRInstrs(const MipsSubtarget &STI,
68 unsigned N = CSI.size();
69 if (!STI.hasMips2())
70 N += llvm::count_if(CSI, [](const CalleeSavedInfo &I) {
71 return Mips::AFGR64RegClass.contains(I.getReg());
72 });
73 return N;
74}
75
76namespace {
77
78/// Helper class to expand pseudos.
79class ExpandPseudo {
80public:
81 ExpandPseudo(MachineFunction &MF);
82 bool expand();
83
84private:
85 using Iter = MachineBasicBlock::iterator;
86
87 bool expandInstr(MachineBasicBlock &MBB, Iter I);
88 void expandLoadCCond(MachineBasicBlock &MBB, Iter I);
89 void expandStoreCCond(MachineBasicBlock &MBB, Iter I);
90 void expandLoadACC(MachineBasicBlock &MBB, Iter I, unsigned RegSize);
91 void expandStoreACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc,
92 unsigned MFLoOpc, unsigned RegSize);
93 bool expandCopy(MachineBasicBlock &MBB, Iter I);
94 bool expandCopyACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc,
95 unsigned MFLoOpc);
96 bool expandBuildPairF64(MachineBasicBlock &MBB,
97 MachineBasicBlock::iterator I, bool FP64) const;
98 bool expandExtractElementF64(MachineBasicBlock &MBB,
99 MachineBasicBlock::iterator I, bool FP64) const;
100
101 MachineFunction &MF;
102 MachineRegisterInfo &MRI;
103 const MipsSubtarget &Subtarget;
104 const MipsSEInstrInfo &TII;
105 const MipsRegisterInfo &RegInfo;
106};
107
108} // end anonymous namespace
109
110ExpandPseudo::ExpandPseudo(MachineFunction &MF_)
111 : MF(MF_), MRI(MF.getRegInfo()),
112 Subtarget(MF.getSubtarget<MipsSubtarget>()),
113 TII(*static_cast<const MipsSEInstrInfo *>(Subtarget.getInstrInfo())),
114 RegInfo(*Subtarget.getRegisterInfo()) {}
115
116bool ExpandPseudo::expand() {
117 bool Expanded = false;
118
119 for (auto &MBB : MF) {
120 for (Iter I = MBB.begin(), End = MBB.end(); I != End;)
121 Expanded |= expandInstr(MBB, I++);
122 }
123
124 return Expanded;
125}
126
127bool ExpandPseudo::expandInstr(MachineBasicBlock &MBB, Iter I) {
128 switch(I->getOpcode()) {
129 case Mips::LOAD_CCOND_DSP:
130 expandLoadCCond(MBB, I);
131 break;
132 case Mips::STORE_CCOND_DSP:
133 expandStoreCCond(MBB, I);
134 break;
135 case Mips::LOAD_ACC64:
136 case Mips::LOAD_ACC64DSP:
137 expandLoadACC(MBB, I, 4);
138 break;
139 case Mips::LOAD_ACC128:
140 expandLoadACC(MBB, I, 8);
141 break;
142 case Mips::STORE_ACC64:
143 expandStoreACC(MBB, I, Mips::PseudoMFHI, Mips::PseudoMFLO, 4);
144 break;
145 case Mips::STORE_ACC64DSP:
146 expandStoreACC(MBB, I, Mips::MFHI_DSP, Mips::MFLO_DSP, 4);
147 break;
148 case Mips::STORE_ACC128:
149 expandStoreACC(MBB, I, Mips::PseudoMFHI64, Mips::PseudoMFLO64, 8);
150 break;
151 case Mips::BuildPairF64:
152 if (expandBuildPairF64(MBB, I, false))
153 MBB.erase(I);
154 return false;
155 case Mips::BuildPairF64_64:
156 if (expandBuildPairF64(MBB, I, true))
157 MBB.erase(I);
158 return false;
159 case Mips::ExtractElementF64:
160 if (expandExtractElementF64(MBB, I, false))
161 MBB.erase(I);
162 return false;
163 case Mips::ExtractElementF64_64:
164 if (expandExtractElementF64(MBB, I, true))
165 MBB.erase(I);
166 return false;
167 case TargetOpcode::COPY:
168 if (!expandCopy(MBB, I))
169 return false;
170 break;
171 default:
172 return false;
173 }
174
175 MBB.erase(I);
176 return true;
177}
178
179void ExpandPseudo::expandLoadCCond(MachineBasicBlock &MBB, Iter I) {
180 // load $vr, FI
181 // copy ccond, $vr
182
183 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
184
185 const TargetRegisterClass *RC = RegInfo.intRegClass(4);
187 Register Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
188
189 TII.loadRegFromStack(MBB, I, VR, FI, RC, 0);
190 BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), Dst)
191 .addReg(VR, RegState::Kill);
192}
193
194void ExpandPseudo::expandStoreCCond(MachineBasicBlock &MBB, Iter I) {
195 // copy $vr, ccond
196 // store $vr, FI
197
198 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
199
200 const TargetRegisterClass *RC = RegInfo.intRegClass(4);
202 Register Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
203
204 BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), VR)
205 .addReg(Src, getKillRegState(I->getOperand(0).isKill()));
206 TII.storeRegToStack(MBB, I, VR, true, FI, RC, 0);
207}
208
209void ExpandPseudo::expandLoadACC(MachineBasicBlock &MBB, Iter I,
210 unsigned RegSize) {
211 // load $vr0, FI
212 // copy lo, $vr0
213 // load $vr1, FI + 4
214 // copy hi, $vr1
215
216 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
217
218 const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize);
221 Register Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
222 Register Lo = RegInfo.getSubReg(Dst, Mips::sub_lo);
223 Register Hi = RegInfo.getSubReg(Dst, Mips::sub_hi);
224 DebugLoc DL = I->getDebugLoc();
225 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
226
227 TII.loadRegFromStack(MBB, I, VR0, FI, RC, 0);
228 BuildMI(MBB, I, DL, Desc, Lo).addReg(VR0, RegState::Kill);
229 TII.loadRegFromStack(MBB, I, VR1, FI, RC, RegSize);
230 BuildMI(MBB, I, DL, Desc, Hi).addReg(VR1, RegState::Kill);
231}
232
233void ExpandPseudo::expandStoreACC(MachineBasicBlock &MBB, Iter I,
234 unsigned MFHiOpc, unsigned MFLoOpc,
235 unsigned RegSize) {
236 // mflo $vr0, src
237 // store $vr0, FI
238 // mfhi $vr1, src
239 // store $vr1, FI + 4
240
241 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
242
243 const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize);
246 Register Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
247 RegState SrcKill = getKillRegState(I->getOperand(0).isKill());
248 DebugLoc DL = I->getDebugLoc();
249
250 BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src);
251 TII.storeRegToStack(MBB, I, VR0, true, FI, RC, 0);
252 BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill);
253 TII.storeRegToStack(MBB, I, VR1, true, FI, RC, RegSize);
254}
255
256bool ExpandPseudo::expandCopy(MachineBasicBlock &MBB, Iter I) {
257 Register Src = I->getOperand(1).getReg();
258 std::pair<unsigned, unsigned> Opcodes = getMFHiLoOpc(Src);
259
260 if (!Opcodes.first)
261 return false;
262
263 return expandCopyACC(MBB, I, Opcodes.first, Opcodes.second);
264}
265
266bool ExpandPseudo::expandCopyACC(MachineBasicBlock &MBB, Iter I,
267 unsigned MFHiOpc, unsigned MFLoOpc) {
268 // mflo $vr0, src
269 // copy dst_lo, $vr0
270 // mfhi $vr1, src
271 // copy dst_hi, $vr1
272
273 unsigned Dst = I->getOperand(0).getReg(), Src = I->getOperand(1).getReg();
274 const TargetRegisterClass *DstRC = RegInfo.getMinimalPhysRegClass(Dst);
275 unsigned VRegSize = RegInfo.getRegSizeInBits(*DstRC) / 16;
276 const TargetRegisterClass *RC = RegInfo.intRegClass(VRegSize);
279 RegState SrcKill = getKillRegState(I->getOperand(1).isKill());
280 Register DstLo = RegInfo.getSubReg(Dst, Mips::sub_lo);
281 Register DstHi = RegInfo.getSubReg(Dst, Mips::sub_hi);
282 DebugLoc DL = I->getDebugLoc();
283
284 BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src);
285 BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstLo)
286 .addReg(VR0, RegState::Kill);
287 BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill);
288 BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstHi)
289 .addReg(VR1, RegState::Kill);
290 return true;
291}
292
293/// This method expands the same instruction that MipsSEInstrInfo::
294/// expandBuildPairF64 does, for FPXX/FP64 when mthc1 is not available and
295/// for FP64A. It is implemented here
296/// because frame indexes are eliminated before MipsSEInstrInfo::
297/// expandBuildPairF64 is called.
298bool ExpandPseudo::expandBuildPairF64(MachineBasicBlock &MBB,
300 bool FP64) const {
301 // For FPXX/FP64 when mthc1 is not available, use:
302 // spill + reload via ldc1
303 //
304 // The case where 64-bit GPRs can be used doesn't need to be handled here
305 // because it never creates a BuildPairF64 node.
306 //
307 // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence
308 // for odd-numbered double precision values (because the lower 32-bits is
309 // transferred with mtc1 which is redirected to the upper half of the even
310 // register). Unfortunately, we have to make this decision before register
311 // allocation so for now we use a spill/reload sequence for all
312 // double-precision values in regardless of being an odd/even register.
313 //
314 // For the cases that should be covered here MipsSEISelDAGToDAG adds $sp as
315 // implicit operand, so other passes (like ShrinkWrapping) are aware that
316 // stack is used.
317 if (I->getNumOperands() == 4 && I->getOperand(3).isReg()
318 && I->getOperand(3).getReg() == Mips::SP) {
319 Register DstReg = I->getOperand(0).getReg();
320 Register LoReg = I->getOperand(1).getReg();
321 Register HiReg = I->getOperand(2).getReg();
322
323 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
324 const TargetRegisterClass *RC2 =
325 FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
326
327 // We re-use the same spill slot each time so that the stack frame doesn't
328 // grow too much in functions with a large number of moves.
329 int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(MF, RC2);
330 if (!Subtarget.isLittle())
331 std::swap(LoReg, HiReg);
332 TII.storeRegToStack(MBB, I, LoReg, I->getOperand(1).isKill(), FI, RC, 0);
333 TII.storeRegToStack(MBB, I, HiReg, I->getOperand(2).isKill(), FI, RC, 4);
334 TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, 0);
335 return true;
336 }
337
338 return false;
339}
340
341/// This method expands the same instruction that MipsSEInstrInfo::
342/// expandExtractElementF64 does, for FPXX/FP64 when mfhc1 is not available and
343/// for FP64A. It is implemented here
344/// because frame indexes are eliminated before MipsSEInstrInfo::
345/// expandExtractElementF64 is called.
346bool ExpandPseudo::expandExtractElementF64(MachineBasicBlock &MBB,
348 bool FP64) const {
349 const MachineOperand &Op1 = I->getOperand(1);
350 const MachineOperand &Op2 = I->getOperand(2);
351
352 if ((Op1.isReg() && Op1.isUndef()) || (Op2.isReg() && Op2.isUndef())) {
353 Register DstReg = I->getOperand(0).getReg();
354 BuildMI(MBB, I, I->getDebugLoc(), TII.get(Mips::IMPLICIT_DEF), DstReg);
355 return true;
356 }
357
358 // For FPXX/FP64 when mfhc1 is not available, use:
359 // spill + reload via ldc1
360 //
361 // The case where 64-bit GPRs can be used doesn't need to be handled here
362 // because it never creates a ExtractElementF64 node.
363 //
364 // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence
365 // for odd-numbered double precision values (because the lower 32-bits is
366 // transferred with mfc1 which is redirected to the upper half of the even
367 // register). Unfortunately, we have to make this decision before register
368 // allocation so for now we use a spill/reload sequence for all
369 // double-precision values in regardless of being an odd/even register.
370 //
371 // For the cases that should be covered here MipsSEISelDAGToDAG adds $sp as
372 // implicit operand, so other passes (like ShrinkWrapping) are aware that
373 // stack is used.
374 if (I->getNumOperands() == 4 && I->getOperand(3).isReg()
375 && I->getOperand(3).getReg() == Mips::SP) {
376 Register DstReg = I->getOperand(0).getReg();
377 Register SrcReg = Op1.getReg();
378 unsigned N = Op2.getImm();
379 int64_t Offset = 4 * (Subtarget.isLittle() ? N : (1 - N));
380
381 const TargetRegisterClass *RC =
382 FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
383 const TargetRegisterClass *RC2 = &Mips::GPR32RegClass;
384
385 // We re-use the same spill slot each time so that the stack frame doesn't
386 // grow too much in functions with a large number of moves.
387 int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(MF, RC);
388 TII.storeRegToStack(MBB, I, SrcReg, Op1.isKill(), FI, RC, 0);
389 TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, Offset);
390 return true;
391 }
392
393 return false;
394}
395
398
400 MachineBasicBlock &MBB) const {
401 MachineFrameInfo &MFI = MF.getFrameInfo();
403
404 const MipsSEInstrInfo &TII =
405 *static_cast<const MipsSEInstrInfo *>(STI.getInstrInfo());
406 const MipsRegisterInfo &RegInfo = *STI.getRegisterInfo();
407
409 DebugLoc dl;
410 MipsABIInfo ABI = STI.getABI();
411 unsigned SP = ABI.GetStackPtr();
412 unsigned FP = ABI.GetFramePtr();
413 unsigned ZERO = ABI.GetNullPtr();
414 unsigned MOVE = ABI.GetGPRMoveOp();
415 unsigned ADDiu = ABI.GetPtrAddiuOp();
416 unsigned AND = ABI.GetPtrAndOp();
417
418 const TargetRegisterClass *RC = ABI.ArePtrs64bit() ?
419 &Mips::GPR64RegClass : &Mips::GPR32RegClass;
420
421 // First, compute final stack size.
422 uint64_t StackSize = MFI.getStackSize();
423
424 // No need to allocate space on the stack.
425 if (StackSize == 0 && !MFI.adjustsStack()) return;
426
428
429 // Adjust stack.
430 TII.adjustStackPtr(SP, -StackSize, MBB, MBBI);
431 CFIBuilder.buildDefCFAOffset(StackSize);
432
433 if (MF.getFunction().hasFnAttribute("interrupt"))
434 emitInterruptPrologueStub(MF, MBB);
435
436 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
437
438 // Find the instruction past the last instruction that saves a callee-saved
439 // register to the stack.
440 std::advance(MBBI, getNumCSRInstrs(STI, CSI));
441 CFIBuilder.setInsertPoint(MBBI);
442
443 if (!CSI.empty()) {
444 // Iterate over list of callee-saved registers and emit .cfi_offset
445 // directives.
446 for (const CalleeSavedInfo &I : CSI) {
447 int64_t Offset = MFI.getObjectOffset(I.getFrameIdx());
448 MCRegister Reg = I.getReg();
449
450 // If Reg is a double precision register, emit two cfa_offsets,
451 // one for each of the paired single precision registers.
452 if (Mips::AFGR64RegClass.contains(Reg)) {
453 MCRegister Reg0 = RegInfo.getSubReg(Reg, Mips::sub_lo);
454 MCRegister Reg1 = RegInfo.getSubReg(Reg, Mips::sub_hi);
455
456 if (!STI.isLittle())
457 std::swap(Reg0, Reg1);
458
459 CFIBuilder.buildOffset(Reg0, Offset);
460 CFIBuilder.buildOffset(Reg1, Offset + 4);
461 } else if (Mips::FGR64RegClass.contains(Reg)) {
462 MCRegister Reg0 = Reg;
463 MCRegister Reg1 = Reg + 1;
464
465 if (!STI.isLittle())
466 std::swap(Reg0, Reg1);
467
468 CFIBuilder.buildOffset(Reg0, Offset);
469 CFIBuilder.buildOffset(Reg1, Offset + 4);
470 } else {
471 // Reg is either in GPR32 or FGR32.
472 CFIBuilder.buildOffset(Reg, Offset);
473 }
474 }
475 }
476
477 if (MipsFI->callsEhReturn()) {
478 // Insert instructions that spill eh data registers.
479 for (int I = 0; I < 4; ++I) {
480 if (!MBB.isLiveIn(ABI.GetEhDataReg(I)))
481 MBB.addLiveIn(ABI.GetEhDataReg(I));
482 TII.storeRegToStackSlot(MBB, MBBI, ABI.GetEhDataReg(I), false,
483 MipsFI->getEhDataRegFI(I), RC, Register());
484 }
485
486 // Emit .cfi_offset directives for eh data registers.
487 for (int I = 0; I < 4; ++I) {
488 int64_t Offset = MFI.getObjectOffset(MipsFI->getEhDataRegFI(I));
489 CFIBuilder.buildOffset(ABI.GetEhDataReg(I), Offset);
490 }
491 }
492
493 // if framepointer enabled, set it to point to the stack pointer.
494 if (hasFP(MF)) {
495 // Insert instruction "move $fp, $sp" at this location.
496 BuildMI(MBB, MBBI, dl, TII.get(MOVE), FP).addReg(SP).addReg(ZERO)
498
499 CFIBuilder.buildDefCFARegister(FP);
500
501 if (RegInfo.hasStackRealignment(MF)) {
502 // addiu $Reg, $zero, -MaxAlignment
503 // andi $sp, $sp, $Reg
505 assert((Log2(MFI.getMaxAlign()) < 16) &&
506 "Function's alignment size requirement is not supported.");
507 int64_t MaxAlign = -(int64_t)MFI.getMaxAlign().value();
508
509 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), VR).addReg(ZERO).addImm(MaxAlign);
510 BuildMI(MBB, MBBI, dl, TII.get(AND), SP).addReg(SP).addReg(VR);
511
512 if (hasBP(MF)) {
513 // move $s7, $sp
514 unsigned BP = STI.getABI().GetBasePtr();
515 BuildMI(MBB, MBBI, dl, TII.get(MOVE), BP)
516 .addReg(SP)
517 .addReg(ZERO);
518 }
519 }
520 }
521
522 // Mark all the instructions added to the prologue as frame setup, so the
523 // scavenger doesn't place emergency spills above them.
524 for (MachineInstr &MI : make_range(MBB.begin(), MBBI))
526}
527
528void MipsSEFrameLowering::emitInterruptPrologueStub(
532 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
533
534 // Report an error the target doesn't support Mips32r2 or later.
535 // The epilogue relies on the use of the "ehb" to clear execution
536 // hazards. Pre R2 Mips relies on an implementation defined number
537 // of "ssnop"s to clear the execution hazard. Support for ssnop hazard
538 // clearing is not provided so reject that configuration.
539 if (!STI.hasMips32r2())
541 "\"interrupt\" attribute is not supported on pre-MIPS32R2 or "
542 "MIPS16 targets.");
543
544 // The GP register contains the "user" value, so we cannot perform
545 // any gp relative loads until we restore the "kernel" or "system" gp
546 // value. Until support is written we shall only accept the static
547 // relocation model.
549 report_fatal_error("\"interrupt\" attribute is only supported for the "
550 "static relocation model on MIPS at the present time.");
551
552 if (!STI.isABI_O32() || STI.hasMips64())
553 report_fatal_error("\"interrupt\" attribute is only supported for the "
554 "O32 ABI on MIPS32R2+ at the present time.");
555
556 // Perform ISR handling like GCC
557 StringRef IntKind =
558 MF.getFunction().getFnAttribute("interrupt").getValueAsString();
559 const TargetRegisterClass *PtrRC = &Mips::GPR32RegClass;
560
561 // EIC interrupt handling needs to read the Cause register to disable
562 // interrupts.
563 if (IntKind == "eic") {
564 // Coprocessor registers are always live per se.
565 MBB.addLiveIn(Mips::COP013);
566 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MFC0), Mips::R26)
567 .addReg(Mips::COP013)
569
570 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::EXT), Mips::R26)
571 .addReg(Mips::R26)
572 .addImm(10)
573 .addImm(6)
575 }
576
577 // Fetch and spill EPC
578 MBB.addLiveIn(Mips::COP014);
579 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MFC0), Mips::R27)
580 .addReg(Mips::COP014)
582
583 STI.getInstrInfo()->storeRegToStack(MBB, MBBI, Mips::R27, false,
584 MipsFI->getISRRegFI(0), PtrRC, 0);
585
586 // Fetch and Spill Status
587 MBB.addLiveIn(Mips::COP012);
588 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MFC0), Mips::R27)
589 .addReg(Mips::COP012)
591
592 STI.getInstrInfo()->storeRegToStack(MBB, MBBI, Mips::R27, false,
593 MipsFI->getISRRegFI(1), PtrRC, 0);
594
595 // Build the configuration for disabling lower priority interrupts. Non EIC
596 // interrupts need to be masked off with zero, EIC from the Cause register.
597 unsigned InsPosition = 8;
598 unsigned InsSize = 0;
599 unsigned SrcReg = Mips::R0;
600
601 // If the interrupt we're tied to is the EIC, switch the source for the
602 // masking off interrupts to the cause register.
603 if (IntKind == "eic") {
604 SrcReg = Mips::R26;
605 InsPosition = 10;
606 InsSize = 6;
607 } else
608 InsSize = StringSwitch<unsigned>(IntKind)
609 .Case("sw0", 1)
610 .Case("sw1", 2)
611 .Case("hw0", 3)
612 .Case("hw1", 4)
613 .Case("hw2", 5)
614 .Case("hw3", 6)
615 .Case("hw4", 7)
616 .Case("hw5", 8)
617 .Default(0);
618 assert(InsSize != 0 && "Unknown interrupt type!");
619
620 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::INS), Mips::R27)
621 .addReg(SrcReg)
622 .addImm(InsPosition)
623 .addImm(InsSize)
624 .addReg(Mips::R27)
626
627 // Mask off KSU, ERL, EXL
628 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::INS), Mips::R27)
629 .addReg(Mips::R0)
630 .addImm(1)
631 .addImm(4)
632 .addReg(Mips::R27)
634
635 // Disable the FPU as we are not spilling those register sets.
636 if (!STI.useSoftFloat())
637 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::INS), Mips::R27)
638 .addReg(Mips::R0)
639 .addImm(29)
640 .addImm(1)
641 .addReg(Mips::R27)
643
644 // Set the new status
645 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MTC0), Mips::COP012)
646 .addReg(Mips::R27)
648}
649
651 MachineBasicBlock &MBB) const {
652 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
653 MachineFrameInfo &MFI = MF.getFrameInfo();
655
656 const MipsSEInstrInfo &TII =
657 *static_cast<const MipsSEInstrInfo *>(STI.getInstrInfo());
658
659 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
660 MipsABIInfo ABI = STI.getABI();
661 unsigned SP = ABI.GetStackPtr();
662 unsigned FP = ABI.GetFramePtr();
663 unsigned ZERO = ABI.GetNullPtr();
664 unsigned MOVE = ABI.GetGPRMoveOp();
665
666 // if framepointer enabled, restore the stack pointer.
667 if (hasFP(MF)) {
668 // Find the first instruction that restores a callee-saved register.
670
671 std::advance(I, -int(getNumCSRInstrs(STI, MFI.getCalleeSavedInfo())));
672
673 // Insert instruction "move $sp, $fp" at this location.
674 BuildMI(MBB, I, DL, TII.get(MOVE), SP).addReg(FP).addReg(ZERO);
675 }
676
677 if (MipsFI->callsEhReturn()) {
678 const TargetRegisterClass *RC =
679 ABI.ArePtrs64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
680
681 // Find first instruction that restores a callee-saved register.
683 std::advance(I, -int(getNumCSRInstrs(STI, MFI.getCalleeSavedInfo())));
684
685 // Insert instructions that restore eh data registers.
686 for (int J = 0; J < 4; ++J) {
687 TII.loadRegFromStackSlot(MBB, I, ABI.GetEhDataReg(J),
688 MipsFI->getEhDataRegFI(J), RC, Register());
689 }
690 }
691
692 if (MF.getFunction().hasFnAttribute("interrupt"))
693 emitInterruptEpilogueStub(MF, MBB);
694
695 // Get the number of bytes from FrameInfo
696 uint64_t StackSize = MFI.getStackSize();
697
698 if (!StackSize)
699 return;
700
701 // Adjust stack.
702 TII.adjustStackPtr(SP, StackSize, MBB, MBBI);
703}
704
705void MipsSEFrameLowering::emitInterruptEpilogueStub(
707 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
709 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
710
711 // Perform ISR handling like GCC
712 const TargetRegisterClass *PtrRC = &Mips::GPR32RegClass;
713
714 // Disable Interrupts.
715 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::DI), Mips::R0);
716 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::EHB));
717
718 // Restore EPC
720 MBB, MBBI, Mips::R27, MipsFI->getISRRegFI(0), PtrRC, Register());
721 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MTC0), Mips::COP014)
722 .addReg(Mips::R27);
723
724 // Restore Status
726 MBB, MBBI, Mips::R27, MipsFI->getISRRegFI(1), PtrRC, Register());
727 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MTC0), Mips::COP012)
728 .addReg(Mips::R27);
729}
730
733 Register &FrameReg) const {
734 const MachineFrameInfo &MFI = MF.getFrameInfo();
735 MipsABIInfo ABI = STI.getABI();
736
737 if (MFI.isFixedObjectIndex(FI))
738 FrameReg = hasFP(MF) ? ABI.GetFramePtr() : ABI.GetStackPtr();
739 else
740 FrameReg = hasBP(MF) ? ABI.GetBasePtr() : ABI.GetStackPtr();
741
742 return StackOffset::getFixed(MFI.getObjectOffset(FI) + MFI.getStackSize() -
744 MFI.getOffsetAdjustment());
745}
746
750 MachineFunction *MF = MBB.getParent();
751 const TargetInstrInfo &TII = *STI.getInstrInfo();
752
753 for (const CalleeSavedInfo &I : CSI) {
754 // Add the callee-saved register as live-in. Do not add if the register is
755 // RA and return address is taken, because it has already been added in
756 // method MipsTargetLowering::lowerRETURNADDR.
757 // It's killed at the spill, unless the register is RA and return address
758 // is taken.
759 MCRegister Reg = I.getReg();
760 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA || Reg == Mips::RA_64)
762 if (!IsRAAndRetAddrIsTaken)
763 MBB.addLiveIn(Reg);
764
765 // ISRs require HI/LO to be spilled into kernel registers to be then
766 // spilled to the stack frame.
767 bool IsLOHI = (Reg == Mips::LO0 || Reg == Mips::LO0_64 ||
768 Reg == Mips::HI0 || Reg == Mips::HI0_64);
769 const Function &Func = MBB.getParent()->getFunction();
770 if (IsLOHI && Func.hasFnAttribute("interrupt")) {
771 DebugLoc DL = MI->getDebugLoc();
772
773 unsigned Op = 0;
774 if (!STI.getABI().ArePtrs64bit()) {
775 Op = (Reg == Mips::HI0) ? Mips::MFHI : Mips::MFLO;
776 Reg = Mips::R26;
777 } else {
778 Op = (Reg == Mips::HI0) ? Mips::MFHI64 : Mips::MFLO64;
779 Reg = Mips::R26_64;
780 }
781 BuildMI(MBB, MI, DL, TII.get(Op), Mips::R26)
783 }
784
785 // Insert the spill to the stack frame.
786 bool IsKill = !IsRAAndRetAddrIsTaken;
787 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
788 TII.storeRegToStackSlot(MBB, MI, Reg, IsKill, I.getFrameIdx(), RC,
789 Register());
790 }
791
792 return true;
793}
794
795bool
797 const MachineFrameInfo &MFI = MF.getFrameInfo();
798 // Reserve call frame if the size of the maximum call frame fits into 16-bit
799 // immediate field and there are no variable sized objects on the stack.
800 // Make sure the second register scavenger spill slot can be accessed with one
801 // instruction.
803 !MFI.hasVarSizedObjects();
804}
805
806/// Mark \p Reg and all registers aliasing it in the bitset.
807static void setAliasRegs(MachineFunction &MF, BitVector &SavedRegs,
808 unsigned Reg) {
810 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
811 SavedRegs.set(*AI);
812}
813
815 BitVector &SavedRegs,
816 RegScavenger *RS) const {
820 MipsABIInfo ABI = STI.getABI();
821 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
822 unsigned FP = ABI.GetFramePtr();
823 unsigned BP = ABI.GetBasePtr();
824
825 // Mark $ra and $fp as used if function has dedicated frame pointer.
826 if (hasFP(MF)) {
827 setAliasRegs(MF, SavedRegs, RA);
828 setAliasRegs(MF, SavedRegs, FP);
829 }
830 // Mark $s7 as used if function has dedicated base pointer.
831 if (hasBP(MF))
832 setAliasRegs(MF, SavedRegs, BP);
833
834 // Create spill slots for eh data registers if function calls eh_return.
835 if (MipsFI->callsEhReturn())
836 MipsFI->createEhDataRegsFI(MF);
837
838 // Create spill slots for Coprocessor 0 registers if function is an ISR.
839 if (MipsFI->isISR())
840 MipsFI->createISRRegFI(MF);
841
842 // Expand pseudo instructions which load, store or copy accumulators.
843 // Add an emergency spill slot if a pseudo was expanded.
844 if (ExpandPseudo(MF).expand()) {
845 // The spill slot should be half the size of the accumulator. If target have
846 // general-purpose registers 64 bits wide, it should be 64-bit, otherwise
847 // it should be 32-bit.
848 const TargetRegisterClass &RC = STI.isGP64bit() ?
849 Mips::GPR64RegClass : Mips::GPR32RegClass;
850 int FI = MF.getFrameInfo().CreateSpillStackObject(TRI->getSpillSize(RC),
851 TRI->getSpillAlign(RC));
852 RS->addScavengingFrameIndex(FI);
853 }
854
855 // Set scavenging frame index if necessary.
856 uint64_t MaxSPOffset = estimateStackSize(MF);
857
858 // MSA has a minimum offset of 10 bits signed. If there is a variable
859 // sized object on the stack, the estimation cannot account for it.
860 if (isIntN(STI.hasMSA() ? 10 : 16, MaxSPOffset) &&
862 return;
863
864 const TargetRegisterClass &RC =
865 ABI.ArePtrs64bit() ? Mips::GPR64RegClass : Mips::GPR32RegClass;
866 int FI = MF.getFrameInfo().CreateSpillStackObject(TRI->getSpillSize(RC),
867 TRI->getSpillAlign(RC));
868 RS->addScavengingFrameIndex(FI);
869}
870
871const MipsFrameLowering *
const AArch64Subtarget & Subtarget
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file implements the BitVector class.
@ ZERO
Special weight used for cases with exact zero probability.
static Expected< BitVector > expand(StringRef S, StringRef Original)
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static std::pair< unsigned, unsigned > getMFHiLoOpc(unsigned Src)
static unsigned getNumCSRInstrs(const MipsSubtarget &STI, ArrayRef< CalleeSavedInfo > CSI)
Return the number of instructions that save (or restore) the callee-saved registers in CSI.
static void setAliasRegs(MachineFunction &MF, BitVector &SavedRegs, unsigned Reg)
Mark Reg and all registers aliasing it in the bitset.
MachineRegisterInfo & MRI
MachineInstr *& Lo
This file declares the machine register scavenger class.
SI optimize exec mask operations pre RA
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
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
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:367
Helper class for creating CFI instructions and inserting them into MIR.
void buildDefCFAOffset(int64_t Offset, MCSymbol *Label=nullptr) const
void buildDefCFARegister(MCRegister Reg) const
void buildOffset(MCRegister Reg, int64_t Offset) const
void setInsertPoint(MachineBasicBlock::iterator IP)
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
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:766
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
MCRegAliasIterator enumerates all registers aliasing Reg.
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
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 isReturnAddressTaken() 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.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
int64_t getOffsetAdjustment() const
Return the correction for frame offsets.
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.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
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 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.
Representation of each machine instruction.
const MachineOperand & getOperand(unsigned i) const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
MipsFrameLowering(const MipsSubtarget &sti, Align Alignment)
bool hasBP(const MachineFunction &MF) const
uint64_t estimateStackSize(const MachineFunction &MF) const
const MipsSubtarget & STI
MipsFunctionInfo - This class is derived from MachineFunction private Mips target-specific informatio...
int getEhDataRegFI(unsigned Reg) const
int getISRRegFI(Register Reg) const
void createEhDataRegsFI(MachineFunction &MF)
void createISRRegFI(MachineFunction &MF)
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
virtual void storeRegToStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, int64_t Offset, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const =0
virtual const TargetRegisterClass * intRegClass(unsigned Size) const =0
Return GPR register class.
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
MipsSEFrameLowering(const MipsSubtarget &STI)
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...
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
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 emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
const MipsInstrInfo * getInstrInfo() const override
bool hasMips64() const
bool hasMips32r2() const
Reloc::Model getRelocationModel() const
bool hasMips2() const
Wrapper class representing virtual and physical registers.
Definition Register.h:20
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:31
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
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
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...
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
constexpr MCPhysReg RA_64
constexpr MCPhysReg SP
constexpr MCPhysReg RA
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
RegState
Flags to represent properties of register accesses.
constexpr RegState getKillRegState(bool B)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
const MipsFrameLowering * createMipsSEFrameLowering(const MipsSubtarget &ST)
Op::Description Desc
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
DWARFExpression::Operation Op
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2035
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:249
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:202
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
#define N
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:82