blob: e584770dd497885a9cd17efc1e03e64a41d69d26 [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();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000038 MachineFunction &MF = *MBB.getParent();
39 MachineFrameInfo &MFI = *MF.getFrameInfo();
40
41 MachineMemOperand *MMO =
42 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
43 MachineMemOperand::MOStore, 0,
44 MFI.getObjectSize(FrameIdx),
45 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000046
47 if (RC == &MSP430::GR16RegClass)
48 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
49 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000050 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000051 else if (RC == &MSP430::GR8RegClass)
52 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
53 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000054 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000055 else
Torok Edwinc23197a2009-07-14 16:55:14 +000056 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000057}
58
59void 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();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000065 MachineFunction &MF = *MBB.getParent();
66 MachineFrameInfo &MFI = *MF.getFrameInfo();
67
68 MachineMemOperand *MMO =
69 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
70 MachineMemOperand::MOLoad, 0,
71 MFI.getObjectSize(FrameIdx),
72 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000073
74 if (RC == &MSP430::GR16RegClass)
75 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000076 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000077 else if (RC == &MSP430::GR8RegClass)
78 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000079 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000080 else
Torok Edwinc23197a2009-07-14 16:55:14 +000081 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000082}
83
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000084bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
85 MachineBasicBlock::iterator I,
86 unsigned DestReg, unsigned SrcReg,
87 const TargetRegisterClass *DestRC,
88 const TargetRegisterClass *SrcRC) const {
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000089 DebugLoc DL = DebugLoc::getUnknownLoc();
90 if (I != MBB.end()) DL = I->getDebugLoc();
91
Anton Korobeynikov51c31d62009-05-03 13:05:42 +000092 if (DestRC == SrcRC) {
93 unsigned Opc;
94 if (DestRC == &MSP430::GR16RegClass) {
95 Opc = MSP430::MOV16rr;
96 } else if (DestRC == &MSP430::GR8RegClass) {
97 Opc = MSP430::MOV8rr;
98 } else {
99 return false;
100 }
101
102 BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg);
103 return true;
104 }
105
106 return false;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000107}
108
109bool
110MSP430InstrInfo::isMoveInstr(const MachineInstr& MI,
111 unsigned &SrcReg, unsigned &DstReg,
112 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
113 SrcSubIdx = DstSubIdx = 0; // No sub-registers yet.
114
115 switch (MI.getOpcode()) {
116 default:
117 return false;
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000118 case MSP430::MOV8rr:
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000119 case MSP430::MOV16rr:
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000120 assert(MI.getNumOperands() >= 2 &&
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000121 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();
126 return true;
127 }
128}
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000129
130bool
131MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
132 MachineBasicBlock::iterator MI,
133 const std::vector<CalleeSavedInfo> &CSI) const {
134 if (CSI.empty())
135 return false;
136
137 DebugLoc DL = DebugLoc::getUnknownLoc();
138 if (MI != MBB.end()) DL = MI->getDebugLoc();
139
140 MachineFunction &MF = *MBB.getParent();
141 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
142 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
143
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.
147 MBB.addLiveIn(Reg);
148 BuildMI(MBB, MI, DL, get(MSP430::PUSH16r))
Bill Wendling587daed2009-05-13 21:33:08 +0000149 .addReg(Reg, RegState::Kill);
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000150 }
151 return true;
152}
153
154bool
155MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
156 MachineBasicBlock::iterator MI,
157 const std::vector<CalleeSavedInfo> &CSI) const {
158 if (CSI.empty())
159 return false;
160
161 DebugLoc DL = DebugLoc::getUnknownLoc();
162 if (MI != MBB.end()) DL = MI->getDebugLoc();
163
164 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
165 BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg());
166
167 return true;
168}
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000169
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000170unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
171 MachineBasicBlock::iterator I = MBB.end();
172 unsigned Count = 0;
173
174 while (I != MBB.begin()) {
175 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000176 if (I->isDebugValue())
177 continue;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000178 if (I->getOpcode() != MSP430::JMP &&
179 I->getOpcode() != MSP430::JCC)
180 break;
181 // Remove the branch.
182 I->eraseFromParent();
183 I = MBB.end();
184 ++Count;
185 }
186
187 return Count;
188}
189
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000190bool MSP430InstrInfo::
191ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
192 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
193
194 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
195
196 switch (CC) {
197 default:
198 assert(0 && "Invalid branch condition!");
199 break;
200 case MSP430CC::COND_E:
201 CC = MSP430CC::COND_NE;
202 break;
203 case MSP430CC::COND_NE:
204 CC = MSP430CC::COND_E;
205 break;
206 case MSP430CC::COND_L:
207 CC = MSP430CC::COND_GE;
208 break;
209 case MSP430CC::COND_GE:
210 CC = MSP430CC::COND_L;
211 break;
212 case MSP430CC::COND_HS:
213 CC = MSP430CC::COND_LO;
214 break;
215 case MSP430CC::COND_LO:
216 CC = MSP430CC::COND_HS;
217 break;
218 }
219
220 Cond[0].setImm(CC);
221 return false;
222}
223
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000224bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
225 const TargetInstrDesc &TID = MI->getDesc();
226 if (!TID.isTerminator()) return false;
227
228 // Conditional branch is a special case.
229 if (TID.isBranch() && !TID.isBarrier())
230 return true;
231 if (!TID.isPredicable())
232 return true;
233 return !isPredicated(MI);
234}
235
236bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
237 MachineBasicBlock *&TBB,
238 MachineBasicBlock *&FBB,
239 SmallVectorImpl<MachineOperand> &Cond,
240 bool AllowModify) const {
241 // Start from the bottom of the block and work up, examining the
242 // terminator instructions.
243 MachineBasicBlock::iterator I = MBB.end();
244 while (I != MBB.begin()) {
245 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000246 if (I->isDebugValue())
247 continue;
248
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000249 // Working from the bottom, when we see a non-terminator
250 // instruction, we're done.
251 if (!isUnpredicatedTerminator(I))
252 break;
253
254 // A terminator that isn't a branch can't easily be handled
255 // by this analysis.
256 if (!I->getDesc().isBranch())
257 return true;
258
259 // Handle unconditional branches.
260 if (I->getOpcode() == MSP430::JMP) {
261 if (!AllowModify) {
262 TBB = I->getOperand(0).getMBB();
263 continue;
264 }
265
266 // If the block has any instructions after a JMP, delete them.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000267 while (llvm::next(I) != MBB.end())
268 llvm::next(I)->eraseFromParent();
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000269 Cond.clear();
270 FBB = 0;
271
272 // Delete the JMP if it's equivalent to a fall-through.
273 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
274 TBB = 0;
275 I->eraseFromParent();
276 I = MBB.end();
277 continue;
278 }
279
280 // TBB is used to indicate the unconditinal destination.
281 TBB = I->getOperand(0).getMBB();
282 continue;
283 }
284
285 // Handle conditional branches.
286 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
287 MSP430CC::CondCodes BranchCode =
288 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
289 if (BranchCode == MSP430CC::COND_INVALID)
290 return true; // Can't handle weird stuff.
291
292 // Working from the bottom, handle the first conditional branch.
293 if (Cond.empty()) {
294 FBB = TBB;
295 TBB = I->getOperand(0).getMBB();
296 Cond.push_back(MachineOperand::CreateImm(BranchCode));
297 continue;
298 }
299
300 // Handle subsequent conditional branches. Only handle the case where all
301 // conditional branches branch to the same destination.
302 assert(Cond.size() == 1);
303 assert(TBB);
304
305 // Only handle the case where all conditional branches branch to
306 // the same destination.
307 if (TBB != I->getOperand(0).getMBB())
308 return true;
309
310 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
311 // If the conditions are the same, we can leave them alone.
312 if (OldBranchCode == BranchCode)
313 continue;
314
315 return true;
316 }
317
318 return false;
319}
320
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000321unsigned
322MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
323 MachineBasicBlock *FBB,
324 const SmallVectorImpl<MachineOperand> &Cond) const {
325 // FIXME this should probably have a DebugLoc operand
326 DebugLoc dl = DebugLoc::getUnknownLoc();
327
328 // Shouldn't be a fall through.
329 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
330 assert((Cond.size() == 1 || Cond.size() == 0) &&
331 "MSP430 branch conditions have one component!");
332
333 if (Cond.empty()) {
334 // Unconditional branch?
335 assert(!FBB && "Unconditional branch with multiple successors!");
336 BuildMI(&MBB, dl, get(MSP430::JMP)).addMBB(TBB);
337 return 1;
338 }
339
340 // Conditional branch.
341 unsigned Count = 0;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000342 BuildMI(&MBB, dl, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
343 ++Count;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000344
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000345 if (FBB) {
346 // Two-way Conditional branch. Insert the second branch.
347 BuildMI(&MBB, dl, get(MSP430::JMP)).addMBB(FBB);
348 ++Count;
349 }
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000350 return Count;
351}
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000352
353/// GetInstSize - Return the number of bytes of code the specified
354/// instruction may be. This returns the maximum number of bytes.
355///
356unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
357 const TargetInstrDesc &Desc = MI->getDesc();
358
359 switch (Desc.TSFlags & MSP430II::SizeMask) {
360 default:
361 switch (Desc.getOpcode()) {
362 default:
363 assert(0 && "Unknown instruction size!");
Chris Lattner518bb532010-02-09 19:54:29 +0000364 case TargetOpcode::DBG_LABEL:
365 case TargetOpcode::EH_LABEL:
366 case TargetOpcode::IMPLICIT_DEF:
367 case TargetOpcode::KILL:
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000368 return 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000369 case TargetOpcode::INLINEASM: {
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000370 const MachineFunction *MF = MI->getParent()->getParent();
371 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
372 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
373 *MF->getTarget().getMCAsmInfo());
374 }
375 }
376 case MSP430II::SizeSpecial:
377 switch (MI->getOpcode()) {
378 default:
379 assert(0 && "Unknown instruction size!");
380 case MSP430::SAR8r1c:
381 case MSP430::SAR16r1c:
382 return 4;
383 }
384 case MSP430II::Size2Bytes:
385 return 2;
386 case MSP430II::Size4Bytes:
387 return 4;
388 case MSP430II::Size6Bytes:
389 return 6;
390 }
391
392 return 6;
393}