1 //===-- SparcRegisterInfo.cpp - SPARC Register Information ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the SPARC implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "SparcRegisterInfo.h"
16 #include "SparcMachineFunctionInfo.h"
17 #include "SparcSubtarget.h"
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Target/TargetInstrInfo.h"
30 #define GET_REGINFO_TARGET_DESC
31 #include "SparcGenRegisterInfo.inc"
34 ReserveAppRegisters("sparc-reserve-app-registers", cl::Hidden, cl::init(false),
35 cl::desc("Reserve application registers (%g2-%g4)"));
37 SparcRegisterInfo::SparcRegisterInfo() : SparcGenRegisterInfo(SP::O7) {}
40 SparcRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
45 SparcRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
46 CallingConv::ID CC) const {
51 SparcRegisterInfo::getRTCallPreservedMask(CallingConv::ID CC) const {
55 BitVector SparcRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
56 BitVector Reserved(getNumRegs());
57 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
58 // FIXME: G1 reserved for now for large imm generation by frame code.
61 // G1-G4 can be used in applications.
62 if (ReserveAppRegisters) {
67 // G5 is not reserved in 64 bit mode.
68 if (!Subtarget.is64Bit())
78 // Unaliased double registers are not available in non-V9 targets.
79 if (!Subtarget.isV9()) {
80 for (unsigned n = 0; n != 16; ++n) {
81 for (MCRegAliasIterator AI(SP::D16 + n, this, true); AI.isValid(); ++AI)
89 const TargetRegisterClass*
90 SparcRegisterInfo::getPointerRegClass(const MachineFunction &MF,
91 unsigned Kind) const {
92 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
93 return Subtarget.is64Bit() ? &SP::I64RegsRegClass : &SP::IntRegsRegClass;
96 static void replaceFI(MachineFunction &MF,
97 MachineBasicBlock::iterator II,
100 unsigned FIOperandNum, int Offset,
103 // Replace frame index with a frame pointer reference.
104 if (Offset >= -4096 && Offset <= 4095) {
105 // If the offset is small enough to fit in the immediate field, directly
107 MI.getOperand(FIOperandNum).ChangeToRegister(FramePtr, false);
108 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
112 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
114 // FIXME: it would be better to scavenge a register here instead of
115 // reserving G1 all of the time.
117 // Emit nonnegaive immediates with sethi + or.
118 // sethi %hi(Offset), %g1
120 // Insert G1+%lo(offset) into the user.
121 BuildMI(*MI.getParent(), II, dl, TII.get(SP::SETHIi), SP::G1)
122 .addImm(HI22(Offset));
126 BuildMI(*MI.getParent(), II, dl, TII.get(SP::ADDrr), SP::G1).addReg(SP::G1)
128 // Insert: G1+%lo(offset) into the user.
129 MI.getOperand(FIOperandNum).ChangeToRegister(SP::G1, false);
130 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(LO10(Offset));
134 // Emit Negative numbers with sethi + xor
135 // sethi %hix(Offset), %g1
136 // xor %g1, %lox(offset), %g1
138 // Insert: G1 + 0 into the user.
139 BuildMI(*MI.getParent(), II, dl, TII.get(SP::SETHIi), SP::G1)
140 .addImm(HIX22(Offset));
141 BuildMI(*MI.getParent(), II, dl, TII.get(SP::XORri), SP::G1)
142 .addReg(SP::G1).addImm(LOX10(Offset));
144 BuildMI(*MI.getParent(), II, dl, TII.get(SP::ADDrr), SP::G1).addReg(SP::G1)
146 // Insert: G1+%lo(offset) into the user.
147 MI.getOperand(FIOperandNum).ChangeToRegister(SP::G1, false);
148 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
153 SparcRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
154 int SPAdj, unsigned FIOperandNum,
155 RegScavenger *RS) const {
156 assert(SPAdj == 0 && "Unexpected");
158 MachineInstr &MI = *II;
159 DebugLoc dl = MI.getDebugLoc();
160 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
162 // Addressable stack objects are accessed using neg. offsets from %fp
163 MachineFunction &MF = *MI.getParent()->getParent();
164 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
165 int64_t Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
166 MI.getOperand(FIOperandNum + 1).getImm() +
167 Subtarget.getStackPointerBias();
168 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
169 unsigned FramePtr = SP::I6;
170 if (FuncInfo->isLeafProc()) {
171 // Use %sp and adjust offset if needed.
173 int stackSize = MF.getFrameInfo()->getStackSize();
174 Offset += (stackSize) ? Subtarget.getAdjustedFrameSize(stackSize) : 0 ;
177 if (!Subtarget.isV9() || !Subtarget.hasHardQuad()) {
178 if (MI.getOpcode() == SP::STQFri) {
179 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
180 unsigned SrcReg = MI.getOperand(2).getReg();
181 unsigned SrcEvenReg = getSubReg(SrcReg, SP::sub_even64);
182 unsigned SrcOddReg = getSubReg(SrcReg, SP::sub_odd64);
184 BuildMI(*MI.getParent(), II, dl, TII.get(SP::STDFri))
185 .addReg(FramePtr).addImm(0).addReg(SrcEvenReg);
186 replaceFI(MF, II, *StMI, dl, 0, Offset, FramePtr);
187 MI.setDesc(TII.get(SP::STDFri));
188 MI.getOperand(2).setReg(SrcOddReg);
190 } else if (MI.getOpcode() == SP::LDQFri) {
191 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
192 unsigned DestReg = MI.getOperand(0).getReg();
193 unsigned DestEvenReg = getSubReg(DestReg, SP::sub_even64);
194 unsigned DestOddReg = getSubReg(DestReg, SP::sub_odd64);
196 BuildMI(*MI.getParent(), II, dl, TII.get(SP::LDDFri), DestEvenReg)
197 .addReg(FramePtr).addImm(0);
198 replaceFI(MF, II, *StMI, dl, 1, Offset, FramePtr);
200 MI.setDesc(TII.get(SP::LDDFri));
201 MI.getOperand(0).setReg(DestOddReg);
206 replaceFI(MF, II, MI, dl, FIOperandNum, Offset, FramePtr);
210 unsigned SparcRegisterInfo::getFrameRegister(const MachineFunction &MF) const {