blob: b883f46dc6b891af22f5c3cdd70da3d97dae1e89 [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"
Torok Edwinc25e7582009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000024
Evan Cheng22fee2d2011-06-28 20:07:07 +000025#define GET_INSTRINFO_MC_DESC
26#include "MSP430GenInstrInfo.inc"
27
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000028using namespace llvm;
29
30MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
31 : TargetInstrInfoImpl(MSP430Insts, array_lengthof(MSP430Insts)),
Anton Korobeynikovb5612642009-05-03 13:07:54 +000032 RI(tm, *this), TM(tm) {}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000033
Anton Korobeynikovaa299152009-05-03 13:09:57 +000034void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
35 MachineBasicBlock::iterator MI,
36 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +000037 const TargetRegisterClass *RC,
38 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000039 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000040 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000041 MachineFunction &MF = *MBB.getParent();
42 MachineFrameInfo &MFI = *MF.getFrameInfo();
43
44 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +000045 MF.getMachineMemOperand(
46 MachinePointerInfo(PseudoSourceValue::getFixedStack(FrameIdx)),
47 MachineMemOperand::MOStore,
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000048 MFI.getObjectSize(FrameIdx),
49 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000050
51 if (RC == &MSP430::GR16RegClass)
52 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
53 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000054 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000055 else if (RC == &MSP430::GR8RegClass)
56 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
57 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000058 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000059 else
Torok Edwinc23197a2009-07-14 16:55:14 +000060 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000061}
62
63void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
64 MachineBasicBlock::iterator MI,
65 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +000066 const TargetRegisterClass *RC,
67 const TargetRegisterInfo *TRI) const{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000068 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000069 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000070 MachineFunction &MF = *MBB.getParent();
71 MachineFrameInfo &MFI = *MF.getFrameInfo();
72
73 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +000074 MF.getMachineMemOperand(
75 MachinePointerInfo(PseudoSourceValue::getFixedStack(FrameIdx)),
76 MachineMemOperand::MOLoad,
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000077 MFI.getObjectSize(FrameIdx),
78 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-05-03 13:09:57 +000079
80 if (RC == &MSP430::GR16RegClass)
81 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000082 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000083 else if (RC == &MSP430::GR8RegClass)
84 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000085 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000086 else
Torok Edwinc23197a2009-07-14 16:55:14 +000087 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000088}
89
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +000090void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
91 MachineBasicBlock::iterator I, DebugLoc DL,
92 unsigned DestReg, unsigned SrcReg,
93 bool KillSrc) const {
94 unsigned Opc;
95 if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
96 Opc = MSP430::MOV16rr;
97 else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
98 Opc = MSP430::MOV8rr;
99 else
100 llvm_unreachable("Impossible reg-to-reg copy");
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000101
Jakob Stoklund Olesen41ce3cf2010-07-11 06:53:30 +0000102 BuildMI(MBB, I, DL, get(Opc), DestReg)
103 .addReg(SrcReg, getKillRegState(KillSrc));
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000104}
105
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000106unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
107 MachineBasicBlock::iterator I = MBB.end();
108 unsigned Count = 0;
109
110 while (I != MBB.begin()) {
111 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000112 if (I->isDebugValue())
113 continue;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000114 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000115 I->getOpcode() != MSP430::JCC &&
116 I->getOpcode() != MSP430::Br &&
117 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000118 break;
119 // Remove the branch.
120 I->eraseFromParent();
121 I = MBB.end();
122 ++Count;
123 }
124
125 return Count;
126}
127
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000128bool MSP430InstrInfo::
129ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
130 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
131
132 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
133
134 switch (CC) {
135 default:
136 assert(0 && "Invalid branch condition!");
137 break;
138 case MSP430CC::COND_E:
139 CC = MSP430CC::COND_NE;
140 break;
141 case MSP430CC::COND_NE:
142 CC = MSP430CC::COND_E;
143 break;
144 case MSP430CC::COND_L:
145 CC = MSP430CC::COND_GE;
146 break;
147 case MSP430CC::COND_GE:
148 CC = MSP430CC::COND_L;
149 break;
150 case MSP430CC::COND_HS:
151 CC = MSP430CC::COND_LO;
152 break;
153 case MSP430CC::COND_LO:
154 CC = MSP430CC::COND_HS;
155 break;
156 }
157
158 Cond[0].setImm(CC);
159 return false;
160}
161
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000162bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000163 const MCInstrDesc &MCID = MI->getDesc();
164 if (!MCID.isTerminator()) return false;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000165
166 // Conditional branch is a special case.
Evan Chenge837dea2011-06-28 19:10:37 +0000167 if (MCID.isBranch() && !MCID.isBarrier())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000168 return true;
Evan Chenge837dea2011-06-28 19:10:37 +0000169 if (!MCID.isPredicable())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000170 return true;
171 return !isPredicated(MI);
172}
173
174bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
175 MachineBasicBlock *&TBB,
176 MachineBasicBlock *&FBB,
177 SmallVectorImpl<MachineOperand> &Cond,
178 bool AllowModify) const {
179 // Start from the bottom of the block and work up, examining the
180 // terminator instructions.
181 MachineBasicBlock::iterator I = MBB.end();
182 while (I != MBB.begin()) {
183 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000184 if (I->isDebugValue())
185 continue;
186
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000187 // Working from the bottom, when we see a non-terminator
188 // instruction, we're done.
189 if (!isUnpredicatedTerminator(I))
190 break;
191
192 // A terminator that isn't a branch can't easily be handled
193 // by this analysis.
194 if (!I->getDesc().isBranch())
195 return true;
196
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000197 // Cannot handle indirect branches.
198 if (I->getOpcode() == MSP430::Br ||
199 I->getOpcode() == MSP430::Bm)
200 return true;
201
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000202 // Handle unconditional branches.
203 if (I->getOpcode() == MSP430::JMP) {
204 if (!AllowModify) {
205 TBB = I->getOperand(0).getMBB();
206 continue;
207 }
208
209 // If the block has any instructions after a JMP, delete them.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000210 while (llvm::next(I) != MBB.end())
211 llvm::next(I)->eraseFromParent();
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000212 Cond.clear();
213 FBB = 0;
214
215 // Delete the JMP if it's equivalent to a fall-through.
216 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
217 TBB = 0;
218 I->eraseFromParent();
219 I = MBB.end();
220 continue;
221 }
222
223 // TBB is used to indicate the unconditinal destination.
224 TBB = I->getOperand(0).getMBB();
225 continue;
226 }
227
228 // Handle conditional branches.
229 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
230 MSP430CC::CondCodes BranchCode =
231 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
232 if (BranchCode == MSP430CC::COND_INVALID)
233 return true; // Can't handle weird stuff.
234
235 // Working from the bottom, handle the first conditional branch.
236 if (Cond.empty()) {
237 FBB = TBB;
238 TBB = I->getOperand(0).getMBB();
239 Cond.push_back(MachineOperand::CreateImm(BranchCode));
240 continue;
241 }
242
243 // Handle subsequent conditional branches. Only handle the case where all
244 // conditional branches branch to the same destination.
245 assert(Cond.size() == 1);
246 assert(TBB);
247
248 // Only handle the case where all conditional branches branch to
249 // the same destination.
250 if (TBB != I->getOperand(0).getMBB())
251 return true;
252
253 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
254 // If the conditions are the same, we can leave them alone.
255 if (OldBranchCode == BranchCode)
256 continue;
257
258 return true;
259 }
260
261 return false;
262}
263
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000264unsigned
265MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
266 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000267 const SmallVectorImpl<MachineOperand> &Cond,
268 DebugLoc DL) const {
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000269 // Shouldn't be a fall through.
270 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
271 assert((Cond.size() == 1 || Cond.size() == 0) &&
272 "MSP430 branch conditions have one component!");
273
274 if (Cond.empty()) {
275 // Unconditional branch?
276 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000277 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000278 return 1;
279 }
280
281 // Conditional branch.
282 unsigned Count = 0;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000283 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000284 ++Count;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000285
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000286 if (FBB) {
287 // Two-way Conditional branch. Insert the second branch.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000288 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000289 ++Count;
290 }
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000291 return Count;
292}
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000293
294/// GetInstSize - Return the number of bytes of code the specified
295/// instruction may be. This returns the maximum number of bytes.
296///
297unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000298 const MCInstrDesc &Desc = MI->getDesc();
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000299
300 switch (Desc.TSFlags & MSP430II::SizeMask) {
301 default:
302 switch (Desc.getOpcode()) {
303 default:
304 assert(0 && "Unknown instruction size!");
Bill Wendling7431bea2010-07-16 22:20:36 +0000305 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000306 case TargetOpcode::EH_LABEL:
307 case TargetOpcode::IMPLICIT_DEF:
308 case TargetOpcode::KILL:
Dale Johannesen375be772010-04-07 19:51:44 +0000309 case TargetOpcode::DBG_VALUE:
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000310 return 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000311 case TargetOpcode::INLINEASM: {
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000312 const MachineFunction *MF = MI->getParent()->getParent();
313 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
314 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
315 *MF->getTarget().getMCAsmInfo());
316 }
317 }
318 case MSP430II::SizeSpecial:
319 switch (MI->getOpcode()) {
320 default:
321 assert(0 && "Unknown instruction size!");
322 case MSP430::SAR8r1c:
323 case MSP430::SAR16r1c:
324 return 4;
325 }
326 case MSP430II::Size2Bytes:
327 return 2;
328 case MSP430II::Size4Bytes:
329 return 4;
330 case MSP430II::Size6Bytes:
331 return 6;
332 }
333
334 return 6;
335}