blob: e9dfd5258a78edc1046510986b5c3a4359e226f5 [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"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000018#include "llvm/Function.h"
Anton Korobeynikovaa299152009-05-03 13:09:57 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikovaa299152009-05-03 13:09:57 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000023#include "llvm/Target/TargetRegistry.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000025
Evan Cheng4db3cff2011-07-01 17:57:27 +000026#define GET_INSTRINFO_CTOR
Evan Cheng22fee2d2011-06-28 20:07:07 +000027#define GET_INSTRINFO_MC_DESC
28#include "MSP430GenInstrInfo.inc"
29
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000030using namespace llvm;
31
32MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000033 : MSP430GenInstrInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
Anton Korobeynikovb5612642009-05-03 13:07:54 +000034 RI(tm, *this), TM(tm) {}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000035
Anton Korobeynikovaa299152009-05-03 13:09:57 +000036void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
37 MachineBasicBlock::iterator MI,
38 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +000039 const TargetRegisterClass *RC,
40 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000041 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000042 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000043 MachineFunction &MF = *MBB.getParent();
44 MachineFrameInfo &MFI = *MF.getFrameInfo();
45
46 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +000047 MF.getMachineMemOperand(
48 MachinePointerInfo(PseudoSourceValue::getFixedStack(FrameIdx)),
49 MachineMemOperand::MOStore,
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000050 MFI.getObjectSize(FrameIdx),
51 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000052
53 if (RC == &MSP430::GR16RegClass)
54 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
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 if (RC == &MSP430::GR8RegClass)
58 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
59 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000060 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000061 else
Torok Edwinc23197a2009-07-14 16:55:14 +000062 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000063}
64
65void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
66 MachineBasicBlock::iterator MI,
67 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +000068 const TargetRegisterClass *RC,
69 const TargetRegisterInfo *TRI) const{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000070 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000071 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000072 MachineFunction &MF = *MBB.getParent();
73 MachineFrameInfo &MFI = *MF.getFrameInfo();
74
75 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +000076 MF.getMachineMemOperand(
77 MachinePointerInfo(PseudoSourceValue::getFixedStack(FrameIdx)),
78 MachineMemOperand::MOLoad,
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000079 MFI.getObjectSize(FrameIdx),
80 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000081
82 if (RC == &MSP430::GR16RegClass)
83 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000084 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000085 else if (RC == &MSP430::GR8RegClass)
86 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000087 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000088 else
Torok Edwinc23197a2009-07-14 16:55:14 +000089 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000090}
91
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +000092void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
93 MachineBasicBlock::iterator I, DebugLoc DL,
94 unsigned DestReg, unsigned SrcReg,
95 bool KillSrc) const {
96 unsigned Opc;
97 if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
98 Opc = MSP430::MOV16rr;
99 else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
100 Opc = MSP430::MOV8rr;
101 else
102 llvm_unreachable("Impossible reg-to-reg copy");
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000103
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +0000104 BuildMI(MBB, I, DL, get(Opc), DestReg)
105 .addReg(SrcReg, getKillRegState(KillSrc));
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000106}
107
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000108unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
109 MachineBasicBlock::iterator I = MBB.end();
110 unsigned Count = 0;
111
112 while (I != MBB.begin()) {
113 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000114 if (I->isDebugValue())
115 continue;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000116 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000117 I->getOpcode() != MSP430::JCC &&
118 I->getOpcode() != MSP430::Br &&
119 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000120 break;
121 // Remove the branch.
122 I->eraseFromParent();
123 I = MBB.end();
124 ++Count;
125 }
126
127 return Count;
128}
129
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000130bool MSP430InstrInfo::
131ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
132 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
133
134 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
135
136 switch (CC) {
137 default:
138 assert(0 && "Invalid branch condition!");
139 break;
140 case MSP430CC::COND_E:
141 CC = MSP430CC::COND_NE;
142 break;
143 case MSP430CC::COND_NE:
144 CC = MSP430CC::COND_E;
145 break;
146 case MSP430CC::COND_L:
147 CC = MSP430CC::COND_GE;
148 break;
149 case MSP430CC::COND_GE:
150 CC = MSP430CC::COND_L;
151 break;
152 case MSP430CC::COND_HS:
153 CC = MSP430CC::COND_LO;
154 break;
155 case MSP430CC::COND_LO:
156 CC = MSP430CC::COND_HS;
157 break;
158 }
159
160 Cond[0].setImm(CC);
161 return false;
162}
163
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000164bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000165 const MCInstrDesc &MCID = MI->getDesc();
166 if (!MCID.isTerminator()) return false;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000167
168 // Conditional branch is a special case.
Evan Chenge837dea2011-06-28 19:10:37 +0000169 if (MCID.isBranch() && !MCID.isBarrier())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000170 return true;
Evan Chenge837dea2011-06-28 19:10:37 +0000171 if (!MCID.isPredicable())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000172 return true;
173 return !isPredicated(MI);
174}
175
176bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
177 MachineBasicBlock *&TBB,
178 MachineBasicBlock *&FBB,
179 SmallVectorImpl<MachineOperand> &Cond,
180 bool AllowModify) const {
181 // Start from the bottom of the block and work up, examining the
182 // terminator instructions.
183 MachineBasicBlock::iterator I = MBB.end();
184 while (I != MBB.begin()) {
185 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000186 if (I->isDebugValue())
187 continue;
188
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000189 // Working from the bottom, when we see a non-terminator
190 // instruction, we're done.
191 if (!isUnpredicatedTerminator(I))
192 break;
193
194 // A terminator that isn't a branch can't easily be handled
195 // by this analysis.
196 if (!I->getDesc().isBranch())
197 return true;
198
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000199 // Cannot handle indirect branches.
200 if (I->getOpcode() == MSP430::Br ||
201 I->getOpcode() == MSP430::Bm)
202 return true;
203
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000204 // Handle unconditional branches.
205 if (I->getOpcode() == MSP430::JMP) {
206 if (!AllowModify) {
207 TBB = I->getOperand(0).getMBB();
208 continue;
209 }
210
211 // If the block has any instructions after a JMP, delete them.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000212 while (llvm::next(I) != MBB.end())
213 llvm::next(I)->eraseFromParent();
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000214 Cond.clear();
215 FBB = 0;
216
217 // Delete the JMP if it's equivalent to a fall-through.
218 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
219 TBB = 0;
220 I->eraseFromParent();
221 I = MBB.end();
222 continue;
223 }
224
225 // TBB is used to indicate the unconditinal destination.
226 TBB = I->getOperand(0).getMBB();
227 continue;
228 }
229
230 // Handle conditional branches.
231 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
232 MSP430CC::CondCodes BranchCode =
233 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
234 if (BranchCode == MSP430CC::COND_INVALID)
235 return true; // Can't handle weird stuff.
236
237 // Working from the bottom, handle the first conditional branch.
238 if (Cond.empty()) {
239 FBB = TBB;
240 TBB = I->getOperand(0).getMBB();
241 Cond.push_back(MachineOperand::CreateImm(BranchCode));
242 continue;
243 }
244
245 // Handle subsequent conditional branches. Only handle the case where all
246 // conditional branches branch to the same destination.
247 assert(Cond.size() == 1);
248 assert(TBB);
249
250 // Only handle the case where all conditional branches branch to
251 // the same destination.
252 if (TBB != I->getOperand(0).getMBB())
253 return true;
254
255 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
256 // If the conditions are the same, we can leave them alone.
257 if (OldBranchCode == BranchCode)
258 continue;
259
260 return true;
261 }
262
263 return false;
264}
265
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000266unsigned
267MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
268 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000269 const SmallVectorImpl<MachineOperand> &Cond,
270 DebugLoc DL) const {
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000271 // Shouldn't be a fall through.
272 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
273 assert((Cond.size() == 1 || Cond.size() == 0) &&
274 "MSP430 branch conditions have one component!");
275
276 if (Cond.empty()) {
277 // Unconditional branch?
278 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000279 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000280 return 1;
281 }
282
283 // Conditional branch.
284 unsigned Count = 0;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000285 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000286 ++Count;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000287
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000288 if (FBB) {
289 // Two-way Conditional branch. Insert the second branch.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000290 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000291 ++Count;
292 }
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000293 return Count;
294}
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000295
296/// GetInstSize - Return the number of bytes of code the specified
297/// instruction may be. This returns the maximum number of bytes.
298///
299unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000300 const MCInstrDesc &Desc = MI->getDesc();
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000301
302 switch (Desc.TSFlags & MSP430II::SizeMask) {
303 default:
304 switch (Desc.getOpcode()) {
305 default:
306 assert(0 && "Unknown instruction size!");
Bill Wendling7431bea2010-07-16 22:20:36 +0000307 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000308 case TargetOpcode::EH_LABEL:
309 case TargetOpcode::IMPLICIT_DEF:
310 case TargetOpcode::KILL:
Dale Johannesen375be772010-04-07 19:51:44 +0000311 case TargetOpcode::DBG_VALUE:
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000312 return 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000313 case TargetOpcode::INLINEASM: {
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000314 const MachineFunction *MF = MI->getParent()->getParent();
315 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
316 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
317 *MF->getTarget().getMCAsmInfo());
318 }
319 }
320 case MSP430II::SizeSpecial:
321 switch (MI->getOpcode()) {
322 default:
323 assert(0 && "Unknown instruction size!");
324 case MSP430::SAR8r1c:
325 case MSP430::SAR16r1c:
326 return 4;
327 }
328 case MSP430II::Size2Bytes:
329 return 2;
330 case MSP430II::Size4Bytes:
331 return 4;
332 case MSP430II::Size6Bytes:
333 return 6;
334 }
335
336 return 6;
337}
Evan Cheng59ee62d2011-07-11 03:57:24 +0000338
339MCInstrInfo *createMSP430MCInstrInfo() {
340 MCInstrInfo *X = new MCInstrInfo();
341 InitMSP430MCInstrInfo(X);
342 return X;
343}
344
345extern "C" void LLVMInitializeMSP430MCInstrInfo() {
346 TargetRegistry::RegisterMCInstrInfo(TheMSP430Target, createMSP430MCInstrInfo);
347}