blob: 6135ce08092092b0430212dc0a2027d608c3dc28 [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();
Matthias Braun941a7052016-07-28 18:40:00 +000045 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000046
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();
Matthias Braun941a7052016-07-28 18:40:00 +000072 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikove61e0b92009-11-07 17:13:57 +000073
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 Korobeynikov064dbac2016-02-24 15:15:02 +000081 .addReg(DestReg, getDefRegState(true)).addFrameIndex(FrameIdx)
82 .addImm(0).addMemOperand(MMO);
Anton Korobeynikovf6af8222009-05-03 13:09:57 +000083 else if (RC == &MSP430::GR8RegClass)
84 BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
Anton Korobeynikov064dbac2016-02-24 15:15:02 +000085 .addReg(DestReg, getDefRegState(true)).addFrameIndex(FrameIdx)
86 .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,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000092 MachineBasicBlock::iterator I,
93 const DebugLoc &DL, unsigned DestReg,
94 unsigned SrcReg, bool KillSrc) const {
Jakob Stoklund Olesen65306362010-07-11 06:53:30 +000095 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
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000107unsigned MSP430InstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000108 int *BytesRemoved) const {
109 assert(!BytesRemoved && "code size not handled");
110
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000111 MachineBasicBlock::iterator I = MBB.end();
112 unsigned Count = 0;
113
114 while (I != MBB.begin()) {
115 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000116 if (I->isDebugValue())
117 continue;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000118 if (I->getOpcode() != MSP430::JMP &&
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000119 I->getOpcode() != MSP430::JCC &&
120 I->getOpcode() != MSP430::Br &&
121 I->getOpcode() != MSP430::Bm)
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000122 break;
123 // Remove the branch.
124 I->eraseFromParent();
125 I = MBB.end();
126 ++Count;
127 }
128
129 return Count;
130}
131
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000132bool MSP430InstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000133reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000134 assert(Cond.size() == 1 && "Invalid Xbranch condition!");
135
136 MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
137
138 switch (CC) {
Craig Toppere55c5562012-02-07 02:50:20 +0000139 default: llvm_unreachable("Invalid branch condition!");
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000140 case MSP430CC::COND_E:
141 CC = MSP430CC::COND_NE;
142 break;
143 case MSP430CC::COND_NE:
144 CC = MSP430CC::COND_E;
145 break;
146 case MSP430CC::COND_L:
147 CC = MSP430CC::COND_GE;
148 break;
149 case MSP430CC::COND_GE:
150 CC = MSP430CC::COND_L;
151 break;
152 case MSP430CC::COND_HS:
153 CC = MSP430CC::COND_LO;
154 break;
155 case MSP430CC::COND_LO:
156 CC = MSP430CC::COND_HS;
157 break;
158 }
159
160 Cond[0].setImm(CC);
161 return false;
162}
163
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000164bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
165 if (!MI.isTerminator())
166 return false;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000167
168 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000169 if (MI.isBranch() && !MI.isBarrier())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000170 return true;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000171 if (!MI.isPredicable())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000172 return true;
173 return !isPredicated(MI);
174}
175
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000176bool MSP430InstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000177 MachineBasicBlock *&TBB,
178 MachineBasicBlock *&FBB,
179 SmallVectorImpl<MachineOperand> &Cond,
180 bool AllowModify) const {
181 // Start from the bottom of the block and work up, examining the
182 // terminator instructions.
183 MachineBasicBlock::iterator I = MBB.end();
184 while (I != MBB.begin()) {
185 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000186 if (I->isDebugValue())
187 continue;
188
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000189 // Working from the bottom, when we see a non-terminator
190 // instruction, we're done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000191 if (!isUnpredicatedTerminator(*I))
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000192 break;
193
194 // A terminator that isn't a branch can't easily be handled
195 // by this analysis.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000196 if (!I->isBranch())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000197 return true;
198
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000199 // Cannot handle indirect branches.
200 if (I->getOpcode() == MSP430::Br ||
201 I->getOpcode() == MSP430::Bm)
202 return true;
203
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000204 // Handle unconditional branches.
205 if (I->getOpcode() == MSP430::JMP) {
206 if (!AllowModify) {
207 TBB = I->getOperand(0).getMBB();
208 continue;
209 }
210
211 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000212 while (std::next(I) != MBB.end())
213 std::next(I)->eraseFromParent();
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000214 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000215 FBB = nullptr;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000216
217 // Delete the JMP if it's equivalent to a fall-through.
218 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000219 TBB = nullptr;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000220 I->eraseFromParent();
221 I = MBB.end();
222 continue;
223 }
224
225 // TBB is used to indicate the unconditinal destination.
226 TBB = I->getOperand(0).getMBB();
227 continue;
228 }
229
230 // Handle conditional branches.
231 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
232 MSP430CC::CondCodes BranchCode =
233 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
234 if (BranchCode == MSP430CC::COND_INVALID)
235 return true; // Can't handle weird stuff.
236
237 // Working from the bottom, handle the first conditional branch.
238 if (Cond.empty()) {
239 FBB = TBB;
240 TBB = I->getOperand(0).getMBB();
241 Cond.push_back(MachineOperand::CreateImm(BranchCode));
242 continue;
243 }
244
245 // Handle subsequent conditional branches. Only handle the case where all
246 // conditional branches branch to the same destination.
247 assert(Cond.size() == 1);
248 assert(TBB);
249
250 // Only handle the case where all conditional branches branch to
251 // the same destination.
252 if (TBB != I->getOperand(0).getMBB())
253 return true;
254
255 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
256 // If the conditions are the same, we can leave them alone.
257 if (OldBranchCode == BranchCode)
258 continue;
259
260 return true;
261 }
262
263 return false;
264}
265
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000266unsigned MSP430InstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000267 MachineBasicBlock *TBB,
268 MachineBasicBlock *FBB,
269 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000270 const DebugLoc &DL,
271 int *BytesAdded) const {
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000272 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000273 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000274 assert((Cond.size() == 1 || Cond.size() == 0) &&
275 "MSP430 branch conditions have one component!");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000276 assert(!BytesAdded && "code size not handled");
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000277
278 if (Cond.empty()) {
279 // Unconditional branch?
280 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattner6f306d72010-04-02 20:16:16 +0000281 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000282 return 1;
283 }
284
285 // Conditional branch.
286 unsigned Count = 0;
Chris Lattner6f306d72010-04-02 20:16:16 +0000287 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000288 ++Count;
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000289
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000290 if (FBB) {
291 // Two-way Conditional branch. Insert the second branch.
Chris Lattner6f306d72010-04-02 20:16:16 +0000292 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000293 ++Count;
294 }
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000295 return Count;
296}
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000297
298/// GetInstSize - Return the number of bytes of code the specified
299/// instruction may be. This returns the maximum number of bytes.
300///
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000301unsigned MSP430InstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000302 const MCInstrDesc &Desc = MI.getDesc();
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000303
304 switch (Desc.TSFlags & MSP430II::SizeMask) {
305 default:
306 switch (Desc.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000307 default: llvm_unreachable("Unknown instruction size!");
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000308 case TargetOpcode::CFI_INSTRUCTION:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000309 case TargetOpcode::EH_LABEL:
310 case TargetOpcode::IMPLICIT_DEF:
311 case TargetOpcode::KILL:
Dale Johannesen60b28972010-04-07 19:51:44 +0000312 case TargetOpcode::DBG_VALUE:
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000313 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000314 case TargetOpcode::INLINEASM: {
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000315 const MachineFunction *MF = MI.getParent()->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000316 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000317 return TII.getInlineAsmLength(MI.getOperand(0).getSymbolName(),
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000318 *MF->getTarget().getMCAsmInfo());
319 }
320 }
321 case MSP430II::SizeSpecial:
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000322 switch (MI.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000323 default: llvm_unreachable("Unknown instruction size!");
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000324 case MSP430::SAR8r1c:
325 case MSP430::SAR16r1c:
326 return 4;
327 }
328 case MSP430II::Size2Bytes:
329 return 2;
330 case MSP430II::Size4Bytes:
331 return 4;
332 case MSP430II::Size6Bytes:
333 return 6;
334 }
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000335}