1 //===- MSP430InstrInfo.cpp - MSP430 Instruction 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 MSP430 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "MSP430InstrInfo.h"
16 #include "MSP430MachineFunctionInfo.h"
17 #include "MSP430TargetMachine.h"
18 #include "MSP430GenInstrInfo.inc"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
24 #include "llvm/Support/ErrorHandling.h"
28 MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
29 : TargetInstrInfoImpl(MSP430Insts, array_lengthof(MSP430Insts)),
30 RI(tm, *this), TM(tm) {}
32 void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
33 MachineBasicBlock::iterator MI,
34 unsigned SrcReg, bool isKill, int FrameIdx,
35 const TargetRegisterClass *RC) const {
36 DebugLoc DL = DebugLoc::getUnknownLoc();
37 if (MI != MBB.end()) DL = MI->getDebugLoc();
38 MachineFunction &MF = *MBB.getParent();
39 MachineFrameInfo &MFI = *MF.getFrameInfo();
41 MachineMemOperand *MMO =
42 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
43 MachineMemOperand::MOStore, 0,
44 MFI.getObjectSize(FrameIdx),
45 MFI.getObjectAlignment(FrameIdx));
47 if (RC == &MSP430::GR16RegClass)
48 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
49 .addFrameIndex(FrameIdx).addImm(0)
50 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
51 else if (RC == &MSP430::GR8RegClass)
52 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
53 .addFrameIndex(FrameIdx).addImm(0)
54 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
56 llvm_unreachable("Cannot store this register to stack slot!");
59 void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
60 MachineBasicBlock::iterator MI,
61 unsigned DestReg, int FrameIdx,
62 const TargetRegisterClass *RC) const{
63 DebugLoc DL = DebugLoc::getUnknownLoc();
64 if (MI != MBB.end()) DL = MI->getDebugLoc();
65 MachineFunction &MF = *MBB.getParent();
66 MachineFrameInfo &MFI = *MF.getFrameInfo();
68 MachineMemOperand *MMO =
69 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
70 MachineMemOperand::MOLoad, 0,
71 MFI.getObjectSize(FrameIdx),
72 MFI.getObjectAlignment(FrameIdx));
74 if (RC == &MSP430::GR16RegClass)
75 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
76 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
77 else if (RC == &MSP430::GR8RegClass)
78 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
79 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
81 llvm_unreachable("Cannot store this register to stack slot!");
84 bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
85 MachineBasicBlock::iterator I,
86 unsigned DestReg, unsigned SrcReg,
87 const TargetRegisterClass *DestRC,
88 const TargetRegisterClass *SrcRC) const {
89 DebugLoc DL = DebugLoc::getUnknownLoc();
90 if (I != MBB.end()) DL = I->getDebugLoc();
92 if (DestRC == SrcRC) {
94 if (DestRC == &MSP430::GR16RegClass) {
95 Opc = MSP430::MOV16rr;
96 } else if (DestRC == &MSP430::GR8RegClass) {
102 BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg);
110 MSP430InstrInfo::isMoveInstr(const MachineInstr& MI,
111 unsigned &SrcReg, unsigned &DstReg,
112 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
113 SrcSubIdx = DstSubIdx = 0; // No sub-registers yet.
115 switch (MI.getOpcode()) {
119 case MSP430::MOV16rr:
120 assert(MI.getNumOperands() >= 2 &&
121 MI.getOperand(0).isReg() &&
122 MI.getOperand(1).isReg() &&
123 "invalid register-register move instruction");
124 SrcReg = MI.getOperand(1).getReg();
125 DstReg = MI.getOperand(0).getReg();
131 MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
132 MachineBasicBlock::iterator MI,
133 const std::vector<CalleeSavedInfo> &CSI) const {
137 DebugLoc DL = DebugLoc::getUnknownLoc();
138 if (MI != MBB.end()) DL = MI->getDebugLoc();
140 MachineFunction &MF = *MBB.getParent();
141 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
142 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
144 for (unsigned i = CSI.size(); i != 0; --i) {
145 unsigned Reg = CSI[i-1].getReg();
146 // Add the callee-saved register as live-in. It's killed at the spill.
148 BuildMI(MBB, MI, DL, get(MSP430::PUSH16r))
149 .addReg(Reg, RegState::Kill);
155 MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
156 MachineBasicBlock::iterator MI,
157 const std::vector<CalleeSavedInfo> &CSI) const {
161 DebugLoc DL = DebugLoc::getUnknownLoc();
162 if (MI != MBB.end()) DL = MI->getDebugLoc();
164 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
165 BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg());
170 unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
171 MachineBasicBlock::iterator I = MBB.end();
174 while (I != MBB.begin()) {
176 if (I->getOpcode() != MSP430::JMP &&
177 I->getOpcode() != MSP430::JCC)
179 // Remove the branch.
180 I->eraseFromParent();
188 bool MSP430InstrInfo::
189 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
190 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
192 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
196 assert(0 && "Invalid branch condition!");
198 case MSP430CC::COND_E:
199 CC = MSP430CC::COND_NE;
201 case MSP430CC::COND_NE:
202 CC = MSP430CC::COND_E;
204 case MSP430CC::COND_L:
205 CC = MSP430CC::COND_GE;
207 case MSP430CC::COND_GE:
208 CC = MSP430CC::COND_L;
210 case MSP430CC::COND_HS:
211 CC = MSP430CC::COND_LO;
213 case MSP430CC::COND_LO:
214 CC = MSP430CC::COND_HS;
222 bool MSP430InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB)const{
223 if (MBB.empty()) return false;
225 switch (MBB.back().getOpcode()) {
226 case MSP430::RET: // Return.
227 case MSP430::JMP: // Uncond branch.
229 default: return false;
233 bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
234 const TargetInstrDesc &TID = MI->getDesc();
235 if (!TID.isTerminator()) return false;
237 // Conditional branch is a special case.
238 if (TID.isBranch() && !TID.isBarrier())
240 if (!TID.isPredicable())
242 return !isPredicated(MI);
245 bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
246 MachineBasicBlock *&TBB,
247 MachineBasicBlock *&FBB,
248 SmallVectorImpl<MachineOperand> &Cond,
249 bool AllowModify) const {
250 // Start from the bottom of the block and work up, examining the
251 // terminator instructions.
252 MachineBasicBlock::iterator I = MBB.end();
253 while (I != MBB.begin()) {
255 // Working from the bottom, when we see a non-terminator
256 // instruction, we're done.
257 if (!isUnpredicatedTerminator(I))
260 // A terminator that isn't a branch can't easily be handled
262 if (!I->getDesc().isBranch())
265 // Handle unconditional branches.
266 if (I->getOpcode() == MSP430::JMP) {
268 TBB = I->getOperand(0).getMBB();
272 // If the block has any instructions after a JMP, delete them.
273 while (next(I) != MBB.end())
274 next(I)->eraseFromParent();
278 // Delete the JMP if it's equivalent to a fall-through.
279 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
281 I->eraseFromParent();
286 // TBB is used to indicate the unconditinal destination.
287 TBB = I->getOperand(0).getMBB();
291 // Handle conditional branches.
292 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
293 MSP430CC::CondCodes BranchCode =
294 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
295 if (BranchCode == MSP430CC::COND_INVALID)
296 return true; // Can't handle weird stuff.
298 // Working from the bottom, handle the first conditional branch.
301 TBB = I->getOperand(0).getMBB();
302 Cond.push_back(MachineOperand::CreateImm(BranchCode));
306 // Handle subsequent conditional branches. Only handle the case where all
307 // conditional branches branch to the same destination.
308 assert(Cond.size() == 1);
311 // Only handle the case where all conditional branches branch to
312 // the same destination.
313 if (TBB != I->getOperand(0).getMBB())
316 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
317 // If the conditions are the same, we can leave them alone.
318 if (OldBranchCode == BranchCode)
328 MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
329 MachineBasicBlock *FBB,
330 const SmallVectorImpl<MachineOperand> &Cond) const {
331 // FIXME this should probably have a DebugLoc operand
332 DebugLoc dl = DebugLoc::getUnknownLoc();
334 // Shouldn't be a fall through.
335 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
336 assert((Cond.size() == 1 || Cond.size() == 0) &&
337 "MSP430 branch conditions have one component!");
340 // Unconditional branch?
341 assert(!FBB && "Unconditional branch with multiple successors!");
342 BuildMI(&MBB, dl, get(MSP430::JMP)).addMBB(TBB);
346 // Conditional branch.
348 BuildMI(&MBB, dl, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
352 // Two-way Conditional branch. Insert the second branch.
353 BuildMI(&MBB, dl, get(MSP430::JMP)).addMBB(FBB);