1 //===- MipsRegisterInfo.cpp - MIPS Register Information -== -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Bruno Cardoso Lopes and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the MIPS implementation of the MRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mips-reg-info"
17 #include "MipsRegisterInfo.h"
18 #include "MipsMachineFunction.h"
19 #include "llvm/Constants.h"
20 #include "llvm/Type.h"
21 #include "llvm/Function.h"
22 #include "llvm/CodeGen/ValueTypes.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineLocation.h"
27 #include "llvm/Target/TargetFrameInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetOptions.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/ADT/BitVector.h"
34 #include "llvm/ADT/STLExtras.h"
35 //#include "MipsSubtarget.h"
39 // TODO: add subtarget support
40 MipsRegisterInfo::MipsRegisterInfo(const TargetInstrInfo &tii)
41 : MipsGenRegisterInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
44 /// getRegisterNumbering - Given the enum value for some register, e.g.
45 /// Mips::RA, return the number that it corresponds to (e.g. 31).
46 unsigned MipsRegisterInfo::
47 getRegisterNumbering(unsigned RegEnum)
50 case Mips::ZERO : return 0;
51 case Mips::AT : return 1;
52 case Mips::V0 : return 2;
53 case Mips::V1 : return 3;
54 case Mips::A0 : return 4;
55 case Mips::A1 : return 5;
56 case Mips::A2 : return 6;
57 case Mips::A3 : return 7;
58 case Mips::T0 : return 8;
59 case Mips::T1 : return 9;
60 case Mips::T2 : return 10;
61 case Mips::T3 : return 11;
62 case Mips::T4 : return 12;
63 case Mips::T5 : return 13;
64 case Mips::T6 : return 14;
65 case Mips::T7 : return 15;
66 case Mips::T8 : return 16;
67 case Mips::T9 : return 17;
68 case Mips::S0 : return 18;
69 case Mips::S1 : return 19;
70 case Mips::S2 : return 20;
71 case Mips::S3 : return 21;
72 case Mips::S4 : return 22;
73 case Mips::S5 : return 23;
74 case Mips::S6 : return 24;
75 case Mips::S7 : return 25;
76 case Mips::K0 : return 26;
77 case Mips::K1 : return 27;
78 case Mips::GP : return 28;
79 case Mips::SP : return 29;
80 case Mips::FP : return 30;
81 case Mips::RA : return 31;
82 default: assert(0 && "Unknown register number!");
86 void MipsRegisterInfo::
87 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
88 unsigned SrcReg, int FI,
89 const TargetRegisterClass *RC) const
91 if (RC == Mips::CPURegsRegisterClass)
92 BuildMI(MBB, I, TII.get(Mips::SW)).addReg(SrcReg, false, false, true)
93 .addImm(0).addFrameIndex(FI);
95 assert(0 && "Can't store this register to stack slot");
98 void MipsRegisterInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
99 SmallVector<MachineOperand,4> Addr,
100 const TargetRegisterClass *RC,
101 SmallVector<MachineInstr*, 4> &NewMIs) const {
102 if (RC != Mips::CPURegsRegisterClass)
103 assert(0 && "Can't store this register");
104 MachineInstrBuilder MIB = BuildMI(TII.get(Mips::SW))
105 .addReg(SrcReg, false, false, true);
106 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
107 MachineOperand &MO = Addr[i];
109 MIB.addReg(MO.getReg());
110 else if (MO.isImmediate())
111 MIB.addImm(MO.getImmedValue());
113 MIB.addFrameIndex(MO.getFrameIndex());
115 NewMIs.push_back(MIB);
119 void MipsRegisterInfo::
120 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
121 unsigned DestReg, int FI,
122 const TargetRegisterClass *RC) const
124 if (RC == Mips::CPURegsRegisterClass)
125 BuildMI(MBB, I, TII.get(Mips::LW), DestReg).addImm(0).addFrameIndex(FI);
127 assert(0 && "Can't load this register from stack slot");
130 void MipsRegisterInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
131 SmallVector<MachineOperand,4> Addr,
132 const TargetRegisterClass *RC,
133 SmallVector<MachineInstr*, 4> &NewMIs) const {
134 if (RC != Mips::CPURegsRegisterClass)
135 assert(0 && "Can't load this register");
136 MachineInstrBuilder MIB = BuildMI(TII.get(Mips::LW), DestReg);
137 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
138 MachineOperand &MO = Addr[i];
140 MIB.addReg(MO.getReg());
141 else if (MO.isImmediate())
142 MIB.addImm(MO.getImmedValue());
144 MIB.addFrameIndex(MO.getFrameIndex());
146 NewMIs.push_back(MIB);
150 void MipsRegisterInfo::
151 copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
152 unsigned DestReg, unsigned SrcReg,
153 const TargetRegisterClass *DestRC,
154 const TargetRegisterClass *SrcRC) const
156 if (DestRC != SrcRC) {
157 cerr << "Not yet supported!";
161 if (DestRC == Mips::CPURegsRegisterClass)
162 BuildMI(MBB, I, TII.get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
165 assert (0 && "Can't copy this register");
168 void MipsRegisterInfo::reMaterialize(MachineBasicBlock &MBB,
169 MachineBasicBlock::iterator I,
171 const MachineInstr *Orig) const
173 MachineInstr *MI = Orig->clone();
174 MI->getOperand(0).setReg(DestReg);
178 MachineInstr *MipsRegisterInfo::
179 foldMemoryOperand(MachineInstr* MI, unsigned OpNum, int FI) const
181 MachineInstr *NewMI = NULL;
183 switch (MI->getOpcode())
186 if ((MI->getOperand(0).isRegister()) &&
187 (MI->getOperand(1).isRegister()) &&
188 (MI->getOperand(1).getReg() == Mips::ZERO) &&
189 (MI->getOperand(2).isRegister()))
191 if (OpNum == 0) // COPY -> STORE
192 NewMI = BuildMI(TII.get(Mips::SW)).addFrameIndex(FI)
193 .addImm(0).addReg(MI->getOperand(2).getReg());
195 NewMI = BuildMI(TII.get(Mips::LW), MI->getOperand(0)
196 .getReg()).addImm(0).addFrameIndex(FI);
202 NewMI->copyKillDeadInfo(MI);
206 //===----------------------------------------------------------------------===//
208 // Callee Saved Registers methods
210 //===----------------------------------------------------------------------===//
212 /// Mips Callee Saved Registers
213 const unsigned* MipsRegisterInfo::
214 getCalleeSavedRegs(const MachineFunction *MF) const
216 // Mips calle-save register range is $16-$26(s0-s7)
217 static const unsigned CalleeSavedRegs[] = {
218 Mips::S0, Mips::S1, Mips::S2, Mips::S3,
219 Mips::S4, Mips::S5, Mips::S6, Mips::S7, 0
221 return CalleeSavedRegs;
224 /// Mips Callee Saved Register Classes
225 const TargetRegisterClass* const*
226 MipsRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const
228 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
229 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
230 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
231 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
232 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass, 0
234 return CalleeSavedRegClasses;
237 BitVector MipsRegisterInfo::
238 getReservedRegs(const MachineFunction &MF) const
240 BitVector Reserved(getNumRegs());
241 Reserved.set(Mips::ZERO);
242 Reserved.set(Mips::AT);
243 Reserved.set(Mips::K0);
244 Reserved.set(Mips::K1);
245 Reserved.set(Mips::GP);
246 Reserved.set(Mips::SP);
247 Reserved.set(Mips::FP);
248 Reserved.set(Mips::RA);
252 //===----------------------------------------------------------------------===//
254 // Stack Frame Processing methods
255 // +----------------------------+
257 // The stack is allocated decrementing the stack pointer on
258 // the first instruction of a function prologue. Once decremented,
259 // all stack referencesare are done thought a positive offset
260 // from the stack/frame pointer, so the stack is considering
261 // to grow up! Otherwise terrible hacks would have to be made
262 // to get this stack ABI compliant :)
264 // The stack frame required by the ABI:
269 // . saved $GP (used in PIC - not supported yet)
271 // . saved "Callee Saved" Registers
274 // StackSize -----------
276 // Offset - offset from sp after stack allocation on function prologue
278 // The sp is the stack pointer subtracted/added from the stack size
279 // at the Prologue/Epilogue
281 // References to the previous stack (to obtain arguments) are done
282 // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
285 // - reference to the actual stack frame
286 // for any local area var there is smt like : FI >= 0, StackOffset: 4
289 // - reference to previous stack frame
290 // suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
291 // The emitted instruction will be something like:
292 // lw REGX, 16+StackSize(SP)
294 // Since the total stack size is unknown on LowerFORMAL_ARGUMENTS, all
295 // stack references (ObjectOffset) created to reference the function
296 // arguments, are negative numbers. This way, on eliminateFrameIndex it's
297 // possible to detect those references and the offsets are adjusted to
298 // their real location.
302 //===----------------------------------------------------------------------===//
304 // hasFP - Return true if the specified function should have a dedicated frame
305 // pointer register. This is true if the function has variable sized allocas or
306 // if frame pointer elimination is disabled.
307 bool MipsRegisterInfo::
308 hasFP(const MachineFunction &MF) const {
309 return (NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects());
312 // This function eliminate ADJCALLSTACKDOWN,
313 // ADJCALLSTACKUP pseudo instructions
314 void MipsRegisterInfo::
315 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
316 MachineBasicBlock::iterator I) const {
317 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
321 // FrameIndex represent objects inside a abstract stack.
322 // We must replace FrameIndex with an stack/frame pointer
324 void MipsRegisterInfo::
325 eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
326 RegScavenger *RS) const
328 MachineInstr &MI = *II;
329 MachineFunction &MF = *MI.getParent()->getParent();
332 while (!MI.getOperand(i).isFrameIndex()) {
334 assert(i < MI.getNumOperands() &&
335 "Instr doesn't have FrameIndex operand!");
338 int FrameIndex = MI.getOperand(i).getFrameIndex();
339 int stackSize = MF.getFrameInfo()->getStackSize();
340 int spOffset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
343 DOUT << "\nFunction : " << MF.getFunction()->getName() << "\n";
344 DOUT << "<--------->\n";
346 DOUT << "FrameIndex : " << FrameIndex << "\n";
347 DOUT << "spOffset : " << spOffset << "\n";
348 DOUT << "stackSize : " << stackSize << "\n";
351 // as explained on LowerFORMAL_ARGUMENTS, detect negative offsets
352 // and adjust SPOffsets considering the final stack size.
353 int Offset = ((spOffset < 0) ? (stackSize + (-(spOffset+4))) : (spOffset));
354 Offset += MI.getOperand(i-1).getImm();
357 DOUT << "Offset : " << Offset << "\n";
358 DOUT << "<--------->\n";
361 MI.getOperand(i-1).ChangeToImmediate(Offset);
362 MI.getOperand(i).ChangeToRegister(getFrameRegister(MF),false);
365 void MipsRegisterInfo::
366 emitPrologue(MachineFunction &MF) const
368 MachineBasicBlock &MBB = MF.front();
369 MachineFrameInfo *MFI = MF.getFrameInfo();
370 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
371 MachineBasicBlock::iterator MBBI = MBB.begin();
372 bool isPIC = (MF.getTarget().getRelocationModel() == Reloc::PIC_);
374 // Replace the dummy '0' SPOffset by the negative
375 // offsets, as explained on LowerFORMAL_ARGUMENTS
376 MipsFI->adjustLoadArgsFI(MFI);
377 MipsFI->adjustStoreVarArgsFI(MFI);
379 // Get the number of bytes to allocate from the FrameInfo.
380 int NumBytes = (int) MFI->getStackSize();
383 DOUT << "\n<--- EMIT PROLOGUE --->\n";
384 DOUT << "Actual Stack size :" << NumBytes << "\n";
387 // No need to allocate space on the stack.
388 if (NumBytes == 0) return;
390 int FPOffset, RAOffset;
392 // Allocate space for saved RA and FP when needed
393 if ((hasFP(MF)) && (MFI->hasCalls())) {
395 RAOffset = (NumBytes+4);
397 } else if ((!hasFP(MF)) && (MFI->hasCalls())) {
401 } else if ((hasFP(MF)) && (!MFI->hasCalls())) {
407 MFI->setObjectOffset(MFI->CreateStackObject(4,4), FPOffset);
408 MFI->setObjectOffset(MFI->CreateStackObject(4,4), RAOffset);
409 MipsFI->setFPStackOffset(FPOffset);
410 MipsFI->setRAStackOffset(RAOffset);
413 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
414 NumBytes = ((NumBytes+Align-1)/Align*Align);
417 DOUT << "FPOffset :" << FPOffset << "\n";
418 DOUT << "RAOffset :" << RAOffset << "\n";
419 DOUT << "New stack size :" << NumBytes << "\n\n";
423 MFI->setStackSize(NumBytes);
425 // PIC speficic function prologue
427 BuildMI(MBB, MBBI, TII.get(Mips::CPLOAD)).addReg(Mips::T9);
429 // Adjust stack : addi sp, sp, (-imm)
430 BuildMI(MBB, MBBI, TII.get(Mips::ADDiu), Mips::SP)
431 .addReg(Mips::SP).addImm(-NumBytes);
433 // Save the return address only if the function isnt a leaf one.
434 // sw $ra, stack_loc($sp)
435 if (MFI->hasCalls()) {
436 BuildMI(MBB, MBBI, TII.get(Mips::SW))
437 .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
440 // if framepointer enabled, save it and set it
441 // to point to the stack pointer
443 // sw $fp,stack_loc($sp)
444 BuildMI(MBB, MBBI, TII.get(Mips::SW))
445 .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
448 BuildMI(MBB, MBBI, TII.get(Mips::ADDu), Mips::FP)
449 .addReg(Mips::SP).addReg(Mips::ZERO);
452 // PIC speficic function prologue
453 if ((isPIC) && (MFI->hasCalls()))
454 BuildMI(MBB, MBBI, TII.get(Mips::CPRESTORE))
455 .addImm(MipsFI->getGPStackOffset());
459 void MipsRegisterInfo::
460 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const
462 MachineBasicBlock::iterator MBBI = prior(MBB.end());
463 MachineFrameInfo *MFI = MF.getFrameInfo();
464 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
466 // Get the number of bytes from FrameInfo
467 int NumBytes = (int) MFI->getStackSize();
469 // Get the FI's where RA and FP are saved.
470 int FPOffset = MipsFI->getFPStackOffset();
471 int RAOffset = MipsFI->getRAStackOffset();
473 // if framepointer enabled, restore it and restore the
477 BuildMI(MBB, MBBI, TII.get(Mips::ADDu), Mips::SP)
478 .addReg(Mips::FP).addReg(Mips::ZERO);
480 // lw $fp,stack_loc($sp)
481 BuildMI(MBB, MBBI, TII.get(Mips::LW))
482 .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
485 // Restore the return address only if the function isnt a leaf one.
486 // lw $ra, stack_loc($sp)
487 if (MFI->hasCalls()) {
488 BuildMI(MBB, MBBI, TII.get(Mips::LW))
489 .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
492 // adjust stack : insert addi sp, sp, (imm)
494 BuildMI(MBB, MBBI, TII.get(Mips::ADDiu), Mips::SP)
495 .addReg(Mips::SP).addImm(NumBytes);
499 void MipsRegisterInfo::
500 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
503 unsigned MipsRegisterInfo::
504 getRARegister() const {
508 unsigned MipsRegisterInfo::
509 getFrameRegister(MachineFunction &MF) const {
510 return hasFP(MF) ? Mips::FP : Mips::SP;
513 unsigned MipsRegisterInfo::
514 getEHExceptionRegister() const {
515 assert(0 && "What is the exception register");
519 unsigned MipsRegisterInfo::
520 getEHHandlerRegister() const {
521 assert(0 && "What is the exception handler register");
525 #include "MipsGenRegisterInfo.inc"