blob: 38f73b902b8f656c8d377ce418e65fc13871a968 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- MSP430InstrInfo.cpp - MSP430 Instruction Information --------------===//
Anton Korobeynikov10138002009-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 Korobeynikov10138002009-05-03 12:57:15 +000014#include "MSP430InstrInfo.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "MSP430.h"
Anton Korobeynikov1af0b612009-05-03 13:11:04 +000016#include "MSP430MachineFunctionInfo.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000017#include "MSP430TargetMachine.h"
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
Anton Korobeynikov10138002009-05-03 12:57:15 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Function.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
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000025#define GET_INSTRINFO_CTOR_DTOR
Evan Cheng1e210d02011-06-28 20:07:07 +000026#include "MSP430GenInstrInfo.inc"
27
Anton Korobeynikov10138002009-05-03 12:57:15 +000028using namespace llvm;
29
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000030// Pin the vtable to this file.
31void MSP430InstrInfo::anchor() {}
32
Anton Korobeynikov10138002009-05-03 12:57:15 +000033MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
Evan Cheng703a0fb2011-07-01 17:57:27 +000034 : MSP430GenInstrInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
Bill Wendlingf5358212013-06-07 06:30:15 +000035 RI(tm) {}
Anton Korobeynikovd7afd692009-05-03 13:02:04 +000036
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000037void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
38 MachineBasicBlock::iterator MI,
39 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +000040 const TargetRegisterClass *RC,
41 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +000042 DebugLoc DL;
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000043 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000044 MachineFunction &MF = *MBB.getParent();
45 MachineFrameInfo &MFI = *MF.getFrameInfo();
46
47 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +000048 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +000049 MachineMemOperand::MOStore,
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000050 MFI.getObjectSize(FrameIdx),
51 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000052
53 if (RC == &MSP430::GR16RegClass)
54 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
55 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000056 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000057 else if (RC == &MSP430::GR8RegClass)
58 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
59 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000060 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000061 else
Torok Edwinfbcc6632009-07-14 16:55:14 +000062 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000063}
64
65void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
66 MachineBasicBlock::iterator MI,
67 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +000068 const TargetRegisterClass *RC,
69 const TargetRegisterInfo *TRI) const{
Chris Lattner6f306d72010-04-02 20:16:16 +000070 DebugLoc DL;
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000071 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000072 MachineFunction &MF = *MBB.getParent();
73 MachineFrameInfo &MFI = *MF.getFrameInfo();
74
75 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +000076 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +000077 MachineMemOperand::MOLoad,
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000078 MFI.getObjectSize(FrameIdx),
79 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000080
81 if (RC == &MSP430::GR16RegClass)
82 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
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 if (RC == &MSP430::GR8RegClass)
85 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000086 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000087 else
Torok Edwinfbcc6632009-07-14 16:55:14 +000088 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000089}
90
Jakob Stoklund Olesen65306362010-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 Korobeynikov80a73e72009-05-03 13:05:42 +0000102
Jakob Stoklund Olesen65306362010-07-11 06:53:30 +0000103 BuildMI(MBB, I, DL, get(Opc), DestReg)
104 .addReg(SrcReg, getKillRegState(KillSrc));
Anton Korobeynikovd7afd692009-05-03 13:02:04 +0000105}
106
Anton Korobeynikov5399c2d2009-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 Johannesen4244d122010-04-02 01:38:09 +0000113 if (I->isDebugValue())
114 continue;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000115 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000116 I->getOpcode() != MSP430::JCC &&
117 I->getOpcode() != MSP430::Br &&
118 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov5399c2d2009-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 Korobeynikov5399c2d2009-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) {
Craig Toppere55c5562012-02-07 02:50:20 +0000136 default: llvm_unreachable("Invalid branch condition!");
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000137 case MSP430CC::COND_E:
138 CC = MSP430CC::COND_NE;
139 break;
140 case MSP430CC::COND_NE:
141 CC = MSP430CC::COND_E;
142 break;
143 case MSP430CC::COND_L:
144 CC = MSP430CC::COND_GE;
145 break;
146 case MSP430CC::COND_GE:
147 CC = MSP430CC::COND_L;
148 break;
149 case MSP430CC::COND_HS:
150 CC = MSP430CC::COND_LO;
151 break;
152 case MSP430CC::COND_LO:
153 CC = MSP430CC::COND_HS;
154 break;
155 }
156
157 Cond[0].setImm(CC);
158 return false;
159}
160
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000161bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000162 if (!MI->isTerminator()) return false;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000163
164 // Conditional branch is a special case.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000165 if (MI->isBranch() && !MI->isBarrier())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000166 return true;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000167 if (!MI->isPredicable())
Anton Korobeynikov5399c2d2009-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 Johannesen4244d122010-04-02 01:38:09 +0000182 if (I->isDebugValue())
183 continue;
184
Anton Korobeynikov5399c2d2009-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.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000192 if (!I->isBranch())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000193 return true;
194
Anton Korobeynikovebbdfef2010-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 Korobeynikov5399c2d2009-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.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000208 while (std::next(I) != MBB.end())
209 std::next(I)->eraseFromParent();
Anton Korobeynikov5399c2d2009-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 Korobeynikov41917df2009-05-03 13:15:22 +0000262unsigned
263MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
264 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000265 const SmallVectorImpl<MachineOperand> &Cond,
266 DebugLoc DL) const {
Anton Korobeynikov41917df2009-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 Lattner6f306d72010-04-02 20:16:16 +0000275 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000276 return 1;
277 }
278
279 // Conditional branch.
280 unsigned Count = 0;
Chris Lattner6f306d72010-04-02 20:16:16 +0000281 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000282 ++Count;
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000283
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000284 if (FBB) {
285 // Two-way Conditional branch. Insert the second branch.
Chris Lattner6f306d72010-04-02 20:16:16 +0000286 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000287 ++Count;
288 }
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000289 return Count;
290}
Anton Korobeynikovce52fd52010-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 Cheng6cc775f2011-06-28 19:10:37 +0000296 const MCInstrDesc &Desc = MI->getDesc();
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000297
298 switch (Desc.TSFlags & MSP430II::SizeMask) {
299 default:
300 switch (Desc.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000301 default: llvm_unreachable("Unknown instruction size!");
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000302 case TargetOpcode::CFI_INSTRUCTION:
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()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000317 default: llvm_unreachable("Unknown instruction size!");
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000318 case MSP430::SAR8r1c:
319 case MSP430::SAR16r1c:
320 return 4;
321 }
322 case MSP430II::Size2Bytes:
323 return 2;
324 case MSP430II::Size4Bytes:
325 return 4;
326 case MSP430II::Size6Bytes:
327 return 6;
328 }
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000329}