blob: d4f82bda1ec9f565399736cb945f046864d687ca [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
Chandler Carruthd174b722014-04-22 02:03:14 +000025using namespace llvm;
26
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000027#define GET_INSTRINFO_CTOR_DTOR
Evan Cheng1e210d02011-06-28 20:07:07 +000028#include "MSP430GenInstrInfo.inc"
29
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000030// Pin the vtable to this file.
31void MSP430InstrInfo::anchor() {}
32
Eric Christopher72a5b2a2014-06-27 01:14:50 +000033MSP430InstrInfo::MSP430InstrInfo(MSP430Subtarget &STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000034 : MSP430GenInstrInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
Eric Christopher72a5b2a2014-06-27 01:14:50 +000035 RI() {}
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
Alex Lorenze40c8a22015-08-11 23:09:45 +000047 MachineMemOperand *MMO = MF.getMachineMemOperand(
48 MachinePointerInfo::getFixedStack(MF, FrameIdx),
49 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
50 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000051
52 if (RC == &MSP430::GR16RegClass)
53 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
54 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000055 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000056 else if (RC == &MSP430::GR8RegClass)
57 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
58 .addFrameIndex(FrameIdx).addImm(0)
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000059 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000060 else
Torok Edwinfbcc6632009-07-14 16:55:14 +000061 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000062}
63
64void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
65 MachineBasicBlock::iterator MI,
66 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +000067 const TargetRegisterClass *RC,
68 const TargetRegisterInfo *TRI) const{
Chris Lattner6f306d72010-04-02 20:16:16 +000069 DebugLoc DL;
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000070 if (MI != MBB.end()) DL = MI->getDebugLoc();
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000071 MachineFunction &MF = *MBB.getParent();
72 MachineFrameInfo &MFI = *MF.getFrameInfo();
73
Alex Lorenze40c8a22015-08-11 23:09:45 +000074 MachineMemOperand *MMO = MF.getMachineMemOperand(
75 MachinePointerInfo::getFixedStack(MF, FrameIdx),
76 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
77 MFI.getObjectAlignment(FrameIdx));
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000078
79 if (RC == &MSP430::GR16RegClass)
80 BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000081 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000082 else if (RC == &MSP430::GR8RegClass)
83 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000084 .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000085 else
Torok Edwinfbcc6632009-07-14 16:55:14 +000086 llvm_unreachable("Cannot store this register to stack slot!");
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000087}
88
Jakob Stoklund Olesen65306362010-07-11 06:53:30 +000089void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
90 MachineBasicBlock::iterator I, DebugLoc DL,
91 unsigned DestReg, unsigned SrcReg,
92 bool KillSrc) const {
93 unsigned Opc;
94 if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
95 Opc = MSP430::MOV16rr;
96 else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
97 Opc = MSP430::MOV8rr;
98 else
99 llvm_unreachable("Impossible reg-to-reg copy");
Anton Korobeynikov80a73e72009-05-03 13:05:42 +0000100
Jakob Stoklund Olesen65306362010-07-11 06:53:30 +0000101 BuildMI(MBB, I, DL, get(Opc), DestReg)
102 .addReg(SrcReg, getKillRegState(KillSrc));
Anton Korobeynikovd7afd692009-05-03 13:02:04 +0000103}
104
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000105unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
106 MachineBasicBlock::iterator I = MBB.end();
107 unsigned Count = 0;
108
109 while (I != MBB.begin()) {
110 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000111 if (I->isDebugValue())
112 continue;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000113 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000114 I->getOpcode() != MSP430::JCC &&
115 I->getOpcode() != MSP430::Br &&
116 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000117 break;
118 // Remove the branch.
119 I->eraseFromParent();
120 I = MBB.end();
121 ++Count;
122 }
123
124 return Count;
125}
126
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000127bool MSP430InstrInfo::
128ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
129 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
130
131 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
132
133 switch (CC) {
Craig Toppere55c5562012-02-07 02:50:20 +0000134 default: llvm_unreachable("Invalid branch condition!");
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000135 case MSP430CC::COND_E:
136 CC = MSP430CC::COND_NE;
137 break;
138 case MSP430CC::COND_NE:
139 CC = MSP430CC::COND_E;
140 break;
141 case MSP430CC::COND_L:
142 CC = MSP430CC::COND_GE;
143 break;
144 case MSP430CC::COND_GE:
145 CC = MSP430CC::COND_L;
146 break;
147 case MSP430CC::COND_HS:
148 CC = MSP430CC::COND_LO;
149 break;
150 case MSP430CC::COND_LO:
151 CC = MSP430CC::COND_HS;
152 break;
153 }
154
155 Cond[0].setImm(CC);
156 return false;
157}
158
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000159bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000160 if (!MI->isTerminator()) return false;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000161
162 // Conditional branch is a special case.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000163 if (MI->isBranch() && !MI->isBarrier())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000164 return true;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000165 if (!MI->isPredicable())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000166 return true;
167 return !isPredicated(MI);
168}
169
170bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
171 MachineBasicBlock *&TBB,
172 MachineBasicBlock *&FBB,
173 SmallVectorImpl<MachineOperand> &Cond,
174 bool AllowModify) const {
175 // Start from the bottom of the block and work up, examining the
176 // terminator instructions.
177 MachineBasicBlock::iterator I = MBB.end();
178 while (I != MBB.begin()) {
179 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000180 if (I->isDebugValue())
181 continue;
182
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000183 // Working from the bottom, when we see a non-terminator
184 // instruction, we're done.
185 if (!isUnpredicatedTerminator(I))
186 break;
187
188 // A terminator that isn't a branch can't easily be handled
189 // by this analysis.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000190 if (!I->isBranch())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000191 return true;
192
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000193 // Cannot handle indirect branches.
194 if (I->getOpcode() == MSP430::Br ||
195 I->getOpcode() == MSP430::Bm)
196 return true;
197
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000198 // Handle unconditional branches.
199 if (I->getOpcode() == MSP430::JMP) {
200 if (!AllowModify) {
201 TBB = I->getOperand(0).getMBB();
202 continue;
203 }
204
205 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000206 while (std::next(I) != MBB.end())
207 std::next(I)->eraseFromParent();
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000208 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000209 FBB = nullptr;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000210
211 // Delete the JMP if it's equivalent to a fall-through.
212 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000213 TBB = nullptr;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000214 I->eraseFromParent();
215 I = MBB.end();
216 continue;
217 }
218
219 // TBB is used to indicate the unconditinal destination.
220 TBB = I->getOperand(0).getMBB();
221 continue;
222 }
223
224 // Handle conditional branches.
225 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
226 MSP430CC::CondCodes BranchCode =
227 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
228 if (BranchCode == MSP430CC::COND_INVALID)
229 return true; // Can't handle weird stuff.
230
231 // Working from the bottom, handle the first conditional branch.
232 if (Cond.empty()) {
233 FBB = TBB;
234 TBB = I->getOperand(0).getMBB();
235 Cond.push_back(MachineOperand::CreateImm(BranchCode));
236 continue;
237 }
238
239 // Handle subsequent conditional branches. Only handle the case where all
240 // conditional branches branch to the same destination.
241 assert(Cond.size() == 1);
242 assert(TBB);
243
244 // Only handle the case where all conditional branches branch to
245 // the same destination.
246 if (TBB != I->getOperand(0).getMBB())
247 return true;
248
249 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
250 // If the conditions are the same, we can leave them alone.
251 if (OldBranchCode == BranchCode)
252 continue;
253
254 return true;
255 }
256
257 return false;
258}
259
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000260unsigned
261MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
262 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000263 ArrayRef<MachineOperand> Cond,
Stuart Hastings0125b642010-06-17 22:43:56 +0000264 DebugLoc DL) const {
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000265 // Shouldn't be a fall through.
266 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
267 assert((Cond.size() == 1 || Cond.size() == 0) &&
268 "MSP430 branch conditions have one component!");
269
270 if (Cond.empty()) {
271 // Unconditional branch?
272 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattner6f306d72010-04-02 20:16:16 +0000273 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000274 return 1;
275 }
276
277 // Conditional branch.
278 unsigned Count = 0;
Chris Lattner6f306d72010-04-02 20:16:16 +0000279 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000280 ++Count;
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000281
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000282 if (FBB) {
283 // Two-way Conditional branch. Insert the second branch.
Chris Lattner6f306d72010-04-02 20:16:16 +0000284 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000285 ++Count;
286 }
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000287 return Count;
288}
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000289
290/// GetInstSize - Return the number of bytes of code the specified
291/// instruction may be. This returns the maximum number of bytes.
292///
293unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000294 const MCInstrDesc &Desc = MI->getDesc();
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000295
296 switch (Desc.TSFlags & MSP430II::SizeMask) {
297 default:
298 switch (Desc.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000299 default: llvm_unreachable("Unknown instruction size!");
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000300 case TargetOpcode::CFI_INSTRUCTION:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000301 case TargetOpcode::EH_LABEL:
302 case TargetOpcode::IMPLICIT_DEF:
303 case TargetOpcode::KILL:
Dale Johannesen60b28972010-04-07 19:51:44 +0000304 case TargetOpcode::DBG_VALUE:
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000305 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000306 case TargetOpcode::INLINEASM: {
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000307 const MachineFunction *MF = MI->getParent()->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000308 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000309 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
310 *MF->getTarget().getMCAsmInfo());
311 }
312 }
313 case MSP430II::SizeSpecial:
314 switch (MI->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000315 default: llvm_unreachable("Unknown instruction size!");
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000316 case MSP430::SAR8r1c:
317 case MSP430::SAR16r1c:
318 return 4;
319 }
320 case MSP430II::Size2Bytes:
321 return 2;
322 case MSP430II::Size4Bytes:
323 return 4;
324 case MSP430II::Size6Bytes:
325 return 6;
326 }
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000327}