blob: be332f05b30bee33a1bbdb71528e23a655f85ac8 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- MSP430InstrInfo.cpp - MSP430 Instruction Information --------------===//
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +00002//
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
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000014#include "MSP430InstrInfo.h"
Craig Topper79aa3412012-03-17 18:46:09 +000015#include "MSP430.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"
Torok Edwinc25e7582009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000024
Evan Cheng4db3cff2011-07-01 17:57:27 +000025#define GET_INSTRINFO_CTOR
Evan Cheng22fee2d2011-06-28 20:07:07 +000026#include "MSP430GenInstrInfo.inc"
27
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000028using namespace llvm;
29
30MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000031 : MSP430GenInstrInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
Benjamin Kramera7542d52012-06-06 18:25:08 +000032 RI(tm, *this) {}
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 =
Jay Foad978e0df2011-11-15 07:34:52 +000045 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +000046 MachineMemOperand::MOStore,
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000047 MFI.getObjectSize(FrameIdx),
48 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovaa299152009-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 Korobeynikov8046ef42009-11-07 17:13:57 +000053 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-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 Korobeynikov8046ef42009-11-07 17:13:57 +000057 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovaa299152009-05-03 13:09:57 +000058 else
Torok Edwinc23197a2009-07-14 16:55:14 +000059 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovaa299152009-05-03 13:09:57 +000060}
61
62void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator MI,
64 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +000065 const TargetRegisterClass *RC,
66 const TargetRegisterInfo *TRI) const{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000067 DebugLoc DL;
Anton Korobeynikovaa299152009-05-03 13:09:57 +000068 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikov8046ef42009-11-07 17:13:57 +000069 MachineFunction &MF = *MBB.getParent();
70 MachineFrameInfo &MFI = *MF.getFrameInfo();
71
72 MachineMemOperand *MMO =
Jay Foad978e0df2011-11-15 07:34:52 +000073 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +000074 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) {
Craig Topperbc219812012-02-07 02:50:20 +0000133 default: llvm_unreachable("Invalid branch condition!");
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000134 case MSP430CC::COND_E:
135 CC = MSP430CC::COND_NE;
136 break;
137 case MSP430CC::COND_NE:
138 CC = MSP430CC::COND_E;
139 break;
140 case MSP430CC::COND_L:
141 CC = MSP430CC::COND_GE;
142 break;
143 case MSP430CC::COND_GE:
144 CC = MSP430CC::COND_L;
145 break;
146 case MSP430CC::COND_HS:
147 CC = MSP430CC::COND_LO;
148 break;
149 case MSP430CC::COND_LO:
150 CC = MSP430CC::COND_HS;
151 break;
152 }
153
154 Cond[0].setImm(CC);
155 return false;
156}
157
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000158bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000159 if (!MI->isTerminator()) return false;
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000160
161 // Conditional branch is a special case.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000162 if (MI->isBranch() && !MI->isBarrier())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000163 return true;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000164 if (!MI->isPredicable())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000165 return true;
166 return !isPredicated(MI);
167}
168
169bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
170 MachineBasicBlock *&TBB,
171 MachineBasicBlock *&FBB,
172 SmallVectorImpl<MachineOperand> &Cond,
173 bool AllowModify) const {
174 // Start from the bottom of the block and work up, examining the
175 // terminator instructions.
176 MachineBasicBlock::iterator I = MBB.end();
177 while (I != MBB.begin()) {
178 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000179 if (I->isDebugValue())
180 continue;
181
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000182 // Working from the bottom, when we see a non-terminator
183 // instruction, we're done.
184 if (!isUnpredicatedTerminator(I))
185 break;
186
187 // A terminator that isn't a branch can't easily be handled
188 // by this analysis.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000189 if (!I->isBranch())
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000190 return true;
191
Anton Korobeynikov69d5b482010-05-01 12:04:32 +0000192 // Cannot handle indirect branches.
193 if (I->getOpcode() == MSP430::Br ||
194 I->getOpcode() == MSP430::Bm)
195 return true;
196
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000197 // Handle unconditional branches.
198 if (I->getOpcode() == MSP430::JMP) {
199 if (!AllowModify) {
200 TBB = I->getOperand(0).getMBB();
201 continue;
202 }
203
204 // If the block has any instructions after a JMP, delete them.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000205 while (llvm::next(I) != MBB.end())
206 llvm::next(I)->eraseFromParent();
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000207 Cond.clear();
208 FBB = 0;
209
210 // Delete the JMP if it's equivalent to a fall-through.
211 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
212 TBB = 0;
213 I->eraseFromParent();
214 I = MBB.end();
215 continue;
216 }
217
218 // TBB is used to indicate the unconditinal destination.
219 TBB = I->getOperand(0).getMBB();
220 continue;
221 }
222
223 // Handle conditional branches.
224 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
225 MSP430CC::CondCodes BranchCode =
226 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
227 if (BranchCode == MSP430CC::COND_INVALID)
228 return true; // Can't handle weird stuff.
229
230 // Working from the bottom, handle the first conditional branch.
231 if (Cond.empty()) {
232 FBB = TBB;
233 TBB = I->getOperand(0).getMBB();
234 Cond.push_back(MachineOperand::CreateImm(BranchCode));
235 continue;
236 }
237
238 // Handle subsequent conditional branches. Only handle the case where all
239 // conditional branches branch to the same destination.
240 assert(Cond.size() == 1);
241 assert(TBB);
242
243 // Only handle the case where all conditional branches branch to
244 // the same destination.
245 if (TBB != I->getOperand(0).getMBB())
246 return true;
247
248 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
249 // If the conditions are the same, we can leave them alone.
250 if (OldBranchCode == BranchCode)
251 continue;
252
253 return true;
254 }
255
256 return false;
257}
258
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000259unsigned
260MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
261 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000262 const SmallVectorImpl<MachineOperand> &Cond,
263 DebugLoc DL) const {
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000264 // Shouldn't be a fall through.
265 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
266 assert((Cond.size() == 1 || Cond.size() == 0) &&
267 "MSP430 branch conditions have one component!");
268
269 if (Cond.empty()) {
270 // Unconditional branch?
271 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000272 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000273 return 1;
274 }
275
276 // Conditional branch.
277 unsigned Count = 0;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000278 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000279 ++Count;
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000280
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000281 if (FBB) {
282 // Two-way Conditional branch. Insert the second branch.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000283 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov90593d22009-10-21 19:17:18 +0000284 ++Count;
285 }
Anton Korobeynikov8644af32009-05-03 13:15:22 +0000286 return Count;
287}
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000288
289/// GetInstSize - Return the number of bytes of code the specified
290/// instruction may be. This returns the maximum number of bytes.
291///
292unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000293 const MCInstrDesc &Desc = MI->getDesc();
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000294
295 switch (Desc.TSFlags & MSP430II::SizeMask) {
296 default:
297 switch (Desc.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +0000298 default: llvm_unreachable("Unknown instruction size!");
Bill Wendling7431bea2010-07-16 22:20:36 +0000299 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000300 case TargetOpcode::EH_LABEL:
301 case TargetOpcode::IMPLICIT_DEF:
302 case TargetOpcode::KILL:
Dale Johannesen375be772010-04-07 19:51:44 +0000303 case TargetOpcode::DBG_VALUE:
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000304 return 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000305 case TargetOpcode::INLINEASM: {
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000306 const MachineFunction *MF = MI->getParent()->getParent();
307 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
308 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
309 *MF->getTarget().getMCAsmInfo());
310 }
311 }
312 case MSP430II::SizeSpecial:
313 switch (MI->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +0000314 default: llvm_unreachable("Unknown instruction size!");
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000315 case MSP430::SAR8r1c:
316 case MSP430::SAR16r1c:
317 return 4;
318 }
319 case MSP430II::Size2Bytes:
320 return 2;
321 case MSP430II::Size4Bytes:
322 return 4;
323 case MSP430II::Size6Bytes:
324 return 6;
325 }
Anton Korobeynikov702adab2010-01-15 21:19:05 +0000326}