blob: f983a7a2969128e68d69f454f7aadb0d9eb918a2 [file] [log] [blame]
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +00001//===- MSP430InstrInfo.cpp - MSP430 Instruction Information ---------------===//
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 MSP430 implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MSP430.h"
15#include "MSP430InstrInfo.h"
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +000016#include "MSP430MachineFunctionInfo.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000017#include "MSP430TargetMachine.h"
18#include "MSP430GenInstrInfo.inc"
19#include "llvm/Function.h"
Anton Korobeynikovaa299152009-05-03 13:09:57 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikovaa299152009-05-03 13:09:57 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000025
26using namespace llvm;
27
28MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
29 : TargetInstrInfoImpl(MSP430Insts, array_lengthof(MSP430Insts)),
Anton Korobeynikovb5612642009-05-03 13:07:54 +000030 RI(tm, *this), TM(tm) {}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000031
Anton Korobeynikovaa299152009-05-03 13:09:57 +000032void 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
39 if (RC == &MSP430::GR16RegClass)
40 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
41 .addFrameIndex(FrameIdx).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +000042 .addReg(SrcReg, getKillRegState(isKill));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000043 else if (RC == &MSP430::GR8RegClass)
44 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
45 .addFrameIndex(FrameIdx).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +000046 .addReg(SrcReg, getKillRegState(isKill));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000047 else
Torok Edwinc23197a2009-07-14 16:55:14 +000048 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000049}
50
51void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
52 MachineBasicBlock::iterator MI,
53 unsigned DestReg, int FrameIdx,
54 const TargetRegisterClass *RC) const{
55 DebugLoc DL = DebugLoc::getUnknownLoc();
56 if (MI != MBB.end()) DL = MI->getDebugLoc();
57
58 if (RC == &MSP430::GR16RegClass)
59 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
60 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0);
61 else if (RC == &MSP430::GR8RegClass)
62 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
63 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0);
64 else
Torok Edwinc23197a2009-07-14 16:55:14 +000065 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000066}
67
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000068bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
69 MachineBasicBlock::iterator I,
70 unsigned DestReg, unsigned SrcReg,
71 const TargetRegisterClass *DestRC,
72 const TargetRegisterClass *SrcRC) const {
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000073 DebugLoc DL = DebugLoc::getUnknownLoc();
74 if (I != MBB.end()) DL = I->getDebugLoc();
75
Anton Korobeynikov51c31d62009-05-03 13:05:42 +000076 if (DestRC == SrcRC) {
77 unsigned Opc;
78 if (DestRC == &MSP430::GR16RegClass) {
79 Opc = MSP430::MOV16rr;
80 } else if (DestRC == &MSP430::GR8RegClass) {
81 Opc = MSP430::MOV8rr;
82 } else {
83 return false;
84 }
85
86 BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg);
87 return true;
88 }
89
90 return false;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000091}
92
93bool
94MSP430InstrInfo::isMoveInstr(const MachineInstr& MI,
95 unsigned &SrcReg, unsigned &DstReg,
96 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
97 SrcSubIdx = DstSubIdx = 0; // No sub-registers yet.
98
99 switch (MI.getOpcode()) {
100 default:
101 return false;
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000102 case MSP430::MOV8rr:
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000103 case MSP430::MOV16rr:
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000104 assert(MI.getNumOperands() >= 2 &&
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000105 MI.getOperand(0).isReg() &&
106 MI.getOperand(1).isReg() &&
107 "invalid register-register move instruction");
108 SrcReg = MI.getOperand(1).getReg();
109 DstReg = MI.getOperand(0).getReg();
110 return true;
111 }
112}
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000113
114bool
115MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
116 MachineBasicBlock::iterator MI,
117 const std::vector<CalleeSavedInfo> &CSI) const {
118 if (CSI.empty())
119 return false;
120
121 DebugLoc DL = DebugLoc::getUnknownLoc();
122 if (MI != MBB.end()) DL = MI->getDebugLoc();
123
124 MachineFunction &MF = *MBB.getParent();
125 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
126 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
127
128 for (unsigned i = CSI.size(); i != 0; --i) {
129 unsigned Reg = CSI[i-1].getReg();
130 // Add the callee-saved register as live-in. It's killed at the spill.
131 MBB.addLiveIn(Reg);
132 BuildMI(MBB, MI, DL, get(MSP430::PUSH16r))
Bill Wendling587daed2009-05-13 21:33:08 +0000133 .addReg(Reg, RegState::Kill);
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000134 }
135 return true;
136}
137
138bool
139MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
140 MachineBasicBlock::iterator MI,
141 const std::vector<CalleeSavedInfo> &CSI) const {
142 if (CSI.empty())
143 return false;
144
145 DebugLoc DL = DebugLoc::getUnknownLoc();
146 if (MI != MBB.end()) DL = MI->getDebugLoc();
147
148 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
149 BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg());
150
151 return true;
152}
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000153
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000154unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
155 MachineBasicBlock::iterator I = MBB.end();
156 unsigned Count = 0;
157
158 while (I != MBB.begin()) {
159 --I;
160 if (I->getOpcode() != MSP430::JMP &&
161 I->getOpcode() != MSP430::JCC)
162 break;
163 // Remove the branch.
164 I->eraseFromParent();
165 I = MBB.end();
166 ++Count;
167 }
168
169 return Count;
170}
171
172
173bool MSP430InstrInfo::
174ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
175 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
176
177 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
178
179 switch (CC) {
180 default:
181 assert(0 && "Invalid branch condition!");
182 break;
183 case MSP430CC::COND_E:
184 CC = MSP430CC::COND_NE;
185 break;
186 case MSP430CC::COND_NE:
187 CC = MSP430CC::COND_E;
188 break;
189 case MSP430CC::COND_L:
190 CC = MSP430CC::COND_GE;
191 break;
192 case MSP430CC::COND_GE:
193 CC = MSP430CC::COND_L;
194 break;
195 case MSP430CC::COND_HS:
196 CC = MSP430CC::COND_LO;
197 break;
198 case MSP430CC::COND_LO:
199 CC = MSP430CC::COND_HS;
200 break;
201 }
202
203 Cond[0].setImm(CC);
204 return false;
205}
206
207bool MSP430InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB)const{
208 if (MBB.empty()) return false;
209
210 switch (MBB.back().getOpcode()) {
211 case MSP430::RET: // Return.
212 case MSP430::JMP: // Uncond branch.
213 return true;
214 default: return false;
215 }
216}
217
218bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
219 const TargetInstrDesc &TID = MI->getDesc();
220 if (!TID.isTerminator()) return false;
221
222 // Conditional branch is a special case.
223 if (TID.isBranch() && !TID.isBarrier())
224 return true;
225 if (!TID.isPredicable())
226 return true;
227 return !isPredicated(MI);
228}
229
230bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
231 MachineBasicBlock *&TBB,
232 MachineBasicBlock *&FBB,
233 SmallVectorImpl<MachineOperand> &Cond,
234 bool AllowModify) const {
235 // Start from the bottom of the block and work up, examining the
236 // terminator instructions.
237 MachineBasicBlock::iterator I = MBB.end();
238 while (I != MBB.begin()) {
239 --I;
240 // Working from the bottom, when we see a non-terminator
241 // instruction, we're done.
242 if (!isUnpredicatedTerminator(I))
243 break;
244
245 // A terminator that isn't a branch can't easily be handled
246 // by this analysis.
247 if (!I->getDesc().isBranch())
248 return true;
249
250 // Handle unconditional branches.
251 if (I->getOpcode() == MSP430::JMP) {
252 if (!AllowModify) {
253 TBB = I->getOperand(0).getMBB();
254 continue;
255 }
256
257 // If the block has any instructions after a JMP, delete them.
258 while (next(I) != MBB.end())
259 next(I)->eraseFromParent();
260 Cond.clear();
261 FBB = 0;
262
263 // Delete the JMP if it's equivalent to a fall-through.
264 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
265 TBB = 0;
266 I->eraseFromParent();
267 I = MBB.end();
268 continue;
269 }
270
271 // TBB is used to indicate the unconditinal destination.
272 TBB = I->getOperand(0).getMBB();
273 continue;
274 }
275
276 // Handle conditional branches.
277 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
278 MSP430CC::CondCodes BranchCode =
279 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
280 if (BranchCode == MSP430CC::COND_INVALID)
281 return true; // Can't handle weird stuff.
282
283 // Working from the bottom, handle the first conditional branch.
284 if (Cond.empty()) {
285 FBB = TBB;
286 TBB = I->getOperand(0).getMBB();
287 Cond.push_back(MachineOperand::CreateImm(BranchCode));
288 continue;
289 }
290
291 // Handle subsequent conditional branches. Only handle the case where all
292 // conditional branches branch to the same destination.
293 assert(Cond.size() == 1);
294 assert(TBB);
295
296 // Only handle the case where all conditional branches branch to
297 // the same destination.
298 if (TBB != I->getOperand(0).getMBB())
299 return true;
300
301 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
302 // If the conditions are the same, we can leave them alone.
303 if (OldBranchCode == BranchCode)
304 continue;
305
306 return true;
307 }
308
309 return false;
310}
311
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000312unsigned
313MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
314 MachineBasicBlock *FBB,
315 const SmallVectorImpl<MachineOperand> &Cond) const {
316 // FIXME this should probably have a DebugLoc operand
317 DebugLoc dl = DebugLoc::getUnknownLoc();
318
319 // Shouldn't be a fall through.
320 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
321 assert((Cond.size() == 1 || Cond.size() == 0) &&
322 "MSP430 branch conditions have one component!");
323
324 if (Cond.empty()) {
325 // Unconditional branch?
326 assert(!FBB && "Unconditional branch with multiple successors!");
327 BuildMI(&MBB, dl, get(MSP430::JMP)).addMBB(TBB);
328 return 1;
329 }
330
331 // Conditional branch.
332 unsigned Count = 0;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000333 BuildMI(&MBB, dl, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
334 ++Count;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000335
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000336 if (FBB) {
337 // Two-way Conditional branch. Insert the second branch.
338 BuildMI(&MBB, dl, get(MSP430::JMP)).addMBB(FBB);
339 ++Count;
340 }
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000341 return Count;
342}