LLVM API Documentation
00001 //===- MSP430InstrInfo.cpp - MSP430 Instruction 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 TargetInstrInfo class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "MSP430.h" 00015 #include "MSP430InstrInfo.h" 00016 #include "MSP430MachineFunctionInfo.h" 00017 #include "MSP430TargetMachine.h" 00018 #include "MSP430GenInstrInfo.inc" 00019 #include "llvm/Function.h" 00020 #include "llvm/CodeGen/MachineFrameInfo.h" 00021 #include "llvm/CodeGen/MachineInstrBuilder.h" 00022 #include "llvm/CodeGen/MachineRegisterInfo.h" 00023 #include "llvm/CodeGen/PseudoSourceValue.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 00026 using namespace llvm; 00027 00028 MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm) 00029 : TargetInstrInfoImpl(MSP430Insts, array_lengthof(MSP430Insts)), 00030 RI(tm, *this), TM(tm) {} 00031 00032 void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 00033 MachineBasicBlock::iterator MI, 00034 unsigned SrcReg, bool isKill, int FrameIdx, 00035 const TargetRegisterClass *RC) const { 00036 DebugLoc DL = DebugLoc::getUnknownLoc(); 00037 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00038 00039 if (RC == &MSP430::GR16RegClass) 00040 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr)) 00041 .addFrameIndex(FrameIdx).addImm(0) 00042 .addReg(SrcReg, getKillRegState(isKill)); 00043 else if (RC == &MSP430::GR8RegClass) 00044 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr)) 00045 .addFrameIndex(FrameIdx).addImm(0) 00046 .addReg(SrcReg, getKillRegState(isKill)); 00047 else 00048 llvm_unreachable("Cannot store this register to stack slot!"); 00049 } 00050 00051 void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 00052 MachineBasicBlock::iterator MI, 00053 unsigned DestReg, int FrameIdx, 00054 const TargetRegisterClass *RC) const{ 00055 DebugLoc DL = DebugLoc::getUnknownLoc(); 00056 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00057 00058 if (RC == &MSP430::GR16RegClass) 00059 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm)) 00060 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0); 00061 else if (RC == &MSP430::GR8RegClass) 00062 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm)) 00063 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0); 00064 else 00065 llvm_unreachable("Cannot store this register to stack slot!"); 00066 } 00067 00068 bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock &MBB, 00069 MachineBasicBlock::iterator I, 00070 unsigned DestReg, unsigned SrcReg, 00071 const TargetRegisterClass *DestRC, 00072 const TargetRegisterClass *SrcRC) const { 00073 DebugLoc DL = DebugLoc::getUnknownLoc(); 00074 if (I != MBB.end()) DL = I->getDebugLoc(); 00075 00076 if (DestRC == SrcRC) { 00077 unsigned Opc; 00078 if (DestRC == &MSP430::GR16RegClass) { 00079 Opc = MSP430::MOV16rr; 00080 } else if (DestRC == &MSP430::GR8RegClass) { 00081 Opc = MSP430::MOV8rr; 00082 } else { 00083 return false; 00084 } 00085 00086 BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg); 00087 return true; 00088 } 00089 00090 return false; 00091 } 00092 00093 bool 00094 MSP430InstrInfo::isMoveInstr(const MachineInstr& MI, 00095 unsigned &SrcReg, unsigned &DstReg, 00096 unsigned &SrcSubIdx, unsigned &DstSubIdx) const { 00097 SrcSubIdx = DstSubIdx = 0; // No sub-registers yet. 00098 00099 switch (MI.getOpcode()) { 00100 default: 00101 return false; 00102 case MSP430::MOV8rr: 00103 case MSP430::MOV16rr: 00104 assert(MI.getNumOperands() >= 2 && 00105 MI.getOperand(0).isReg() && 00106 MI.getOperand(1).isReg() && 00107 "invalid register-register move instruction"); 00108 SrcReg = MI.getOperand(1).getReg(); 00109 DstReg = MI.getOperand(0).getReg(); 00110 return true; 00111 } 00112 } 00113 00114 bool 00115 MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 00116 MachineBasicBlock::iterator MI, 00117 const std::vector<CalleeSavedInfo> &CSI) const { 00118 if (CSI.empty()) 00119 return false; 00120 00121 DebugLoc DL = DebugLoc::getUnknownLoc(); 00122 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00123 00124 MachineFunction &MF = *MBB.getParent(); 00125 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>(); 00126 MFI->setCalleeSavedFrameSize(CSI.size() * 2); 00127 00128 for (unsigned i = CSI.size(); i != 0; --i) { 00129 unsigned Reg = CSI[i-1].getReg(); 00130 // Add the callee-saved register as live-in. It's killed at the spill. 00131 MBB.addLiveIn(Reg); 00132 BuildMI(MBB, MI, DL, get(MSP430::PUSH16r)) 00133 .addReg(Reg, RegState::Kill); 00134 } 00135 return true; 00136 } 00137 00138 bool 00139 MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 00140 MachineBasicBlock::iterator MI, 00141 const std::vector<CalleeSavedInfo> &CSI) const { 00142 if (CSI.empty()) 00143 return false; 00144 00145 DebugLoc DL = DebugLoc::getUnknownLoc(); 00146 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00147 00148 for (unsigned i = 0, e = CSI.size(); i != e; ++i) 00149 BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg()); 00150 00151 return true; 00152 } 00153 00154 unsigned 00155 MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, 00156 MachineBasicBlock *FBB, 00157 const SmallVectorImpl<MachineOperand> &Cond) const { 00158 // FIXME this should probably have a DebugLoc operand 00159 DebugLoc dl = DebugLoc::getUnknownLoc(); 00160 00161 // Shouldn't be a fall through. 00162 assert(TBB && "InsertBranch must not be told to insert a fallthrough"); 00163 assert((Cond.size() == 1 || Cond.size() == 0) && 00164 "MSP430 branch conditions have one component!"); 00165 00166 if (Cond.empty()) { 00167 // Unconditional branch? 00168 assert(!FBB && "Unconditional branch with multiple successors!"); 00169 BuildMI(&MBB, dl, get(MSP430::JMP)).addMBB(TBB); 00170 return 1; 00171 } 00172 00173 // Conditional branch. 00174 unsigned Count = 0; 00175 llvm_unreachable("Implement conditional branches!"); 00176 00177 return Count; 00178 }