blob: ab94adcca8de32a7f245401d8b41a63a0fb2f16b [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 =
Chris Lattner59db5492010-09-21 04:39:43 +000043 MF.getMachineMemOperand(
44 MachinePointerInfo(PseudoSourceValue::getFixedStack(FrameIdx)),
45 MachineMemOperand::MOStore,
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000046 MFI.getObjectSize(FrameIdx),
47 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000048
49 if (RC == &MSP430::GR16RegClass)
50 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
51 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000052 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000053 else if (RC == &MSP430::GR8RegClass)
54 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
55 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000056 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000057 else
Torok Edwinc23197a2009-07-14 16:55:14 +000058 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000059}
60
61void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
62 MachineBasicBlock::iterator MI,
63 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +000064 const TargetRegisterClass *RC,
65 const TargetRegisterInfo *TRI) const{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000066 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000067 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000068 MachineFunction &MF = *MBB.getParent();
69 MachineFrameInfo &MFI = *MF.getFrameInfo();
70
71 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +000072 MF.getMachineMemOperand(
73 MachinePointerInfo(PseudoSourceValue::getFixedStack(FrameIdx)),
74 MachineMemOperand::MOLoad,
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000075 MFI.getObjectSize(FrameIdx),
76 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000077
78 if (RC == &MSP430::GR16RegClass)
79 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000080 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000081 else if (RC == &MSP430::GR8RegClass)
82 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000083 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000084 else
Torok Edwinc23197a2009-07-14 16:55:14 +000085 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000086}
87
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +000088void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
89 MachineBasicBlock::iterator I, DebugLoc DL,
90 unsigned DestReg, unsigned SrcReg,
91 bool KillSrc) const {
92 unsigned Opc;
93 if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
94 Opc = MSP430::MOV16rr;
95 else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
96 Opc = MSP430::MOV8rr;
97 else
98 llvm_unreachable("Impossible reg-to-reg copy");
Anton Korobeynikov51c31d62009-05-03 13:05:42 +000099
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +0000100 BuildMI(MBB, I, DL, get(Opc), DestReg)
101 .addReg(SrcReg, getKillRegState(KillSrc));
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000102}
103
104bool
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000105MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
106 MachineBasicBlock::iterator MI,
Evan Cheng2457f2c2010-05-22 01:47:14 +0000107 const std::vector<CalleeSavedInfo> &CSI,
108 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000109 if (CSI.empty())
110 return false;
111
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000112 DebugLoc DL;
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000113 if (MI != MBB.end()) DL = MI->getDebugLoc();
114
115 MachineFunction &MF = *MBB.getParent();
116 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
117 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
118
119 for (unsigned i = CSI.size(); i != 0; --i) {
120 unsigned Reg = CSI[i-1].getReg();
121 // Add the callee-saved register as live-in. It's killed at the spill.
122 MBB.addLiveIn(Reg);
123 BuildMI(MBB, MI, DL, get(MSP430::PUSH16r))
Bill Wendling587daed2009-05-13 21:33:08 +0000124 .addReg(Reg, RegState::Kill);
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000125 }
126 return true;
127}
128
129bool
130MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
131 MachineBasicBlock::iterator MI,
Evan Cheng2457f2c2010-05-22 01:47:14 +0000132 const std::vector<CalleeSavedInfo> &CSI,
133 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000134 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 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
141 BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg());
142
143 return true;
144}
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000145
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000146unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
147 MachineBasicBlock::iterator I = MBB.end();
148 unsigned Count = 0;
149
150 while (I != MBB.begin()) {
151 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000152 if (I->isDebugValue())
153 continue;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000154 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000155 I->getOpcode() != MSP430::JCC &&
156 I->getOpcode() != MSP430::Br &&
157 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000158 break;
159 // Remove the branch.
160 I->eraseFromParent();
161 I = MBB.end();
162 ++Count;
163 }
164
165 return Count;
166}
167
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000168bool MSP430InstrInfo::
169ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
170 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
171
172 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
173
174 switch (CC) {
175 default:
176 assert(0 && "Invalid branch condition!");
177 break;
178 case MSP430CC::COND_E:
179 CC = MSP430CC::COND_NE;
180 break;
181 case MSP430CC::COND_NE:
182 CC = MSP430CC::COND_E;
183 break;
184 case MSP430CC::COND_L:
185 CC = MSP430CC::COND_GE;
186 break;
187 case MSP430CC::COND_GE:
188 CC = MSP430CC::COND_L;
189 break;
190 case MSP430CC::COND_HS:
191 CC = MSP430CC::COND_LO;
192 break;
193 case MSP430CC::COND_LO:
194 CC = MSP430CC::COND_HS;
195 break;
196 }
197
198 Cond[0].setImm(CC);
199 return false;
200}
201
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000202bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
203 const TargetInstrDesc &TID = MI->getDesc();
204 if (!TID.isTerminator()) return false;
205
206 // Conditional branch is a special case.
207 if (TID.isBranch() && !TID.isBarrier())
208 return true;
209 if (!TID.isPredicable())
210 return true;
211 return !isPredicated(MI);
212}
213
214bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
215 MachineBasicBlock *&TBB,
216 MachineBasicBlock *&FBB,
217 SmallVectorImpl<MachineOperand> &Cond,
218 bool AllowModify) const {
219 // Start from the bottom of the block and work up, examining the
220 // terminator instructions.
221 MachineBasicBlock::iterator I = MBB.end();
222 while (I != MBB.begin()) {
223 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000224 if (I->isDebugValue())
225 continue;
226
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000227 // Working from the bottom, when we see a non-terminator
228 // instruction, we're done.
229 if (!isUnpredicatedTerminator(I))
230 break;
231
232 // A terminator that isn't a branch can't easily be handled
233 // by this analysis.
234 if (!I->getDesc().isBranch())
235 return true;
236
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000237 // Cannot handle indirect branches.
238 if (I->getOpcode() == MSP430::Br ||
239 I->getOpcode() == MSP430::Bm)
240 return true;
241
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000242 // Handle unconditional branches.
243 if (I->getOpcode() == MSP430::JMP) {
244 if (!AllowModify) {
245 TBB = I->getOperand(0).getMBB();
246 continue;
247 }
248
249 // If the block has any instructions after a JMP, delete them.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000250 while (llvm::next(I) != MBB.end())
251 llvm::next(I)->eraseFromParent();
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000252 Cond.clear();
253 FBB = 0;
254
255 // Delete the JMP if it's equivalent to a fall-through.
256 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
257 TBB = 0;
258 I->eraseFromParent();
259 I = MBB.end();
260 continue;
261 }
262
263 // TBB is used to indicate the unconditinal destination.
264 TBB = I->getOperand(0).getMBB();
265 continue;
266 }
267
268 // Handle conditional branches.
269 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
270 MSP430CC::CondCodes BranchCode =
271 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
272 if (BranchCode == MSP430CC::COND_INVALID)
273 return true; // Can't handle weird stuff.
274
275 // Working from the bottom, handle the first conditional branch.
276 if (Cond.empty()) {
277 FBB = TBB;
278 TBB = I->getOperand(0).getMBB();
279 Cond.push_back(MachineOperand::CreateImm(BranchCode));
280 continue;
281 }
282
283 // Handle subsequent conditional branches. Only handle the case where all
284 // conditional branches branch to the same destination.
285 assert(Cond.size() == 1);
286 assert(TBB);
287
288 // Only handle the case where all conditional branches branch to
289 // the same destination.
290 if (TBB != I->getOperand(0).getMBB())
291 return true;
292
293 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
294 // If the conditions are the same, we can leave them alone.
295 if (OldBranchCode == BranchCode)
296 continue;
297
298 return true;
299 }
300
301 return false;
302}
303
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000304unsigned
305MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
306 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000307 const SmallVectorImpl<MachineOperand> &Cond,
308 DebugLoc DL) const {
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000309 // Shouldn't be a fall through.
310 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
311 assert((Cond.size() == 1 || Cond.size() == 0) &&
312 "MSP430 branch conditions have one component!");
313
314 if (Cond.empty()) {
315 // Unconditional branch?
316 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000317 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000318 return 1;
319 }
320
321 // Conditional branch.
322 unsigned Count = 0;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000323 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000324 ++Count;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000325
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000326 if (FBB) {
327 // Two-way Conditional branch. Insert the second branch.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000328 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000329 ++Count;
330 }
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000331 return Count;
332}
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000333
334/// GetInstSize - Return the number of bytes of code the specified
335/// instruction may be. This returns the maximum number of bytes.
336///
337unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
338 const TargetInstrDesc &Desc = MI->getDesc();
339
340 switch (Desc.TSFlags & MSP430II::SizeMask) {
341 default:
342 switch (Desc.getOpcode()) {
343 default:
344 assert(0 && "Unknown instruction size!");
Bill Wendling7431bea2010-07-16 22:20:36 +0000345 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000346 case TargetOpcode::EH_LABEL:
347 case TargetOpcode::IMPLICIT_DEF:
348 case TargetOpcode::KILL:
Dale Johannesen375be772010-04-07 19:51:44 +0000349 case TargetOpcode::DBG_VALUE:
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000350 return 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000351 case TargetOpcode::INLINEASM: {
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000352 const MachineFunction *MF = MI->getParent()->getParent();
353 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
354 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
355 *MF->getTarget().getMCAsmInfo());
356 }
357 }
358 case MSP430II::SizeSpecial:
359 switch (MI->getOpcode()) {
360 default:
361 assert(0 && "Unknown instruction size!");
362 case MSP430::SAR8r1c:
363 case MSP430::SAR16r1c:
364 return 4;
365 }
366 case MSP430II::Size2Bytes:
367 return 2;
368 case MSP430II::Size4Bytes:
369 return 4;
370 case MSP430II::Size6Bytes:
371 return 6;
372 }
373
374 return 6;
375}