Change allowsUnalignedMemoryAccesses to take type argument since some targets
[llvm/avr.git] / lib / Target / Mips / MipsRegisterInfo.cpp
blob51fbf84b0ea395e989a731f028ed300be261f694
1 //===- MipsRegisterInfo.cpp - MIPS Register Information -== -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the MIPS implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mips-reg-info"
16 #include "Mips.h"
17 #include "MipsSubtarget.h"
18 #include "MipsRegisterInfo.h"
19 #include "MipsMachineFunction.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Type.h"
22 #include "llvm/Function.h"
23 #include "llvm/CodeGen/ValueTypes.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineLocation.h"
28 #include "llvm/Target/TargetFrameInfo.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include "llvm/Target/TargetInstrInfo.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/ADT/BitVector.h"
37 #include "llvm/ADT/STLExtras.h"
39 using namespace llvm;
41 MipsRegisterInfo::MipsRegisterInfo(const MipsSubtarget &ST,
42 const TargetInstrInfo &tii)
43 : MipsGenRegisterInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
44 Subtarget(ST), TII(tii) {}
46 /// getRegisterNumbering - Given the enum value for some register, e.g.
47 /// Mips::RA, return the number that it corresponds to (e.g. 31).
48 unsigned MipsRegisterInfo::
49 getRegisterNumbering(unsigned RegEnum)
51 switch (RegEnum) {
52 case Mips::ZERO : case Mips::F0 : case Mips::D0 : return 0;
53 case Mips::AT : case Mips::F1 : return 1;
54 case Mips::V0 : case Mips::F2 : case Mips::D1 : return 2;
55 case Mips::V1 : case Mips::F3 : return 3;
56 case Mips::A0 : case Mips::F4 : case Mips::D2 : return 4;
57 case Mips::A1 : case Mips::F5 : return 5;
58 case Mips::A2 : case Mips::F6 : case Mips::D3 : return 6;
59 case Mips::A3 : case Mips::F7 : return 7;
60 case Mips::T0 : case Mips::F8 : case Mips::D4 : return 8;
61 case Mips::T1 : case Mips::F9 : return 9;
62 case Mips::T2 : case Mips::F10: case Mips::D5: return 10;
63 case Mips::T3 : case Mips::F11: return 11;
64 case Mips::T4 : case Mips::F12: case Mips::D6: return 12;
65 case Mips::T5 : case Mips::F13: return 13;
66 case Mips::T6 : case Mips::F14: case Mips::D7: return 14;
67 case Mips::T7 : case Mips::F15: return 15;
68 case Mips::T8 : case Mips::F16: case Mips::D8: return 16;
69 case Mips::T9 : case Mips::F17: return 17;
70 case Mips::S0 : case Mips::F18: case Mips::D9: return 18;
71 case Mips::S1 : case Mips::F19: return 19;
72 case Mips::S2 : case Mips::F20: case Mips::D10: return 20;
73 case Mips::S3 : case Mips::F21: return 21;
74 case Mips::S4 : case Mips::F22: case Mips::D11: return 22;
75 case Mips::S5 : case Mips::F23: return 23;
76 case Mips::S6 : case Mips::F24: case Mips::D12: return 24;
77 case Mips::S7 : case Mips::F25: return 25;
78 case Mips::K0 : case Mips::F26: case Mips::D13: return 26;
79 case Mips::K1 : case Mips::F27: return 27;
80 case Mips::GP : case Mips::F28: case Mips::D14: return 28;
81 case Mips::SP : case Mips::F29: return 29;
82 case Mips::FP : case Mips::F30: case Mips::D15: return 30;
83 case Mips::RA : case Mips::F31: return 31;
84 default: llvm_unreachable("Unknown register number!");
86 return 0; // Not reached
89 unsigned MipsRegisterInfo::getPICCallReg() { return Mips::T9; }
91 //===----------------------------------------------------------------------===//
92 // Callee Saved Registers methods
93 //===----------------------------------------------------------------------===//
95 /// Mips Callee Saved Registers
96 const unsigned* MipsRegisterInfo::
97 getCalleeSavedRegs(const MachineFunction *MF) const
99 // Mips callee-save register range is $16-$23, $f20-$f30
100 static const unsigned SingleFloatOnlyCalleeSavedRegs[] = {
101 Mips::S0, Mips::S1, Mips::S2, Mips::S3,
102 Mips::S4, Mips::S5, Mips::S6, Mips::S7,
103 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24, Mips::F25,
104 Mips::F26, Mips::F27, Mips::F28, Mips::F29, Mips::F30, 0
107 static const unsigned BitMode32CalleeSavedRegs[] = {
108 Mips::S0, Mips::S1, Mips::S2, Mips::S3,
109 Mips::S4, Mips::S5, Mips::S6, Mips::S7,
110 Mips::F20, Mips::F22, Mips::F24, Mips::F26, Mips::F28, Mips::F30,
111 Mips::D10, Mips::D11, Mips::D12, Mips::D13, Mips::D14, Mips::D15,0
114 if (Subtarget.isSingleFloat())
115 return SingleFloatOnlyCalleeSavedRegs;
116 else
117 return BitMode32CalleeSavedRegs;
120 /// Mips Callee Saved Register Classes
121 const TargetRegisterClass* const*
122 MipsRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const
124 static const TargetRegisterClass * const SingleFloatOnlyCalleeSavedRC[] = {
125 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
126 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
127 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
128 &Mips::FGR32RegClass, &Mips::FGR32RegClass, &Mips::FGR32RegClass,
129 &Mips::FGR32RegClass, &Mips::FGR32RegClass, &Mips::FGR32RegClass,
130 &Mips::FGR32RegClass, &Mips::FGR32RegClass, &Mips::FGR32RegClass,
131 &Mips::FGR32RegClass, &Mips::FGR32RegClass, 0
134 static const TargetRegisterClass * const BitMode32CalleeSavedRC[] = {
135 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
136 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
137 &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
138 &Mips::FGR32RegClass, &Mips::FGR32RegClass, &Mips::FGR32RegClass,
139 &Mips::FGR32RegClass, &Mips::FGR32RegClass, &Mips::FGR32RegClass,
140 &Mips::AFGR64RegClass, &Mips::AFGR64RegClass, &Mips::AFGR64RegClass,
141 &Mips::AFGR64RegClass, &Mips::AFGR64RegClass, &Mips::AFGR64RegClass, 0
144 if (Subtarget.isSingleFloat())
145 return SingleFloatOnlyCalleeSavedRC;
146 else
147 return BitMode32CalleeSavedRC;
150 BitVector MipsRegisterInfo::
151 getReservedRegs(const MachineFunction &MF) const
153 BitVector Reserved(getNumRegs());
154 Reserved.set(Mips::ZERO);
155 Reserved.set(Mips::AT);
156 Reserved.set(Mips::K0);
157 Reserved.set(Mips::K1);
158 Reserved.set(Mips::GP);
159 Reserved.set(Mips::SP);
160 Reserved.set(Mips::FP);
161 Reserved.set(Mips::RA);
163 // SRV4 requires that odd register can't be used.
164 if (!Subtarget.isSingleFloat())
165 for (unsigned FReg=(Mips::F0)+1; FReg < Mips::F30; FReg+=2)
166 Reserved.set(FReg);
168 return Reserved;
171 //===----------------------------------------------------------------------===//
173 // Stack Frame Processing methods
174 // +----------------------------+
176 // The stack is allocated decrementing the stack pointer on
177 // the first instruction of a function prologue. Once decremented,
178 // all stack referencesare are done thought a positive offset
179 // from the stack/frame pointer, so the stack is considering
180 // to grow up! Otherwise terrible hacks would have to be made
181 // to get this stack ABI compliant :)
183 // The stack frame required by the ABI (after call):
184 // Offset
186 // 0 ----------
187 // 4 Args to pass
188 // . saved $GP (used in PIC)
189 // . Alloca allocations
190 // . Local Area
191 // . CPU "Callee Saved" Registers
192 // . saved FP
193 // . saved RA
194 // . FPU "Callee Saved" Registers
195 // StackSize -----------
197 // Offset - offset from sp after stack allocation on function prologue
199 // The sp is the stack pointer subtracted/added from the stack size
200 // at the Prologue/Epilogue
202 // References to the previous stack (to obtain arguments) are done
203 // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
205 // Examples:
206 // - reference to the actual stack frame
207 // for any local area var there is smt like : FI >= 0, StackOffset: 4
208 // sw REGX, 4(SP)
210 // - reference to previous stack frame
211 // suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
212 // The emitted instruction will be something like:
213 // lw REGX, 16+StackSize(SP)
215 // Since the total stack size is unknown on LowerFormalArguments, all
216 // stack references (ObjectOffset) created to reference the function
217 // arguments, are negative numbers. This way, on eliminateFrameIndex it's
218 // possible to detect those references and the offsets are adjusted to
219 // their real location.
221 //===----------------------------------------------------------------------===//
223 void MipsRegisterInfo::adjustMipsStackFrame(MachineFunction &MF) const
225 MachineFrameInfo *MFI = MF.getFrameInfo();
226 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
227 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
228 unsigned StackAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
230 // Min and Max CSI FrameIndex.
231 int MinCSFI = -1, MaxCSFI = -1;
233 // See the description at MipsMachineFunction.h
234 int TopCPUSavedRegOff = -1, TopFPUSavedRegOff = -1;
236 // Replace the dummy '0' SPOffset by the negative offsets, as explained on
237 // LowerFormalArguments. Leaving '0' for while is necessary to avoid
238 // the approach done by calculateFrameObjectOffsets to the stack frame.
239 MipsFI->adjustLoadArgsFI(MFI);
240 MipsFI->adjustStoreVarArgsFI(MFI);
242 // It happens that the default stack frame allocation order does not directly
243 // map to the convention used for mips. So we must fix it. We move the callee
244 // save register slots after the local variables area, as described in the
245 // stack frame above.
246 unsigned CalleeSavedAreaSize = 0;
247 if (!CSI.empty()) {
248 MinCSFI = CSI[0].getFrameIdx();
249 MaxCSFI = CSI[CSI.size()-1].getFrameIdx();
251 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
252 CalleeSavedAreaSize += MFI->getObjectAlignment(CSI[i].getFrameIdx());
254 // Adjust local variables. They should come on the stack right
255 // after the arguments.
256 int LastOffsetFI = -1;
257 for (int i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
258 if (i >= MinCSFI && i <= MaxCSFI)
259 continue;
260 if (MFI->isDeadObjectIndex(i))
261 continue;
262 unsigned Offset = MFI->getObjectOffset(i) - CalleeSavedAreaSize;
263 if (LastOffsetFI == -1)
264 LastOffsetFI = i;
265 if (Offset > MFI->getObjectOffset(LastOffsetFI))
266 LastOffsetFI = i;
267 MFI->setObjectOffset(i, Offset);
270 // Adjust CPU Callee Saved Registers Area. Registers RA and FP must
271 // be saved in this CPU Area there is the need. This whole Area must
272 // be aligned to the default Stack Alignment requirements.
273 unsigned StackOffset = 0;
274 unsigned RegSize = Subtarget.isGP32bit() ? 4 : 8;
276 if (LastOffsetFI >= 0)
277 StackOffset = MFI->getObjectOffset(LastOffsetFI)+
278 MFI->getObjectSize(LastOffsetFI);
279 StackOffset = ((StackOffset+StackAlign-1)/StackAlign*StackAlign);
281 for (unsigned i = 0, e = CSI.size(); i != e ; ++i) {
282 if (CSI[i].getRegClass() != Mips::CPURegsRegisterClass)
283 break;
284 MFI->setObjectOffset(CSI[i].getFrameIdx(), StackOffset);
285 TopCPUSavedRegOff = StackOffset;
286 StackOffset += MFI->getObjectAlignment(CSI[i].getFrameIdx());
289 if (hasFP(MF)) {
290 MFI->setObjectOffset(MFI->CreateStackObject(RegSize, RegSize),
291 StackOffset);
292 MipsFI->setFPStackOffset(StackOffset);
293 TopCPUSavedRegOff = StackOffset;
294 StackOffset += RegSize;
297 if (MFI->hasCalls()) {
298 MFI->setObjectOffset(MFI->CreateStackObject(RegSize, RegSize),
299 StackOffset);
300 MipsFI->setRAStackOffset(StackOffset);
301 TopCPUSavedRegOff = StackOffset;
302 StackOffset += RegSize;
304 StackOffset = ((StackOffset+StackAlign-1)/StackAlign*StackAlign);
306 // Adjust FPU Callee Saved Registers Area. This Area must be
307 // aligned to the default Stack Alignment requirements.
308 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
309 if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
310 continue;
311 MFI->setObjectOffset(CSI[i].getFrameIdx(), StackOffset);
312 TopFPUSavedRegOff = StackOffset;
313 StackOffset += MFI->getObjectAlignment(CSI[i].getFrameIdx());
315 StackOffset = ((StackOffset+StackAlign-1)/StackAlign*StackAlign);
317 // Update frame info
318 MFI->setStackSize(StackOffset);
320 // Recalculate the final tops offset. The final values must be '0'
321 // if there isn't a callee saved register for CPU or FPU, otherwise
322 // a negative offset is needed.
323 if (TopCPUSavedRegOff >= 0)
324 MipsFI->setCPUTopSavedRegOff(TopCPUSavedRegOff-StackOffset);
326 if (TopFPUSavedRegOff >= 0)
327 MipsFI->setFPUTopSavedRegOff(TopFPUSavedRegOff-StackOffset);
330 // hasFP - Return true if the specified function should have a dedicated frame
331 // pointer register. This is true if the function has variable sized allocas or
332 // if frame pointer elimination is disabled.
333 bool MipsRegisterInfo::
334 hasFP(const MachineFunction &MF) const {
335 const MachineFrameInfo *MFI = MF.getFrameInfo();
336 return NoFramePointerElim || MFI->hasVarSizedObjects();
339 // This function eliminate ADJCALLSTACKDOWN,
340 // ADJCALLSTACKUP pseudo instructions
341 void MipsRegisterInfo::
342 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
343 MachineBasicBlock::iterator I) const {
344 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
345 MBB.erase(I);
348 // FrameIndex represent objects inside a abstract stack.
349 // We must replace FrameIndex with an stack/frame pointer
350 // direct reference.
351 void MipsRegisterInfo::
352 eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
353 RegScavenger *RS) const
355 MachineInstr &MI = *II;
356 MachineFunction &MF = *MI.getParent()->getParent();
358 unsigned i = 0;
359 while (!MI.getOperand(i).isFI()) {
360 ++i;
361 assert(i < MI.getNumOperands() &&
362 "Instr doesn't have FrameIndex operand!");
365 #ifndef NDEBUG
366 DEBUG(errs() << "\nFunction : " << MF.getFunction()->getName() << "\n");
367 DOUT << "<--------->\n";
368 MI.print(DOUT);
369 #endif
371 int FrameIndex = MI.getOperand(i).getIndex();
372 int stackSize = MF.getFrameInfo()->getStackSize();
373 int spOffset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
375 #ifndef NDEBUG
376 DOUT << "FrameIndex : " << FrameIndex << "\n";
377 DOUT << "spOffset : " << spOffset << "\n";
378 DOUT << "stackSize : " << stackSize << "\n";
379 #endif
381 // as explained on LowerFormalArguments, detect negative offsets
382 // and adjust SPOffsets considering the final stack size.
383 int Offset = ((spOffset < 0) ? (stackSize + (-(spOffset+4))) : (spOffset));
384 Offset += MI.getOperand(i-1).getImm();
386 #ifndef NDEBUG
387 DOUT << "Offset : " << Offset << "\n";
388 DOUT << "<--------->\n";
389 #endif
391 MI.getOperand(i-1).ChangeToImmediate(Offset);
392 MI.getOperand(i).ChangeToRegister(getFrameRegister(MF), false);
395 void MipsRegisterInfo::
396 emitPrologue(MachineFunction &MF) const
398 MachineBasicBlock &MBB = MF.front();
399 MachineFrameInfo *MFI = MF.getFrameInfo();
400 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
401 MachineBasicBlock::iterator MBBI = MBB.begin();
402 DebugLoc dl = (MBBI != MBB.end() ?
403 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
404 bool isPIC = (MF.getTarget().getRelocationModel() == Reloc::PIC_);
406 // Get the right frame order for Mips.
407 adjustMipsStackFrame(MF);
409 // Get the number of bytes to allocate from the FrameInfo.
410 unsigned StackSize = MFI->getStackSize();
412 // No need to allocate space on the stack.
413 if (StackSize == 0 && !MFI->hasCalls()) return;
415 int FPOffset = MipsFI->getFPStackOffset();
416 int RAOffset = MipsFI->getRAStackOffset();
418 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOREORDER));
420 // TODO: check need from GP here.
421 if (isPIC && Subtarget.isABI_O32())
422 BuildMI(MBB, MBBI, dl, TII.get(Mips::CPLOAD)).addReg(getPICCallReg());
423 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOMACRO));
425 // Adjust stack : addi sp, sp, (-imm)
426 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDiu), Mips::SP)
427 .addReg(Mips::SP).addImm(-StackSize);
429 // Save the return address only if the function isnt a leaf one.
430 // sw $ra, stack_loc($sp)
431 if (MFI->hasCalls()) {
432 BuildMI(MBB, MBBI, dl, TII.get(Mips::SW))
433 .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
436 // if framepointer enabled, save it and set it
437 // to point to the stack pointer
438 if (hasFP(MF)) {
439 // sw $fp,stack_loc($sp)
440 BuildMI(MBB, MBBI, dl, TII.get(Mips::SW))
441 .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
443 // move $fp, $sp
444 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDu), Mips::FP)
445 .addReg(Mips::SP).addReg(Mips::ZERO);
448 // PIC speficic function prologue
449 if ((isPIC) && (MFI->hasCalls())) {
450 BuildMI(MBB, MBBI, dl, TII.get(Mips::CPRESTORE))
451 .addImm(MipsFI->getGPStackOffset());
455 void MipsRegisterInfo::
456 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const
458 MachineBasicBlock::iterator MBBI = prior(MBB.end());
459 MachineFrameInfo *MFI = MF.getFrameInfo();
460 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
461 DebugLoc dl = MBBI->getDebugLoc();
463 // Get the number of bytes from FrameInfo
464 int NumBytes = (int) MFI->getStackSize();
466 // Get the FI's where RA and FP are saved.
467 int FPOffset = MipsFI->getFPStackOffset();
468 int RAOffset = MipsFI->getRAStackOffset();
470 // if framepointer enabled, restore it and restore the
471 // stack pointer
472 if (hasFP(MF)) {
473 // move $sp, $fp
474 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDu), Mips::SP)
475 .addReg(Mips::FP).addReg(Mips::ZERO);
477 // lw $fp,stack_loc($sp)
478 BuildMI(MBB, MBBI, dl, TII.get(Mips::LW), Mips::FP)
479 .addImm(FPOffset).addReg(Mips::SP);
482 // Restore the return address only if the function isnt a leaf one.
483 // lw $ra, stack_loc($sp)
484 if (MFI->hasCalls()) {
485 BuildMI(MBB, MBBI, dl, TII.get(Mips::LW), Mips::RA)
486 .addImm(RAOffset).addReg(Mips::SP);
489 // adjust stack : insert addi sp, sp, (imm)
490 if (NumBytes) {
491 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDiu), Mips::SP)
492 .addReg(Mips::SP).addImm(NumBytes);
497 void MipsRegisterInfo::
498 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
499 // Set the SPOffset on the FI where GP must be saved/loaded.
500 MachineFrameInfo *MFI = MF.getFrameInfo();
501 bool isPIC = (MF.getTarget().getRelocationModel() == Reloc::PIC_);
502 if (MFI->hasCalls() && isPIC) {
503 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
504 MFI->setObjectOffset(MipsFI->getGPFI(), MipsFI->getGPStackOffset());
508 unsigned MipsRegisterInfo::
509 getRARegister() const {
510 return Mips::RA;
513 unsigned MipsRegisterInfo::
514 getFrameRegister(MachineFunction &MF) const {
515 return hasFP(MF) ? Mips::FP : Mips::SP;
518 unsigned MipsRegisterInfo::
519 getEHExceptionRegister() const {
520 llvm_unreachable("What is the exception register");
521 return 0;
524 unsigned MipsRegisterInfo::
525 getEHHandlerRegister() const {
526 llvm_unreachable("What is the exception handler register");
527 return 0;
530 int MipsRegisterInfo::
531 getDwarfRegNum(unsigned RegNum, bool isEH) const {
532 llvm_unreachable("What is the dwarf register number");
533 return -1;
536 #include "MipsGenRegisterInfo.inc"