blob: 846d09361b33ef214189bebb6cd62cd4f04e12c7 [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#include "MSP430GenInstrInfo.inc"
28
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000029using namespace llvm;
30
31MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000032 : MSP430GenInstrInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
Anton Korobeynikovb5612642009-05-03 13:07:54 +000033 RI(tm, *this), TM(tm) {}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000034
Anton Korobeynikovaa299152009-05-03 13:09:57 +000035void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
36 MachineBasicBlock::iterator MI,
37 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +000038 const TargetRegisterClass *RC,
39 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000040 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000041 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000042 MachineFunction &MF = *MBB.getParent();
43 MachineFrameInfo &MFI = *MF.getFrameInfo();
44
45 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +000046 MF.getMachineMemOperand(
47 MachinePointerInfo(PseudoSourceValue::getFixedStack(FrameIdx)),
48 MachineMemOperand::MOStore,
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000049 MFI.getObjectSize(FrameIdx),
50 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000051
52 if (RC == &MSP430::GR16RegClass)
53 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
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 if (RC == &MSP430::GR8RegClass)
57 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
58 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000059 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000060 else
Torok Edwinc23197a2009-07-14 16:55:14 +000061 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000062}
63
64void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
65 MachineBasicBlock::iterator MI,
66 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +000067 const TargetRegisterClass *RC,
68 const TargetRegisterInfo *TRI) const{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000069 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000070 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000071 MachineFunction &MF = *MBB.getParent();
72 MachineFrameInfo &MFI = *MF.getFrameInfo();
73
74 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +000075 MF.getMachineMemOperand(
76 MachinePointerInfo(PseudoSourceValue::getFixedStack(FrameIdx)),
77 MachineMemOperand::MOLoad,
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000078 MFI.getObjectSize(FrameIdx),
79 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000080
81 if (RC == &MSP430::GR16RegClass)
82 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
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 if (RC == &MSP430::GR8RegClass)
85 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000086 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000087 else
Torok Edwinc23197a2009-07-14 16:55:14 +000088 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000089}
90
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +000091void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
92 MachineBasicBlock::iterator I, DebugLoc DL,
93 unsigned DestReg, unsigned SrcReg,
94 bool KillSrc) const {
95 unsigned Opc;
96 if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
97 Opc = MSP430::MOV16rr;
98 else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
99 Opc = MSP430::MOV8rr;
100 else
101 llvm_unreachable("Impossible reg-to-reg copy");
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000102
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +0000103 BuildMI(MBB, I, DL, get(Opc), DestReg)
104 .addReg(SrcReg, getKillRegState(KillSrc));
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000105}
106
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000107unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
108 MachineBasicBlock::iterator I = MBB.end();
109 unsigned Count = 0;
110
111 while (I != MBB.begin()) {
112 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000113 if (I->isDebugValue())
114 continue;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000115 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000116 I->getOpcode() != MSP430::JCC &&
117 I->getOpcode() != MSP430::Br &&
118 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000119 break;
120 // Remove the branch.
121 I->eraseFromParent();
122 I = MBB.end();
123 ++Count;
124 }
125
126 return Count;
127}
128
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000129bool MSP430InstrInfo::
130ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
131 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
132
133 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
134
135 switch (CC) {
136 default:
137 assert(0 && "Invalid branch condition!");
138 break;
139 case MSP430CC::COND_E:
140 CC = MSP430CC::COND_NE;
141 break;
142 case MSP430CC::COND_NE:
143 CC = MSP430CC::COND_E;
144 break;
145 case MSP430CC::COND_L:
146 CC = MSP430CC::COND_GE;
147 break;
148 case MSP430CC::COND_GE:
149 CC = MSP430CC::COND_L;
150 break;
151 case MSP430CC::COND_HS:
152 CC = MSP430CC::COND_LO;
153 break;
154 case MSP430CC::COND_LO:
155 CC = MSP430CC::COND_HS;
156 break;
157 }
158
159 Cond[0].setImm(CC);
160 return false;
161}
162
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000163bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000164 const MCInstrDesc &MCID = MI->getDesc();
165 if (!MCID.isTerminator()) return false;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000166
167 // Conditional branch is a special case.
Evan Chenge837dea2011-06-28 19:10:37 +0000168 if (MCID.isBranch() && !MCID.isBarrier())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000169 return true;
Evan Chenge837dea2011-06-28 19:10:37 +0000170 if (!MCID.isPredicable())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000171 return true;
172 return !isPredicated(MI);
173}
174
175bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
176 MachineBasicBlock *&TBB,
177 MachineBasicBlock *&FBB,
178 SmallVectorImpl<MachineOperand> &Cond,
179 bool AllowModify) const {
180 // Start from the bottom of the block and work up, examining the
181 // terminator instructions.
182 MachineBasicBlock::iterator I = MBB.end();
183 while (I != MBB.begin()) {
184 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000185 if (I->isDebugValue())
186 continue;
187
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000188 // Working from the bottom, when we see a non-terminator
189 // instruction, we're done.
190 if (!isUnpredicatedTerminator(I))
191 break;
192
193 // A terminator that isn't a branch can't easily be handled
194 // by this analysis.
195 if (!I->getDesc().isBranch())
196 return true;
197
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000198 // Cannot handle indirect branches.
199 if (I->getOpcode() == MSP430::Br ||
200 I->getOpcode() == MSP430::Bm)
201 return true;
202
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000203 // Handle unconditional branches.
204 if (I->getOpcode() == MSP430::JMP) {
205 if (!AllowModify) {
206 TBB = I->getOperand(0).getMBB();
207 continue;
208 }
209
210 // If the block has any instructions after a JMP, delete them.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000211 while (llvm::next(I) != MBB.end())
212 llvm::next(I)->eraseFromParent();
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000213 Cond.clear();
214 FBB = 0;
215
216 // Delete the JMP if it's equivalent to a fall-through.
217 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
218 TBB = 0;
219 I->eraseFromParent();
220 I = MBB.end();
221 continue;
222 }
223
224 // TBB is used to indicate the unconditinal destination.
225 TBB = I->getOperand(0).getMBB();
226 continue;
227 }
228
229 // Handle conditional branches.
230 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
231 MSP430CC::CondCodes BranchCode =
232 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
233 if (BranchCode == MSP430CC::COND_INVALID)
234 return true; // Can't handle weird stuff.
235
236 // Working from the bottom, handle the first conditional branch.
237 if (Cond.empty()) {
238 FBB = TBB;
239 TBB = I->getOperand(0).getMBB();
240 Cond.push_back(MachineOperand::CreateImm(BranchCode));
241 continue;
242 }
243
244 // Handle subsequent conditional branches. Only handle the case where all
245 // conditional branches branch to the same destination.
246 assert(Cond.size() == 1);
247 assert(TBB);
248
249 // Only handle the case where all conditional branches branch to
250 // the same destination.
251 if (TBB != I->getOperand(0).getMBB())
252 return true;
253
254 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
255 // If the conditions are the same, we can leave them alone.
256 if (OldBranchCode == BranchCode)
257 continue;
258
259 return true;
260 }
261
262 return false;
263}
264
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000265unsigned
266MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
267 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000268 const SmallVectorImpl<MachineOperand> &Cond,
269 DebugLoc DL) const {
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000270 // Shouldn't be a fall through.
271 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
272 assert((Cond.size() == 1 || Cond.size() == 0) &&
273 "MSP430 branch conditions have one component!");
274
275 if (Cond.empty()) {
276 // Unconditional branch?
277 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000278 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000279 return 1;
280 }
281
282 // Conditional branch.
283 unsigned Count = 0;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000284 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000285 ++Count;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000286
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000287 if (FBB) {
288 // Two-way Conditional branch. Insert the second branch.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000289 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000290 ++Count;
291 }
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000292 return Count;
293}
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000294
295/// GetInstSize - Return the number of bytes of code the specified
296/// instruction may be. This returns the maximum number of bytes.
297///
298unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000299 const MCInstrDesc &Desc = MI->getDesc();
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000300
301 switch (Desc.TSFlags & MSP430II::SizeMask) {
302 default:
303 switch (Desc.getOpcode()) {
304 default:
305 assert(0 && "Unknown instruction size!");
Bill Wendling7431bea2010-07-16 22:20:36 +0000306 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000307 case TargetOpcode::EH_LABEL:
308 case TargetOpcode::IMPLICIT_DEF:
309 case TargetOpcode::KILL:
Dale Johannesen375be772010-04-07 19:51:44 +0000310 case TargetOpcode::DBG_VALUE:
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000311 return 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000312 case TargetOpcode::INLINEASM: {
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000313 const MachineFunction *MF = MI->getParent()->getParent();
314 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
315 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
316 *MF->getTarget().getMCAsmInfo());
317 }
318 }
319 case MSP430II::SizeSpecial:
320 switch (MI->getOpcode()) {
321 default:
322 assert(0 && "Unknown instruction size!");
323 case MSP430::SAR8r1c:
324 case MSP430::SAR16r1c:
325 return 4;
326 }
327 case MSP430II::Size2Bytes:
328 return 2;
329 case MSP430II::Size4Bytes:
330 return 4;
331 case MSP430II::Size6Bytes:
332 return 6;
333 }
334
335 return 6;
336}