LLVM API Documentation
00001 //===- MSP430RegisterInfo.cpp - MSP430 Register Information ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the MSP430 implementation of the TargetRegisterInfo class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "msp430-reg-info" 00015 00016 #include "MSP430.h" 00017 #include "MSP430MachineFunctionInfo.h" 00018 #include "MSP430RegisterInfo.h" 00019 #include "MSP430TargetMachine.h" 00020 #include "llvm/CodeGen/MachineFrameInfo.h" 00021 #include "llvm/CodeGen/MachineFunction.h" 00022 #include "llvm/CodeGen/MachineInstrBuilder.h" 00023 #include "llvm/Target/TargetMachine.h" 00024 #include "llvm/Target/TargetOptions.h" 00025 #include "llvm/ADT/BitVector.h" 00026 #include "llvm/Support/ErrorHandling.h" 00027 00028 using namespace llvm; 00029 00030 // FIXME: Provide proper call frame setup / destroy opcodes. 00031 MSP430RegisterInfo::MSP430RegisterInfo(MSP430TargetMachine &tm, 00032 const TargetInstrInfo &tii) 00033 : MSP430GenRegisterInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP), 00034 TM(tm), TII(tii) { 00035 StackAlign = TM.getFrameInfo()->getStackAlignment(); 00036 } 00037 00038 const unsigned* 00039 MSP430RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 00040 static const unsigned CalleeSavedRegs[] = { 00041 MSP430::FPW, MSP430::R5W, MSP430::R6W, MSP430::R7W, 00042 MSP430::R8W, MSP430::R9W, MSP430::R10W, MSP430::R11W, 00043 0 00044 }; 00045 00046 return CalleeSavedRegs; 00047 } 00048 00049 const TargetRegisterClass *const * 00050 MSP430RegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const { 00051 static const TargetRegisterClass * const CalleeSavedRegClasses[] = { 00052 &MSP430::GR16RegClass, &MSP430::GR16RegClass, 00053 &MSP430::GR16RegClass, &MSP430::GR16RegClass, 00054 &MSP430::GR16RegClass, &MSP430::GR16RegClass, 00055 &MSP430::GR16RegClass, &MSP430::GR16RegClass, 00056 0 00057 }; 00058 00059 return CalleeSavedRegClasses; 00060 } 00061 00062 BitVector MSP430RegisterInfo::getReservedRegs(const MachineFunction &MF) const { 00063 BitVector Reserved(getNumRegs()); 00064 00065 // Mark 4 special registers as reserved. 00066 Reserved.set(MSP430::PCW); 00067 Reserved.set(MSP430::SPW); 00068 Reserved.set(MSP430::SRW); 00069 Reserved.set(MSP430::CGW); 00070 00071 // Mark frame pointer as reserved if needed. 00072 if (hasFP(MF)) 00073 Reserved.set(MSP430::FPW); 00074 00075 return Reserved; 00076 } 00077 00078 const TargetRegisterClass * 00079 MSP430RegisterInfo::getPointerRegClass(unsigned Kind) const { 00080 return &MSP430::GR16RegClass; 00081 } 00082 00083 00084 bool MSP430RegisterInfo::hasFP(const MachineFunction &MF) const { 00085 return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects(); 00086 } 00087 00088 bool MSP430RegisterInfo::hasReservedCallFrame(MachineFunction &MF) const { 00089 return !MF.getFrameInfo()->hasVarSizedObjects(); 00090 } 00091 00092 void MSP430RegisterInfo:: 00093 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 00094 MachineBasicBlock::iterator I) const { 00095 if (!hasReservedCallFrame(MF)) { 00096 // If the stack pointer can be changed after prologue, turn the 00097 // adjcallstackup instruction into a 'sub SPW, <amt>' and the 00098 // adjcallstackdown instruction into 'add SPW, <amt>' 00099 // TODO: consider using push / pop instead of sub + store / add 00100 MachineInstr *Old = I; 00101 uint64_t Amount = Old->getOperand(0).getImm(); 00102 if (Amount != 0) { 00103 // We need to keep the stack aligned properly. To do this, we round the 00104 // amount of space needed for the outgoing arguments up to the next 00105 // alignment boundary. 00106 Amount = (Amount+StackAlign-1)/StackAlign*StackAlign; 00107 00108 MachineInstr *New = 0; 00109 if (Old->getOpcode() == getCallFrameSetupOpcode()) { 00110 New = BuildMI(MF, Old->getDebugLoc(), 00111 TII.get(MSP430::SUB16ri), MSP430::SPW) 00112 .addReg(MSP430::SPW).addImm(Amount); 00113 } else { 00114 assert(Old->getOpcode() == getCallFrameDestroyOpcode()); 00115 // factor out the amount the callee already popped. 00116 uint64_t CalleeAmt = Old->getOperand(1).getImm(); 00117 Amount -= CalleeAmt; 00118 if (Amount) 00119 New = BuildMI(MF, Old->getDebugLoc(), 00120 TII.get(MSP430::ADD16ri), MSP430::SPW) 00121 .addReg(MSP430::SPW).addImm(Amount); 00122 } 00123 00124 if (New) { 00125 // The SRW implicit def is dead. 00126 New->getOperand(3).setIsDead(); 00127 00128 // Replace the pseudo instruction with a new instruction... 00129 MBB.insert(I, New); 00130 } 00131 } 00132 } else if (I->getOpcode() == getCallFrameDestroyOpcode()) { 00133 // If we are performing frame pointer elimination and if the callee pops 00134 // something off the stack pointer, add it back. 00135 if (uint64_t CalleeAmt = I->getOperand(1).getImm()) { 00136 MachineInstr *Old = I; 00137 MachineInstr *New = 00138 BuildMI(MF, Old->getDebugLoc(), TII.get(MSP430::SUB16ri), 00139 MSP430::SPW).addReg(MSP430::SPW).addImm(CalleeAmt); 00140 // The SRW implicit def is dead. 00141 New->getOperand(3).setIsDead(); 00142 00143 MBB.insert(I, New); 00144 } 00145 } 00146 00147 MBB.erase(I); 00148 } 00149 00150 void 00151 MSP430RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 00152 int SPAdj, RegScavenger *RS) const { 00153 assert(SPAdj == 0 && "Unexpected"); 00154 00155 unsigned i = 0; 00156 MachineInstr &MI = *II; 00157 MachineBasicBlock &MBB = *MI.getParent(); 00158 MachineFunction &MF = *MBB.getParent(); 00159 DebugLoc dl = MI.getDebugLoc(); 00160 while (!MI.getOperand(i).isFI()) { 00161 ++i; 00162 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 00163 } 00164 00165 int FrameIndex = MI.getOperand(i).getIndex(); 00166 00167 unsigned BasePtr = (hasFP(MF) ? MSP430::FPW : MSP430::SPW); 00168 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex); 00169 00170 // Skip the saved PC 00171 Offset += 2; 00172 00173 if (!hasFP(MF)) 00174 Offset += MF.getFrameInfo()->getStackSize(); 00175 else 00176 Offset += 2; // Skip the saved FPW 00177 00178 // Fold imm into offset 00179 Offset += MI.getOperand(i+1).getImm(); 00180 00181 if (MI.getOpcode() == MSP430::ADD16ri) { 00182 // This is actually "load effective address" of the stack slot 00183 // instruction. We have only two-address instructions, thus we need to 00184 // expand it into mov + add 00185 00186 MI.setDesc(TII.get(MSP430::MOV16rr)); 00187 MI.getOperand(i).ChangeToRegister(BasePtr, false); 00188 00189 if (Offset == 0) 00190 return; 00191 00192 // We need to materialize the offset via add instruction. 00193 unsigned DstReg = MI.getOperand(0).getReg(); 00194 if (Offset < 0) 00195 BuildMI(MBB, next(II), dl, TII.get(MSP430::SUB16ri), DstReg) 00196 .addReg(DstReg).addImm(-Offset); 00197 else 00198 BuildMI(MBB, next(II), dl, TII.get(MSP430::ADD16ri), DstReg) 00199 .addReg(DstReg).addImm(Offset); 00200 00201 return; 00202 } 00203 00204 MI.getOperand(i).ChangeToRegister(BasePtr, false); 00205 MI.getOperand(i+1).ChangeToImmediate(Offset); 00206 } 00207 00208 void 00209 MSP430RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF) 00210 const { 00211 // Create a frame entry for the FPW register that must be saved. 00212 if (hasFP(MF)) { 00213 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(2, -4); 00214 assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() && 00215 "Slot for FPW register must be last in order to be found!"); 00216 FrameIdx = 0; 00217 } 00218 } 00219 00220 00221 void MSP430RegisterInfo::emitPrologue(MachineFunction &MF) const { 00222 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB 00223 MachineFrameInfo *MFI = MF.getFrameInfo(); 00224 MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>(); 00225 MachineBasicBlock::iterator MBBI = MBB.begin(); 00226 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() : 00227 DebugLoc::getUnknownLoc()); 00228 00229 // Get the number of bytes to allocate from the FrameInfo. 00230 uint64_t StackSize = MFI->getStackSize(); 00231 00232 uint64_t NumBytes = 0; 00233 if (hasFP(MF)) { 00234 // Calculate required stack adjustment 00235 uint64_t FrameSize = StackSize - 2; 00236 NumBytes = FrameSize - MSP430FI->getCalleeSavedFrameSize(); 00237 00238 // Get the offset of the stack slot for the EBP register... which is 00239 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized. 00240 // Update the frame offset adjustment. 00241 MFI->setOffsetAdjustment(-NumBytes); 00242 00243 // Save FPW into the appropriate stack slot... 00244 BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r)) 00245 .addReg(MSP430::FPW, RegState::Kill); 00246 00247 // Update FPW with the new base value... 00248 BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FPW) 00249 .addReg(MSP430::SPW); 00250 00251 // Mark the FramePtr as live-in in every block except the entry. 00252 for (MachineFunction::iterator I = next(MF.begin()), E = MF.end(); 00253 I != E; ++I) 00254 I->addLiveIn(MSP430::FPW); 00255 00256 } else 00257 NumBytes = StackSize - MSP430FI->getCalleeSavedFrameSize(); 00258 00259 // Skip the callee-saved push instructions. 00260 while (MBBI != MBB.end() && (MBBI->getOpcode() == MSP430::PUSH16r)) 00261 ++MBBI; 00262 00263 if (MBBI != MBB.end()) 00264 DL = MBBI->getDebugLoc(); 00265 00266 if (NumBytes) { // adjust stack pointer: SPW -= numbytes 00267 // If there is an SUB16ri of SPW immediately before this instruction, merge 00268 // the two. 00269 //NumBytes -= mergeSPUpdates(MBB, MBBI, true); 00270 // If there is an ADD16ri or SUB16ri of SPW immediately after this 00271 // instruction, merge the two instructions. 00272 // mergeSPUpdatesDown(MBB, MBBI, &NumBytes); 00273 00274 if (NumBytes) { 00275 MachineInstr *MI = 00276 BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SPW) 00277 .addReg(MSP430::SPW).addImm(NumBytes); 00278 // The SRW implicit def is dead. 00279 MI->getOperand(3).setIsDead(); 00280 } 00281 } 00282 } 00283 00284 void MSP430RegisterInfo::emitEpilogue(MachineFunction &MF, 00285 MachineBasicBlock &MBB) const { 00286 const MachineFrameInfo *MFI = MF.getFrameInfo(); 00287 MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>(); 00288 MachineBasicBlock::iterator MBBI = prior(MBB.end()); 00289 unsigned RetOpcode = MBBI->getOpcode(); 00290 DebugLoc DL = MBBI->getDebugLoc(); 00291 00292 switch (RetOpcode) { 00293 case MSP430::RET: break; // These are ok 00294 default: 00295 llvm_unreachable("Can only insert epilog into returning blocks"); 00296 } 00297 00298 // Get the number of bytes to allocate from the FrameInfo 00299 uint64_t StackSize = MFI->getStackSize(); 00300 unsigned CSSize = MSP430FI->getCalleeSavedFrameSize(); 00301 uint64_t NumBytes = 0; 00302 00303 if (hasFP(MF)) { 00304 // Calculate required stack adjustment 00305 uint64_t FrameSize = StackSize - 2; 00306 NumBytes = FrameSize - CSSize; 00307 00308 // pop FPW. 00309 BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FPW); 00310 } else 00311 NumBytes = StackSize - CSSize; 00312 00313 // Skip the callee-saved pop instructions. 00314 MachineBasicBlock::iterator LastCSPop = MBBI; 00315 while (MBBI != MBB.begin()) { 00316 MachineBasicBlock::iterator PI = prior(MBBI); 00317 unsigned Opc = PI->getOpcode(); 00318 if (Opc != MSP430::POP16r && !PI->getDesc().isTerminator()) 00319 break; 00320 --MBBI; 00321 } 00322 00323 DL = MBBI->getDebugLoc(); 00324 00325 // If there is an ADD16ri or SUB16ri of SPW immediately before this 00326 // instruction, merge the two instructions. 00327 //if (NumBytes || MFI->hasVarSizedObjects()) 00328 // mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes); 00329 00330 if (MFI->hasVarSizedObjects()) { 00331 llvm_unreachable("Not implemented yet!"); 00332 } else { 00333 // adjust stack pointer back: SPW += numbytes 00334 if (NumBytes) { 00335 MachineInstr *MI = 00336 BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SPW) 00337 .addReg(MSP430::SPW).addImm(NumBytes); 00338 // The SRW implicit def is dead. 00339 MI->getOperand(3).setIsDead(); 00340 } 00341 } 00342 } 00343 00344 unsigned MSP430RegisterInfo::getRARegister() const { 00345 return MSP430::PCW; 00346 } 00347 00348 unsigned MSP430RegisterInfo::getFrameRegister(MachineFunction &MF) const { 00349 return hasFP(MF) ? MSP430::FPW : MSP430::SPW; 00350 } 00351 00352 int MSP430RegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const { 00353 llvm_unreachable("Not implemented yet!"); 00354 return 0; 00355 } 00356 00357 #include "MSP430GenRegisterInfo.inc"