blob: 527cf1fd411e2df69b16079e34424168eeea1135 [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
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000086bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
87 MachineBasicBlock::iterator I,
88 unsigned DestReg, unsigned SrcReg,
89 const TargetRegisterClass *DestRC,
Dan Gohman34dcc6f2010-05-06 20:33:48 +000090 const TargetRegisterClass *SrcRC,
91 DebugLoc DL) const {
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
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000137 DebugLoc DL;
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000138 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
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000161 DebugLoc DL;
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000162 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 &&
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000179 I->getOpcode() != MSP430::JCC &&
180 I->getOpcode() != MSP430::Br &&
181 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000182 break;
183 // Remove the branch.
184 I->eraseFromParent();
185 I = MBB.end();
186 ++Count;
187 }
188
189 return Count;
190}
191
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000192bool MSP430InstrInfo::
193ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
194 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
195
196 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
197
198 switch (CC) {
199 default:
200 assert(0 && "Invalid branch condition!");
201 break;
202 case MSP430CC::COND_E:
203 CC = MSP430CC::COND_NE;
204 break;
205 case MSP430CC::COND_NE:
206 CC = MSP430CC::COND_E;
207 break;
208 case MSP430CC::COND_L:
209 CC = MSP430CC::COND_GE;
210 break;
211 case MSP430CC::COND_GE:
212 CC = MSP430CC::COND_L;
213 break;
214 case MSP430CC::COND_HS:
215 CC = MSP430CC::COND_LO;
216 break;
217 case MSP430CC::COND_LO:
218 CC = MSP430CC::COND_HS;
219 break;
220 }
221
222 Cond[0].setImm(CC);
223 return false;
224}
225
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000226bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
227 const TargetInstrDesc &TID = MI->getDesc();
228 if (!TID.isTerminator()) return false;
229
230 // Conditional branch is a special case.
231 if (TID.isBranch() && !TID.isBarrier())
232 return true;
233 if (!TID.isPredicable())
234 return true;
235 return !isPredicated(MI);
236}
237
238bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
239 MachineBasicBlock *&TBB,
240 MachineBasicBlock *&FBB,
241 SmallVectorImpl<MachineOperand> &Cond,
242 bool AllowModify) const {
243 // Start from the bottom of the block and work up, examining the
244 // terminator instructions.
245 MachineBasicBlock::iterator I = MBB.end();
246 while (I != MBB.begin()) {
247 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000248 if (I->isDebugValue())
249 continue;
250
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000251 // Working from the bottom, when we see a non-terminator
252 // instruction, we're done.
253 if (!isUnpredicatedTerminator(I))
254 break;
255
256 // A terminator that isn't a branch can't easily be handled
257 // by this analysis.
258 if (!I->getDesc().isBranch())
259 return true;
260
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000261 // Cannot handle indirect branches.
262 if (I->getOpcode() == MSP430::Br ||
263 I->getOpcode() == MSP430::Bm)
264 return true;
265
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000266 // Handle unconditional branches.
267 if (I->getOpcode() == MSP430::JMP) {
268 if (!AllowModify) {
269 TBB = I->getOperand(0).getMBB();
270 continue;
271 }
272
273 // If the block has any instructions after a JMP, delete them.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000274 while (llvm::next(I) != MBB.end())
275 llvm::next(I)->eraseFromParent();
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000276 Cond.clear();
277 FBB = 0;
278
279 // Delete the JMP if it's equivalent to a fall-through.
280 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
281 TBB = 0;
282 I->eraseFromParent();
283 I = MBB.end();
284 continue;
285 }
286
287 // TBB is used to indicate the unconditinal destination.
288 TBB = I->getOperand(0).getMBB();
289 continue;
290 }
291
292 // Handle conditional branches.
293 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
294 MSP430CC::CondCodes BranchCode =
295 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
296 if (BranchCode == MSP430CC::COND_INVALID)
297 return true; // Can't handle weird stuff.
298
299 // Working from the bottom, handle the first conditional branch.
300 if (Cond.empty()) {
301 FBB = TBB;
302 TBB = I->getOperand(0).getMBB();
303 Cond.push_back(MachineOperand::CreateImm(BranchCode));
304 continue;
305 }
306
307 // Handle subsequent conditional branches. Only handle the case where all
308 // conditional branches branch to the same destination.
309 assert(Cond.size() == 1);
310 assert(TBB);
311
312 // Only handle the case where all conditional branches branch to
313 // the same destination.
314 if (TBB != I->getOperand(0).getMBB())
315 return true;
316
317 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
318 // If the conditions are the same, we can leave them alone.
319 if (OldBranchCode == BranchCode)
320 continue;
321
322 return true;
323 }
324
325 return false;
326}
327
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000328unsigned
329MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
330 MachineBasicBlock *FBB,
331 const SmallVectorImpl<MachineOperand> &Cond) const {
332 // FIXME this should probably have a DebugLoc operand
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000333 DebugLoc DL;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000334
335 // Shouldn't be a fall through.
336 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
337 assert((Cond.size() == 1 || Cond.size() == 0) &&
338 "MSP430 branch conditions have one component!");
339
340 if (Cond.empty()) {
341 // Unconditional branch?
342 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000343 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000344 return 1;
345 }
346
347 // Conditional branch.
348 unsigned Count = 0;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000349 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000350 ++Count;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000351
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000352 if (FBB) {
353 // Two-way Conditional branch. Insert the second branch.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000354 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000355 ++Count;
356 }
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000357 return Count;
358}
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000359
360/// GetInstSize - Return the number of bytes of code the specified
361/// instruction may be. This returns the maximum number of bytes.
362///
363unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
364 const TargetInstrDesc &Desc = MI->getDesc();
365
366 switch (Desc.TSFlags & MSP430II::SizeMask) {
367 default:
368 switch (Desc.getOpcode()) {
369 default:
370 assert(0 && "Unknown instruction size!");
Chris Lattner518bb532010-02-09 19:54:29 +0000371 case TargetOpcode::DBG_LABEL:
372 case TargetOpcode::EH_LABEL:
373 case TargetOpcode::IMPLICIT_DEF:
374 case TargetOpcode::KILL:
Dale Johannesen375be772010-04-07 19:51:44 +0000375 case TargetOpcode::DBG_VALUE:
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000376 return 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000377 case TargetOpcode::INLINEASM: {
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000378 const MachineFunction *MF = MI->getParent()->getParent();
379 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
380 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
381 *MF->getTarget().getMCAsmInfo());
382 }
383 }
384 case MSP430II::SizeSpecial:
385 switch (MI->getOpcode()) {
386 default:
387 assert(0 && "Unknown instruction size!");
388 case MSP430::SAR8r1c:
389 case MSP430::SAR16r1c:
390 return 4;
391 }
392 case MSP430II::Size2Bytes:
393 return 2;
394 case MSP430II::Size4Bytes:
395 return 4;
396 case MSP430II::Size6Bytes:
397 return 6;
398 }
399
400 return 6;
401}