blob: 9d3c7e9d7f302e6c224dd4a633c1b0fedb0cd5dc [file] [log] [blame]
Anton Korobeynikov10138002009-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 Korobeynikov1af0b612009-05-03 13:11:04 +000016#include "MSP430MachineFunctionInfo.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000017#include "MSP430TargetMachine.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000018#include "llvm/Function.h"
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwin56d06592009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000024
Evan Cheng703a0fb2011-07-01 17:57:27 +000025#define GET_INSTRINFO_CTOR
Evan Cheng1e210d02011-06-28 20:07:07 +000026#include "MSP430GenInstrInfo.inc"
27
Anton Korobeynikov10138002009-05-03 12:57:15 +000028using namespace llvm;
29
30MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
Evan Cheng703a0fb2011-07-01 17:57:27 +000031 : MSP430GenInstrInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
Anton Korobeynikovec3f0b32009-05-03 13:07:54 +000032 RI(tm, *this), TM(tm) {}
Anton Korobeynikovd7afd692009-05-03 13:02:04 +000033
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000034void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
35 MachineBasicBlock::iterator MI,
36 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +000037 const TargetRegisterClass *RC,
38 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +000039 DebugLoc DL;
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000040 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000041 MachineFunction &MF = *MBB.getParent();
42 MachineFrameInfo &MFI = *MF.getFrameInfo();
43
44 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +000045 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +000046 MachineMemOperand::MOStore,
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000047 MFI.getObjectSize(FrameIdx),
48 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000049
50 if (RC == &MSP430::GR16RegClass)
51 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
52 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000053 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000054 else if (RC == &MSP430::GR8RegClass)
55 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
56 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000057 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000058 else
Torok Edwinfbcc6632009-07-14 16:55:14 +000059 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000060}
61
62void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator MI,
64 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +000065 const TargetRegisterClass *RC,
66 const TargetRegisterInfo *TRI) const{
Chris Lattner6f306d72010-04-02 20:16:16 +000067 DebugLoc DL;
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000068 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000069 MachineFunction &MF = *MBB.getParent();
70 MachineFrameInfo &MFI = *MF.getFrameInfo();
71
72 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +000073 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +000074 MachineMemOperand::MOLoad,
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000075 MFI.getObjectSize(FrameIdx),
76 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000077
78 if (RC == &MSP430::GR16RegClass)
79 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000080 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000081 else if (RC == &MSP430::GR8RegClass)
82 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000083 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000084 else
Torok Edwinfbcc6632009-07-14 16:55:14 +000085 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000086}
87
Jakob Stoklund Olesen65306362010-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 Korobeynikov80a73e72009-05-03 13:05:42 +000099
Jakob Stoklund Olesen65306362010-07-11 06:53:30 +0000100 BuildMI(MBB, I, DL, get(Opc), DestReg)
101 .addReg(SrcReg, getKillRegState(KillSrc));
Anton Korobeynikovd7afd692009-05-03 13:02:04 +0000102}
103
Anton Korobeynikov5399c2d2009-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 Johannesen4244d122010-04-02 01:38:09 +0000110 if (I->isDebugValue())
111 continue;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000112 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000113 I->getOpcode() != MSP430::JCC &&
114 I->getOpcode() != MSP430::Br &&
115 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov5399c2d2009-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 Korobeynikov5399c2d2009-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 Korobeynikov5399c2d2009-10-21 19:17:18 +0000160bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000161 if (!MI->isTerminator()) return false;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000162
163 // Conditional branch is a special case.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000164 if (MI->isBranch() && !MI->isBarrier())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000165 return true;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000166 if (!MI->isPredicable())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000167 return true;
168 return !isPredicated(MI);
169}
170
171bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
172 MachineBasicBlock *&TBB,
173 MachineBasicBlock *&FBB,
174 SmallVectorImpl<MachineOperand> &Cond,
175 bool AllowModify) const {
176 // Start from the bottom of the block and work up, examining the
177 // terminator instructions.
178 MachineBasicBlock::iterator I = MBB.end();
179 while (I != MBB.begin()) {
180 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000181 if (I->isDebugValue())
182 continue;
183
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000184 // Working from the bottom, when we see a non-terminator
185 // instruction, we're done.
186 if (!isUnpredicatedTerminator(I))
187 break;
188
189 // A terminator that isn't a branch can't easily be handled
190 // by this analysis.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000191 if (!I->isBranch())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000192 return true;
193
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000194 // Cannot handle indirect branches.
195 if (I->getOpcode() == MSP430::Br ||
196 I->getOpcode() == MSP430::Bm)
197 return true;
198
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000199 // Handle unconditional branches.
200 if (I->getOpcode() == MSP430::JMP) {
201 if (!AllowModify) {
202 TBB = I->getOperand(0).getMBB();
203 continue;
204 }
205
206 // If the block has any instructions after a JMP, delete them.
Chris Lattnera48f44d2009-12-03 00:50:42 +0000207 while (llvm::next(I) != MBB.end())
208 llvm::next(I)->eraseFromParent();
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000209 Cond.clear();
210 FBB = 0;
211
212 // Delete the JMP if it's equivalent to a fall-through.
213 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
214 TBB = 0;
215 I->eraseFromParent();
216 I = MBB.end();
217 continue;
218 }
219
220 // TBB is used to indicate the unconditinal destination.
221 TBB = I->getOperand(0).getMBB();
222 continue;
223 }
224
225 // Handle conditional branches.
226 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
227 MSP430CC::CondCodes BranchCode =
228 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
229 if (BranchCode == MSP430CC::COND_INVALID)
230 return true; // Can't handle weird stuff.
231
232 // Working from the bottom, handle the first conditional branch.
233 if (Cond.empty()) {
234 FBB = TBB;
235 TBB = I->getOperand(0).getMBB();
236 Cond.push_back(MachineOperand::CreateImm(BranchCode));
237 continue;
238 }
239
240 // Handle subsequent conditional branches. Only handle the case where all
241 // conditional branches branch to the same destination.
242 assert(Cond.size() == 1);
243 assert(TBB);
244
245 // Only handle the case where all conditional branches branch to
246 // the same destination.
247 if (TBB != I->getOperand(0).getMBB())
248 return true;
249
250 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
251 // If the conditions are the same, we can leave them alone.
252 if (OldBranchCode == BranchCode)
253 continue;
254
255 return true;
256 }
257
258 return false;
259}
260
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000261unsigned
262MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
263 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000264 const SmallVectorImpl<MachineOperand> &Cond,
265 DebugLoc DL) const {
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000266 // Shouldn't be a fall through.
267 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
268 assert((Cond.size() == 1 || Cond.size() == 0) &&
269 "MSP430 branch conditions have one component!");
270
271 if (Cond.empty()) {
272 // Unconditional branch?
273 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattner6f306d72010-04-02 20:16:16 +0000274 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000275 return 1;
276 }
277
278 // Conditional branch.
279 unsigned Count = 0;
Chris Lattner6f306d72010-04-02 20:16:16 +0000280 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000281 ++Count;
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000282
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000283 if (FBB) {
284 // Two-way Conditional branch. Insert the second branch.
Chris Lattner6f306d72010-04-02 20:16:16 +0000285 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000286 ++Count;
287 }
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000288 return Count;
289}
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000290
291/// GetInstSize - Return the number of bytes of code the specified
292/// instruction may be. This returns the maximum number of bytes.
293///
294unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000295 const MCInstrDesc &Desc = MI->getDesc();
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000296
297 switch (Desc.TSFlags & MSP430II::SizeMask) {
298 default:
299 switch (Desc.getOpcode()) {
300 default:
301 assert(0 && "Unknown instruction size!");
Bill Wendling499f7972010-07-16 22:20:36 +0000302 case TargetOpcode::PROLOG_LABEL:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000303 case TargetOpcode::EH_LABEL:
304 case TargetOpcode::IMPLICIT_DEF:
305 case TargetOpcode::KILL:
Dale Johannesen60b28972010-04-07 19:51:44 +0000306 case TargetOpcode::DBG_VALUE:
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000307 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000308 case TargetOpcode::INLINEASM: {
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000309 const MachineFunction *MF = MI->getParent()->getParent();
310 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
311 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
312 *MF->getTarget().getMCAsmInfo());
313 }
314 }
315 case MSP430II::SizeSpecial:
316 switch (MI->getOpcode()) {
317 default:
318 assert(0 && "Unknown instruction size!");
319 case MSP430::SAR8r1c:
320 case MSP430::SAR16r1c:
321 return 4;
322 }
323 case MSP430II::Size2Bytes:
324 return 2;
325 case MSP430II::Size4Bytes:
326 return 4;
327 case MSP430II::Size6Bytes:
328 return 6;
329 }
330
331 return 6;
332}