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