blob: fececb0c1127e70ed41c60012a889add37434733 [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,
Evan Cheng746ad692010-05-06 19:06:44 +000035 const TargetRegisterClass *RC,
36 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000037 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000038 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000039 MachineFunction &MF = *MBB.getParent();
40 MachineFrameInfo &MFI = *MF.getFrameInfo();
41
42 MachineMemOperand *MMO =
43 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
44 MachineMemOperand::MOStore, 0,
45 MFI.getObjectSize(FrameIdx),
46 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000047
48 if (RC == &MSP430::GR16RegClass)
49 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
50 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000051 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000052 else if (RC == &MSP430::GR8RegClass)
53 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
54 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000055 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000056 else
Torok Edwinc23197a2009-07-14 16:55:14 +000057 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000058}
59
60void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
61 MachineBasicBlock::iterator MI,
62 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +000063 const TargetRegisterClass *RC,
64 const TargetRegisterInfo *TRI) const{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000065 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000066 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000067 MachineFunction &MF = *MBB.getParent();
68 MachineFrameInfo &MFI = *MF.getFrameInfo();
69
70 MachineMemOperand *MMO =
71 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
72 MachineMemOperand::MOLoad, 0,
73 MFI.getObjectSize(FrameIdx),
74 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000075
76 if (RC == &MSP430::GR16RegClass)
77 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000078 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000079 else if (RC == &MSP430::GR8RegClass)
80 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000081 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000082 else
Torok Edwinc23197a2009-07-14 16:55:14 +000083 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000084}
85
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +000086void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
87 MachineBasicBlock::iterator I, DebugLoc DL,
88 unsigned DestReg, unsigned SrcReg,
89 bool KillSrc) const {
90 unsigned Opc;
91 if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
92 Opc = MSP430::MOV16rr;
93 else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
94 Opc = MSP430::MOV8rr;
95 else
96 llvm_unreachable("Impossible reg-to-reg copy");
Anton Korobeynikov51c31d62009-05-03 13:05:42 +000097
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +000098 BuildMI(MBB, I, DL, get(Opc), DestReg)
99 .addReg(SrcReg, getKillRegState(KillSrc));
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000100}
101
102bool
103MSP430InstrInfo::isMoveInstr(const MachineInstr& MI,
104 unsigned &SrcReg, unsigned &DstReg,
105 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
106 SrcSubIdx = DstSubIdx = 0; // No sub-registers yet.
107
108 switch (MI.getOpcode()) {
109 default:
110 return false;
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000111 case MSP430::MOV8rr:
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000112 case MSP430::MOV16rr:
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000113 assert(MI.getNumOperands() >= 2 &&
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000114 MI.getOperand(0).isReg() &&
115 MI.getOperand(1).isReg() &&
116 "invalid register-register move instruction");
117 SrcReg = MI.getOperand(1).getReg();
118 DstReg = MI.getOperand(0).getReg();
119 return true;
120 }
121}
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000122
123bool
124MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
125 MachineBasicBlock::iterator MI,
Evan Cheng2457f2c2010-05-22 01:47:14 +0000126 const std::vector<CalleeSavedInfo> &CSI,
127 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000128 if (CSI.empty())
129 return false;
130
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000131 DebugLoc DL;
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000132 if (MI != MBB.end()) DL = MI->getDebugLoc();
133
134 MachineFunction &MF = *MBB.getParent();
135 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
136 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
137
138 for (unsigned i = CSI.size(); i != 0; --i) {
139 unsigned Reg = CSI[i-1].getReg();
140 // Add the callee-saved register as live-in. It's killed at the spill.
141 MBB.addLiveIn(Reg);
142 BuildMI(MBB, MI, DL, get(MSP430::PUSH16r))
Bill Wendling587daed2009-05-13 21:33:08 +0000143 .addReg(Reg, RegState::Kill);
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000144 }
145 return true;
146}
147
148bool
149MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
150 MachineBasicBlock::iterator MI,
Evan Cheng2457f2c2010-05-22 01:47:14 +0000151 const std::vector<CalleeSavedInfo> &CSI,
152 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000153 if (CSI.empty())
154 return false;
155
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000156 DebugLoc DL;
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000157 if (MI != MBB.end()) DL = MI->getDebugLoc();
158
159 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
160 BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg());
161
162 return true;
163}
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000164
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000165unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
166 MachineBasicBlock::iterator I = MBB.end();
167 unsigned Count = 0;
168
169 while (I != MBB.begin()) {
170 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000171 if (I->isDebugValue())
172 continue;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000173 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000174 I->getOpcode() != MSP430::JCC &&
175 I->getOpcode() != MSP430::Br &&
176 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000177 break;
178 // Remove the branch.
179 I->eraseFromParent();
180 I = MBB.end();
181 ++Count;
182 }
183
184 return Count;
185}
186
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000187bool MSP430InstrInfo::
188ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
189 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
190
191 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
192
193 switch (CC) {
194 default:
195 assert(0 && "Invalid branch condition!");
196 break;
197 case MSP430CC::COND_E:
198 CC = MSP430CC::COND_NE;
199 break;
200 case MSP430CC::COND_NE:
201 CC = MSP430CC::COND_E;
202 break;
203 case MSP430CC::COND_L:
204 CC = MSP430CC::COND_GE;
205 break;
206 case MSP430CC::COND_GE:
207 CC = MSP430CC::COND_L;
208 break;
209 case MSP430CC::COND_HS:
210 CC = MSP430CC::COND_LO;
211 break;
212 case MSP430CC::COND_LO:
213 CC = MSP430CC::COND_HS;
214 break;
215 }
216
217 Cond[0].setImm(CC);
218 return false;
219}
220
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000221bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
222 const TargetInstrDesc &TID = MI->getDesc();
223 if (!TID.isTerminator()) return false;
224
225 // Conditional branch is a special case.
226 if (TID.isBranch() && !TID.isBarrier())
227 return true;
228 if (!TID.isPredicable())
229 return true;
230 return !isPredicated(MI);
231}
232
233bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
234 MachineBasicBlock *&TBB,
235 MachineBasicBlock *&FBB,
236 SmallVectorImpl<MachineOperand> &Cond,
237 bool AllowModify) const {
238 // Start from the bottom of the block and work up, examining the
239 // terminator instructions.
240 MachineBasicBlock::iterator I = MBB.end();
241 while (I != MBB.begin()) {
242 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000243 if (I->isDebugValue())
244 continue;
245
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000246 // Working from the bottom, when we see a non-terminator
247 // instruction, we're done.
248 if (!isUnpredicatedTerminator(I))
249 break;
250
251 // A terminator that isn't a branch can't easily be handled
252 // by this analysis.
253 if (!I->getDesc().isBranch())
254 return true;
255
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000256 // Cannot handle indirect branches.
257 if (I->getOpcode() == MSP430::Br ||
258 I->getOpcode() == MSP430::Bm)
259 return true;
260
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000261 // Handle unconditional branches.
262 if (I->getOpcode() == MSP430::JMP) {
263 if (!AllowModify) {
264 TBB = I->getOperand(0).getMBB();
265 continue;
266 }
267
268 // If the block has any instructions after a JMP, delete them.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000269 while (llvm::next(I) != MBB.end())
270 llvm::next(I)->eraseFromParent();
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000271 Cond.clear();
272 FBB = 0;
273
274 // Delete the JMP if it's equivalent to a fall-through.
275 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
276 TBB = 0;
277 I->eraseFromParent();
278 I = MBB.end();
279 continue;
280 }
281
282 // TBB is used to indicate the unconditinal destination.
283 TBB = I->getOperand(0).getMBB();
284 continue;
285 }
286
287 // Handle conditional branches.
288 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
289 MSP430CC::CondCodes BranchCode =
290 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
291 if (BranchCode == MSP430CC::COND_INVALID)
292 return true; // Can't handle weird stuff.
293
294 // Working from the bottom, handle the first conditional branch.
295 if (Cond.empty()) {
296 FBB = TBB;
297 TBB = I->getOperand(0).getMBB();
298 Cond.push_back(MachineOperand::CreateImm(BranchCode));
299 continue;
300 }
301
302 // Handle subsequent conditional branches. Only handle the case where all
303 // conditional branches branch to the same destination.
304 assert(Cond.size() == 1);
305 assert(TBB);
306
307 // Only handle the case where all conditional branches branch to
308 // the same destination.
309 if (TBB != I->getOperand(0).getMBB())
310 return true;
311
312 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
313 // If the conditions are the same, we can leave them alone.
314 if (OldBranchCode == BranchCode)
315 continue;
316
317 return true;
318 }
319
320 return false;
321}
322
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000323unsigned
324MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
325 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000326 const SmallVectorImpl<MachineOperand> &Cond,
327 DebugLoc DL) const {
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000328 // 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!");
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000336 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000337 return 1;
338 }
339
340 // Conditional branch.
341 unsigned Count = 0;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000342 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000343 ++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.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000347 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000348 ++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!");
Bill Wendling7431bea2010-07-16 22:20:36 +0000364 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000365 case TargetOpcode::EH_LABEL:
366 case TargetOpcode::IMPLICIT_DEF:
367 case TargetOpcode::KILL:
Dale Johannesen375be772010-04-07 19:51:44 +0000368 case TargetOpcode::DBG_VALUE:
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000369 return 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000370 case TargetOpcode::INLINEASM: {
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000371 const MachineFunction *MF = MI->getParent()->getParent();
372 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
373 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
374 *MF->getTarget().getMCAsmInfo());
375 }
376 }
377 case MSP430II::SizeSpecial:
378 switch (MI->getOpcode()) {
379 default:
380 assert(0 && "Unknown instruction size!");
381 case MSP430::SAR8r1c:
382 case MSP430::SAR16r1c:
383 return 4;
384 }
385 case MSP430II::Size2Bytes:
386 return 2;
387 case MSP430II::Size4Bytes:
388 return 4;
389 case MSP430II::Size6Bytes:
390 return 6;
391 }
392
393 return 6;
394}