blob: 8ea84906bf08e79905ea887d3b0e441f700ecbb3 [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
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000104unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
105 MachineBasicBlock::iterator I = MBB.end();
106 unsigned Count = 0;
107
108 while (I != MBB.begin()) {
109 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000110 if (I->isDebugValue())
111 continue;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000112 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000113 I->getOpcode() != MSP430::JCC &&
114 I->getOpcode() != MSP430::Br &&
115 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000116 break;
117 // Remove the branch.
118 I->eraseFromParent();
119 I = MBB.end();
120 ++Count;
121 }
122
123 return Count;
124}
125
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000126bool MSP430InstrInfo::
127ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
128 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
129
130 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
131
132 switch (CC) {
133 default:
134 assert(0 && "Invalid branch condition!");
135 break;
136 case MSP430CC::COND_E:
137 CC = MSP430CC::COND_NE;
138 break;
139 case MSP430CC::COND_NE:
140 CC = MSP430CC::COND_E;
141 break;
142 case MSP430CC::COND_L:
143 CC = MSP430CC::COND_GE;
144 break;
145 case MSP430CC::COND_GE:
146 CC = MSP430CC::COND_L;
147 break;
148 case MSP430CC::COND_HS:
149 CC = MSP430CC::COND_LO;
150 break;
151 case MSP430CC::COND_LO:
152 CC = MSP430CC::COND_HS;
153 break;
154 }
155
156 Cond[0].setImm(CC);
157 return false;
158}
159
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000160bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000161 const MCInstrDesc &MCID = MI->getDesc();
162 if (!MCID.isTerminator()) return false;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000163
164 // Conditional branch is a special case.
Evan Chenge837dea2011-06-28 19:10:37 +0000165 if (MCID.isBranch() && !MCID.isBarrier())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000166 return true;
Evan Chenge837dea2011-06-28 19:10:37 +0000167 if (!MCID.isPredicable())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000168 return true;
169 return !isPredicated(MI);
170}
171
172bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
173 MachineBasicBlock *&TBB,
174 MachineBasicBlock *&FBB,
175 SmallVectorImpl<MachineOperand> &Cond,
176 bool AllowModify) const {
177 // Start from the bottom of the block and work up, examining the
178 // terminator instructions.
179 MachineBasicBlock::iterator I = MBB.end();
180 while (I != MBB.begin()) {
181 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000182 if (I->isDebugValue())
183 continue;
184
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000185 // Working from the bottom, when we see a non-terminator
186 // instruction, we're done.
187 if (!isUnpredicatedTerminator(I))
188 break;
189
190 // A terminator that isn't a branch can't easily be handled
191 // by this analysis.
192 if (!I->getDesc().isBranch())
193 return true;
194
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000195 // Cannot handle indirect branches.
196 if (I->getOpcode() == MSP430::Br ||
197 I->getOpcode() == MSP430::Bm)
198 return true;
199
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000200 // Handle unconditional branches.
201 if (I->getOpcode() == MSP430::JMP) {
202 if (!AllowModify) {
203 TBB = I->getOperand(0).getMBB();
204 continue;
205 }
206
207 // If the block has any instructions after a JMP, delete them.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000208 while (llvm::next(I) != MBB.end())
209 llvm::next(I)->eraseFromParent();
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000210 Cond.clear();
211 FBB = 0;
212
213 // Delete the JMP if it's equivalent to a fall-through.
214 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
215 TBB = 0;
216 I->eraseFromParent();
217 I = MBB.end();
218 continue;
219 }
220
221 // TBB is used to indicate the unconditinal destination.
222 TBB = I->getOperand(0).getMBB();
223 continue;
224 }
225
226 // Handle conditional branches.
227 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
228 MSP430CC::CondCodes BranchCode =
229 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
230 if (BranchCode == MSP430CC::COND_INVALID)
231 return true; // Can't handle weird stuff.
232
233 // Working from the bottom, handle the first conditional branch.
234 if (Cond.empty()) {
235 FBB = TBB;
236 TBB = I->getOperand(0).getMBB();
237 Cond.push_back(MachineOperand::CreateImm(BranchCode));
238 continue;
239 }
240
241 // Handle subsequent conditional branches. Only handle the case where all
242 // conditional branches branch to the same destination.
243 assert(Cond.size() == 1);
244 assert(TBB);
245
246 // Only handle the case where all conditional branches branch to
247 // the same destination.
248 if (TBB != I->getOperand(0).getMBB())
249 return true;
250
251 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
252 // If the conditions are the same, we can leave them alone.
253 if (OldBranchCode == BranchCode)
254 continue;
255
256 return true;
257 }
258
259 return false;
260}
261
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000262unsigned
263MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
264 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000265 const SmallVectorImpl<MachineOperand> &Cond,
266 DebugLoc DL) const {
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000267 // Shouldn't be a fall through.
268 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
269 assert((Cond.size() == 1 || Cond.size() == 0) &&
270 "MSP430 branch conditions have one component!");
271
272 if (Cond.empty()) {
273 // Unconditional branch?
274 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000275 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000276 return 1;
277 }
278
279 // Conditional branch.
280 unsigned Count = 0;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000281 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000282 ++Count;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000283
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000284 if (FBB) {
285 // Two-way Conditional branch. Insert the second branch.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000286 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000287 ++Count;
288 }
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000289 return Count;
290}
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000291
292/// GetInstSize - Return the number of bytes of code the specified
293/// instruction may be. This returns the maximum number of bytes.
294///
295unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000296 const MCInstrDesc &Desc = MI->getDesc();
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000297
298 switch (Desc.TSFlags & MSP430II::SizeMask) {
299 default:
300 switch (Desc.getOpcode()) {
301 default:
302 assert(0 && "Unknown instruction size!");
Bill Wendling7431bea2010-07-16 22:20:36 +0000303 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000304 case TargetOpcode::EH_LABEL:
305 case TargetOpcode::IMPLICIT_DEF:
306 case TargetOpcode::KILL:
Dale Johannesen375be772010-04-07 19:51:44 +0000307 case TargetOpcode::DBG_VALUE:
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000308 return 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000309 case TargetOpcode::INLINEASM: {
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000310 const MachineFunction *MF = MI->getParent()->getParent();
311 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
312 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
313 *MF->getTarget().getMCAsmInfo());
314 }
315 }
316 case MSP430II::SizeSpecial:
317 switch (MI->getOpcode()) {
318 default:
319 assert(0 && "Unknown instruction size!");
320 case MSP430::SAR8r1c:
321 case MSP430::SAR16r1c:
322 return 4;
323 }
324 case MSP430II::Size2Bytes:
325 return 2;
326 case MSP430II::Size4Bytes:
327 return 4;
328 case MSP430II::Size6Bytes:
329 return 6;
330 }
331
332 return 6;
333}