blob: 4fb82ad613d77bc0f75614a636aecd1434f7ec6f [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
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
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000161bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
162 if (!MI.isTerminator())
163 return false;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000164
165 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000166 if (MI.isBranch() && !MI.isBarrier())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000167 return true;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000168 if (!MI.isPredicable())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000169 return true;
170 return !isPredicated(MI);
171}
172
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000173bool MSP430InstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000174 MachineBasicBlock *&TBB,
175 MachineBasicBlock *&FBB,
176 SmallVectorImpl<MachineOperand> &Cond,
177 bool AllowModify) const {
178 // Start from the bottom of the block and work up, examining the
179 // terminator instructions.
180 MachineBasicBlock::iterator I = MBB.end();
181 while (I != MBB.begin()) {
182 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000183 if (I->isDebugValue())
184 continue;
185
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000186 // Working from the bottom, when we see a non-terminator
187 // instruction, we're done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000188 if (!isUnpredicatedTerminator(*I))
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000189 break;
190
191 // A terminator that isn't a branch can't easily be handled
192 // by this analysis.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000193 if (!I->isBranch())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000194 return true;
195
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000196 // Cannot handle indirect branches.
197 if (I->getOpcode() == MSP430::Br ||
198 I->getOpcode() == MSP430::Bm)
199 return true;
200
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000201 // Handle unconditional branches.
202 if (I->getOpcode() == MSP430::JMP) {
203 if (!AllowModify) {
204 TBB = I->getOperand(0).getMBB();
205 continue;
206 }
207
208 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000209 while (std::next(I) != MBB.end())
210 std::next(I)->eraseFromParent();
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000211 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000212 FBB = nullptr;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000213
214 // Delete the JMP if it's equivalent to a fall-through.
215 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000216 TBB = nullptr;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000217 I->eraseFromParent();
218 I = MBB.end();
219 continue;
220 }
221
222 // TBB is used to indicate the unconditinal destination.
223 TBB = I->getOperand(0).getMBB();
224 continue;
225 }
226
227 // Handle conditional branches.
228 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
229 MSP430CC::CondCodes BranchCode =
230 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
231 if (BranchCode == MSP430CC::COND_INVALID)
232 return true; // Can't handle weird stuff.
233
234 // Working from the bottom, handle the first conditional branch.
235 if (Cond.empty()) {
236 FBB = TBB;
237 TBB = I->getOperand(0).getMBB();
238 Cond.push_back(MachineOperand::CreateImm(BranchCode));
239 continue;
240 }
241
242 // Handle subsequent conditional branches. Only handle the case where all
243 // conditional branches branch to the same destination.
244 assert(Cond.size() == 1);
245 assert(TBB);
246
247 // Only handle the case where all conditional branches branch to
248 // the same destination.
249 if (TBB != I->getOperand(0).getMBB())
250 return true;
251
252 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
253 // If the conditions are the same, we can leave them alone.
254 if (OldBranchCode == BranchCode)
255 continue;
256
257 return true;
258 }
259
260 return false;
261}
262
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000263unsigned MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB,
264 MachineBasicBlock *TBB,
265 MachineBasicBlock *FBB,
266 ArrayRef<MachineOperand> Cond,
267 const DebugLoc &DL) const {
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000268 // Shouldn't be a fall through.
269 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
270 assert((Cond.size() == 1 || Cond.size() == 0) &&
271 "MSP430 branch conditions have one component!");
272
273 if (Cond.empty()) {
274 // Unconditional branch?
275 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattner6f306d72010-04-02 20:16:16 +0000276 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000277 return 1;
278 }
279
280 // Conditional branch.
281 unsigned Count = 0;
Chris Lattner6f306d72010-04-02 20:16:16 +0000282 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000283 ++Count;
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000284
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000285 if (FBB) {
286 // Two-way Conditional branch. Insert the second branch.
Chris Lattner6f306d72010-04-02 20:16:16 +0000287 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000288 ++Count;
289 }
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000290 return Count;
291}
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000292
293/// GetInstSize - Return the number of bytes of code the specified
294/// instruction may be. This returns the maximum number of bytes.
295///
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000296unsigned MSP430InstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000297 const MCInstrDesc &Desc = MI.getDesc();
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000298
299 switch (Desc.TSFlags & MSP430II::SizeMask) {
300 default:
301 switch (Desc.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000302 default: llvm_unreachable("Unknown instruction size!");
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000303 case TargetOpcode::CFI_INSTRUCTION:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000304 case TargetOpcode::EH_LABEL:
305 case TargetOpcode::IMPLICIT_DEF:
306 case TargetOpcode::KILL:
Dale Johannesen60b28972010-04-07 19:51:44 +0000307 case TargetOpcode::DBG_VALUE:
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000308 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000309 case TargetOpcode::INLINEASM: {
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000310 const MachineFunction *MF = MI.getParent()->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000311 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000312 return TII.getInlineAsmLength(MI.getOperand(0).getSymbolName(),
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000313 *MF->getTarget().getMCAsmInfo());
314 }
315 }
316 case MSP430II::SizeSpecial:
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000317 switch (MI.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000318 default: llvm_unreachable("Unknown instruction size!");
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000319 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 }
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000330}