blob: 5eee11ab0fac99007826b9d008c9608a17644466 [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
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000159bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
160 if (!MI.isTerminator())
161 return false;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000162
163 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000164 if (MI.isBranch() && !MI.isBarrier())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000165 return true;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000166 if (!MI.isPredicable())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000167 return true;
168 return !isPredicated(MI);
169}
170
171bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
172 MachineBasicBlock *&TBB,
173 MachineBasicBlock *&FBB,
174 SmallVectorImpl<MachineOperand> &Cond,
175 bool AllowModify) const {
176 // Start from the bottom of the block and work up, examining the
177 // terminator instructions.
178 MachineBasicBlock::iterator I = MBB.end();
179 while (I != MBB.begin()) {
180 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000181 if (I->isDebugValue())
182 continue;
183
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000184 // Working from the bottom, when we see a non-terminator
185 // instruction, we're done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000186 if (!isUnpredicatedTerminator(*I))
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000187 break;
188
189 // A terminator that isn't a branch can't easily be handled
190 // by this analysis.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000191 if (!I->isBranch())
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000192 return true;
193
Anton Korobeynikovebbdfef2010-05-01 12:04:32 +0000194 // Cannot handle indirect branches.
195 if (I->getOpcode() == MSP430::Br ||
196 I->getOpcode() == MSP430::Bm)
197 return true;
198
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000199 // Handle unconditional branches.
200 if (I->getOpcode() == MSP430::JMP) {
201 if (!AllowModify) {
202 TBB = I->getOperand(0).getMBB();
203 continue;
204 }
205
206 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000207 while (std::next(I) != MBB.end())
208 std::next(I)->eraseFromParent();
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000209 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000210 FBB = nullptr;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000211
212 // Delete the JMP if it's equivalent to a fall-through.
213 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000214 TBB = nullptr;
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000215 I->eraseFromParent();
216 I = MBB.end();
217 continue;
218 }
219
220 // TBB is used to indicate the unconditinal destination.
221 TBB = I->getOperand(0).getMBB();
222 continue;
223 }
224
225 // Handle conditional branches.
226 assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
227 MSP430CC::CondCodes BranchCode =
228 static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
229 if (BranchCode == MSP430CC::COND_INVALID)
230 return true; // Can't handle weird stuff.
231
232 // Working from the bottom, handle the first conditional branch.
233 if (Cond.empty()) {
234 FBB = TBB;
235 TBB = I->getOperand(0).getMBB();
236 Cond.push_back(MachineOperand::CreateImm(BranchCode));
237 continue;
238 }
239
240 // Handle subsequent conditional branches. Only handle the case where all
241 // conditional branches branch to the same destination.
242 assert(Cond.size() == 1);
243 assert(TBB);
244
245 // Only handle the case where all conditional branches branch to
246 // the same destination.
247 if (TBB != I->getOperand(0).getMBB())
248 return true;
249
250 MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
251 // If the conditions are the same, we can leave them alone.
252 if (OldBranchCode == BranchCode)
253 continue;
254
255 return true;
256 }
257
258 return false;
259}
260
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000261unsigned
262MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
263 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000264 ArrayRef<MachineOperand> Cond,
Stuart Hastings0125b642010-06-17 22:43:56 +0000265 DebugLoc DL) const {
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000266 // Shouldn't be a fall through.
267 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
268 assert((Cond.size() == 1 || Cond.size() == 0) &&
269 "MSP430 branch conditions have one component!");
270
271 if (Cond.empty()) {
272 // Unconditional branch?
273 assert(!FBB && "Unconditional branch with multiple successors!");
Chris Lattner6f306d72010-04-02 20:16:16 +0000274 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000275 return 1;
276 }
277
278 // Conditional branch.
279 unsigned Count = 0;
Chris Lattner6f306d72010-04-02 20:16:16 +0000280 BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000281 ++Count;
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000282
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000283 if (FBB) {
284 // Two-way Conditional branch. Insert the second branch.
Chris Lattner6f306d72010-04-02 20:16:16 +0000285 BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
Anton Korobeynikov5399c2d2009-10-21 19:17:18 +0000286 ++Count;
287 }
Anton Korobeynikov41917df2009-05-03 13:15:22 +0000288 return Count;
289}
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000290
291/// GetInstSize - Return the number of bytes of code the specified
292/// instruction may be. This returns the maximum number of bytes.
293///
294unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000295 const MCInstrDesc &Desc = MI->getDesc();
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000296
297 switch (Desc.TSFlags & MSP430II::SizeMask) {
298 default:
299 switch (Desc.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000300 default: llvm_unreachable("Unknown instruction size!");
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000301 case TargetOpcode::CFI_INSTRUCTION:
Chris Lattnerb06015a2010-02-09 19:54:29 +0000302 case TargetOpcode::EH_LABEL:
303 case TargetOpcode::IMPLICIT_DEF:
304 case TargetOpcode::KILL:
Dale Johannesen60b28972010-04-07 19:51:44 +0000305 case TargetOpcode::DBG_VALUE:
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000306 return 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000307 case TargetOpcode::INLINEASM: {
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000308 const MachineFunction *MF = MI->getParent()->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000309 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000310 return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
311 *MF->getTarget().getMCAsmInfo());
312 }
313 }
314 case MSP430II::SizeSpecial:
315 switch (MI->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000316 default: llvm_unreachable("Unknown instruction size!");
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000317 case MSP430::SAR8r1c:
318 case MSP430::SAR16r1c:
319 return 4;
320 }
321 case MSP430II::Size2Bytes:
322 return 2;
323 case MSP430II::Size4Bytes:
324 return 4;
325 case MSP430II::Size6Bytes:
326 return 6;
327 }
Anton Korobeynikovce52fd52010-01-15 21:19:05 +0000328}