1*9880d681SAndroid Build Coastguard Worker //===-- MSP430InstrInfo.cpp - MSP430 Instruction Information --------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file contains the MSP430 implementation of the TargetInstrInfo class.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "MSP430InstrInfo.h"
15*9880d681SAndroid Build Coastguard Worker #include "MSP430.h"
16*9880d681SAndroid Build Coastguard Worker #include "MSP430MachineFunctionInfo.h"
17*9880d681SAndroid Build Coastguard Worker #include "MSP430TargetMachine.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TargetRegistry.h"
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker using namespace llvm;
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker #define GET_INSTRINFO_CTOR_DTOR
28*9880d681SAndroid Build Coastguard Worker #include "MSP430GenInstrInfo.inc"
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker // Pin the vtable to this file.
anchor()31*9880d681SAndroid Build Coastguard Worker void MSP430InstrInfo::anchor() {}
32*9880d681SAndroid Build Coastguard Worker
MSP430InstrInfo(MSP430Subtarget & STI)33*9880d681SAndroid Build Coastguard Worker MSP430InstrInfo::MSP430InstrInfo(MSP430Subtarget &STI)
34*9880d681SAndroid Build Coastguard Worker : MSP430GenInstrInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
35*9880d681SAndroid Build Coastguard Worker RI() {}
36*9880d681SAndroid Build Coastguard Worker
storeRegToStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned SrcReg,bool isKill,int FrameIdx,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI) const37*9880d681SAndroid Build Coastguard Worker void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
38*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MI,
39*9880d681SAndroid Build Coastguard Worker unsigned SrcReg, bool isKill, int FrameIdx,
40*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *RC,
41*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) const {
42*9880d681SAndroid Build Coastguard Worker DebugLoc DL;
43*9880d681SAndroid Build Coastguard Worker if (MI != MBB.end()) DL = MI->getDebugLoc();
44*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = *MBB.getParent();
45*9880d681SAndroid Build Coastguard Worker MachineFrameInfo &MFI = *MF.getFrameInfo();
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker MachineMemOperand *MMO = MF.getMachineMemOperand(
48*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getFixedStack(MF, FrameIdx),
49*9880d681SAndroid Build Coastguard Worker MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
50*9880d681SAndroid Build Coastguard Worker MFI.getObjectAlignment(FrameIdx));
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker if (RC == &MSP430::GR16RegClass)
53*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
54*9880d681SAndroid Build Coastguard Worker .addFrameIndex(FrameIdx).addImm(0)
55*9880d681SAndroid Build Coastguard Worker .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
56*9880d681SAndroid Build Coastguard Worker else if (RC == &MSP430::GR8RegClass)
57*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
58*9880d681SAndroid Build Coastguard Worker .addFrameIndex(FrameIdx).addImm(0)
59*9880d681SAndroid Build Coastguard Worker .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
60*9880d681SAndroid Build Coastguard Worker else
61*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Cannot store this register to stack slot!");
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker
loadRegFromStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned DestReg,int FrameIdx,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI) const64*9880d681SAndroid Build Coastguard Worker void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
65*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MI,
66*9880d681SAndroid Build Coastguard Worker unsigned DestReg, int FrameIdx,
67*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *RC,
68*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) const{
69*9880d681SAndroid Build Coastguard Worker DebugLoc DL;
70*9880d681SAndroid Build Coastguard Worker if (MI != MBB.end()) DL = MI->getDebugLoc();
71*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = *MBB.getParent();
72*9880d681SAndroid Build Coastguard Worker MachineFrameInfo &MFI = *MF.getFrameInfo();
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker MachineMemOperand *MMO = MF.getMachineMemOperand(
75*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getFixedStack(MF, FrameIdx),
76*9880d681SAndroid Build Coastguard Worker MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
77*9880d681SAndroid Build Coastguard Worker MFI.getObjectAlignment(FrameIdx));
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker if (RC == &MSP430::GR16RegClass)
80*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
81*9880d681SAndroid Build Coastguard Worker .addReg(DestReg, getDefRegState(true)).addFrameIndex(FrameIdx)
82*9880d681SAndroid Build Coastguard Worker .addImm(0).addMemOperand(MMO);
83*9880d681SAndroid Build Coastguard Worker else if (RC == &MSP430::GR8RegClass)
84*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
85*9880d681SAndroid Build Coastguard Worker .addReg(DestReg, getDefRegState(true)).addFrameIndex(FrameIdx)
86*9880d681SAndroid Build Coastguard Worker .addImm(0).addMemOperand(MMO);
87*9880d681SAndroid Build Coastguard Worker else
88*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Cannot store this register to stack slot!");
89*9880d681SAndroid Build Coastguard Worker }
90*9880d681SAndroid Build Coastguard Worker
copyPhysReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,const DebugLoc & DL,unsigned DestReg,unsigned SrcReg,bool KillSrc) const91*9880d681SAndroid Build Coastguard Worker void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
92*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I,
93*9880d681SAndroid Build Coastguard Worker const DebugLoc &DL, unsigned DestReg,
94*9880d681SAndroid Build Coastguard Worker unsigned SrcReg, bool KillSrc) const {
95*9880d681SAndroid Build Coastguard Worker unsigned Opc;
96*9880d681SAndroid Build Coastguard Worker if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
97*9880d681SAndroid Build Coastguard Worker Opc = MSP430::MOV16rr;
98*9880d681SAndroid Build Coastguard Worker else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
99*9880d681SAndroid Build Coastguard Worker Opc = MSP430::MOV8rr;
100*9880d681SAndroid Build Coastguard Worker else
101*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Impossible reg-to-reg copy");
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, I, DL, get(Opc), DestReg)
104*9880d681SAndroid Build Coastguard Worker .addReg(SrcReg, getKillRegState(KillSrc));
105*9880d681SAndroid Build Coastguard Worker }
106*9880d681SAndroid Build Coastguard Worker
RemoveBranch(MachineBasicBlock & MBB) const107*9880d681SAndroid Build Coastguard Worker unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
108*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I = MBB.end();
109*9880d681SAndroid Build Coastguard Worker unsigned Count = 0;
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker while (I != MBB.begin()) {
112*9880d681SAndroid Build Coastguard Worker --I;
113*9880d681SAndroid Build Coastguard Worker if (I->isDebugValue())
114*9880d681SAndroid Build Coastguard Worker continue;
115*9880d681SAndroid Build Coastguard Worker if (I->getOpcode() != MSP430::JMP &&
116*9880d681SAndroid Build Coastguard Worker I->getOpcode() != MSP430::JCC &&
117*9880d681SAndroid Build Coastguard Worker I->getOpcode() != MSP430::Br &&
118*9880d681SAndroid Build Coastguard Worker I->getOpcode() != MSP430::Bm)
119*9880d681SAndroid Build Coastguard Worker break;
120*9880d681SAndroid Build Coastguard Worker // Remove the branch.
121*9880d681SAndroid Build Coastguard Worker I->eraseFromParent();
122*9880d681SAndroid Build Coastguard Worker I = MBB.end();
123*9880d681SAndroid Build Coastguard Worker ++Count;
124*9880d681SAndroid Build Coastguard Worker }
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker return Count;
127*9880d681SAndroid Build Coastguard Worker }
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker bool MSP430InstrInfo::
ReverseBranchCondition(SmallVectorImpl<MachineOperand> & Cond) const130*9880d681SAndroid Build Coastguard Worker ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
131*9880d681SAndroid Build Coastguard Worker assert(Cond.size() == 1 && "Invalid Xbranch condition!");
132*9880d681SAndroid Build Coastguard Worker
133*9880d681SAndroid Build Coastguard Worker MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker switch (CC) {
136*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Invalid branch condition!");
137*9880d681SAndroid Build Coastguard Worker case MSP430CC::COND_E:
138*9880d681SAndroid Build Coastguard Worker CC = MSP430CC::COND_NE;
139*9880d681SAndroid Build Coastguard Worker break;
140*9880d681SAndroid Build Coastguard Worker case MSP430CC::COND_NE:
141*9880d681SAndroid Build Coastguard Worker CC = MSP430CC::COND_E;
142*9880d681SAndroid Build Coastguard Worker break;
143*9880d681SAndroid Build Coastguard Worker case MSP430CC::COND_L:
144*9880d681SAndroid Build Coastguard Worker CC = MSP430CC::COND_GE;
145*9880d681SAndroid Build Coastguard Worker break;
146*9880d681SAndroid Build Coastguard Worker case MSP430CC::COND_GE:
147*9880d681SAndroid Build Coastguard Worker CC = MSP430CC::COND_L;
148*9880d681SAndroid Build Coastguard Worker break;
149*9880d681SAndroid Build Coastguard Worker case MSP430CC::COND_HS:
150*9880d681SAndroid Build Coastguard Worker CC = MSP430CC::COND_LO;
151*9880d681SAndroid Build Coastguard Worker break;
152*9880d681SAndroid Build Coastguard Worker case MSP430CC::COND_LO:
153*9880d681SAndroid Build Coastguard Worker CC = MSP430CC::COND_HS;
154*9880d681SAndroid Build Coastguard Worker break;
155*9880d681SAndroid Build Coastguard Worker }
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker Cond[0].setImm(CC);
158*9880d681SAndroid Build Coastguard Worker return false;
159*9880d681SAndroid Build Coastguard Worker }
160*9880d681SAndroid Build Coastguard Worker
isUnpredicatedTerminator(const MachineInstr & MI) const161*9880d681SAndroid Build Coastguard Worker bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
162*9880d681SAndroid Build Coastguard Worker if (!MI.isTerminator())
163*9880d681SAndroid Build Coastguard Worker return false;
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker // Conditional branch is a special case.
166*9880d681SAndroid Build Coastguard Worker if (MI.isBranch() && !MI.isBarrier())
167*9880d681SAndroid Build Coastguard Worker return true;
168*9880d681SAndroid Build Coastguard Worker if (!MI.isPredicable())
169*9880d681SAndroid Build Coastguard Worker return true;
170*9880d681SAndroid Build Coastguard Worker return !isPredicated(MI);
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker
analyzeBranch(MachineBasicBlock & MBB,MachineBasicBlock * & TBB,MachineBasicBlock * & FBB,SmallVectorImpl<MachineOperand> & Cond,bool AllowModify) const173*9880d681SAndroid Build Coastguard Worker bool MSP430InstrInfo::analyzeBranch(MachineBasicBlock &MBB,
174*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *&TBB,
175*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *&FBB,
176*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineOperand> &Cond,
177*9880d681SAndroid Build Coastguard Worker bool AllowModify) const {
178*9880d681SAndroid Build Coastguard Worker // Start from the bottom of the block and work up, examining the
179*9880d681SAndroid Build Coastguard Worker // terminator instructions.
180*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I = MBB.end();
181*9880d681SAndroid Build Coastguard Worker while (I != MBB.begin()) {
182*9880d681SAndroid Build Coastguard Worker --I;
183*9880d681SAndroid Build Coastguard Worker if (I->isDebugValue())
184*9880d681SAndroid Build Coastguard Worker continue;
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard Worker // Working from the bottom, when we see a non-terminator
187*9880d681SAndroid Build Coastguard Worker // instruction, we're done.
188*9880d681SAndroid Build Coastguard Worker if (!isUnpredicatedTerminator(*I))
189*9880d681SAndroid Build Coastguard Worker break;
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Worker // A terminator that isn't a branch can't easily be handled
192*9880d681SAndroid Build Coastguard Worker // by this analysis.
193*9880d681SAndroid Build Coastguard Worker if (!I->isBranch())
194*9880d681SAndroid Build Coastguard Worker return true;
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker // Cannot handle indirect branches.
197*9880d681SAndroid Build Coastguard Worker if (I->getOpcode() == MSP430::Br ||
198*9880d681SAndroid Build Coastguard Worker I->getOpcode() == MSP430::Bm)
199*9880d681SAndroid Build Coastguard Worker return true;
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker // Handle unconditional branches.
202*9880d681SAndroid Build Coastguard Worker if (I->getOpcode() == MSP430::JMP) {
203*9880d681SAndroid Build Coastguard Worker if (!AllowModify) {
204*9880d681SAndroid Build Coastguard Worker TBB = I->getOperand(0).getMBB();
205*9880d681SAndroid Build Coastguard Worker continue;
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker // If the block has any instructions after a JMP, delete them.
209*9880d681SAndroid Build Coastguard Worker while (std::next(I) != MBB.end())
210*9880d681SAndroid Build Coastguard Worker std::next(I)->eraseFromParent();
211*9880d681SAndroid Build Coastguard Worker Cond.clear();
212*9880d681SAndroid Build Coastguard Worker FBB = nullptr;
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker // Delete the JMP if it's equivalent to a fall-through.
215*9880d681SAndroid Build Coastguard Worker if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
216*9880d681SAndroid Build Coastguard Worker TBB = nullptr;
217*9880d681SAndroid Build Coastguard Worker I->eraseFromParent();
218*9880d681SAndroid Build Coastguard Worker I = MBB.end();
219*9880d681SAndroid Build Coastguard Worker continue;
220*9880d681SAndroid Build Coastguard Worker }
221*9880d681SAndroid Build Coastguard Worker
222*9880d681SAndroid Build Coastguard Worker // TBB is used to indicate the unconditinal destination.
223*9880d681SAndroid Build Coastguard Worker TBB = I->getOperand(0).getMBB();
224*9880d681SAndroid Build Coastguard Worker continue;
225*9880d681SAndroid Build Coastguard Worker }
226*9880d681SAndroid Build Coastguard Worker
227*9880d681SAndroid Build Coastguard Worker // Handle conditional branches.
228*9880d681SAndroid Build Coastguard Worker assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
229*9880d681SAndroid Build Coastguard Worker MSP430CC::CondCodes BranchCode =
230*9880d681SAndroid Build Coastguard Worker static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
231*9880d681SAndroid Build Coastguard Worker if (BranchCode == MSP430CC::COND_INVALID)
232*9880d681SAndroid Build Coastguard Worker return true; // Can't handle weird stuff.
233*9880d681SAndroid Build Coastguard Worker
234*9880d681SAndroid Build Coastguard Worker // Working from the bottom, handle the first conditional branch.
235*9880d681SAndroid Build Coastguard Worker if (Cond.empty()) {
236*9880d681SAndroid Build Coastguard Worker FBB = TBB;
237*9880d681SAndroid Build Coastguard Worker TBB = I->getOperand(0).getMBB();
238*9880d681SAndroid Build Coastguard Worker Cond.push_back(MachineOperand::CreateImm(BranchCode));
239*9880d681SAndroid Build Coastguard Worker continue;
240*9880d681SAndroid Build Coastguard Worker }
241*9880d681SAndroid Build Coastguard Worker
242*9880d681SAndroid Build Coastguard Worker // Handle subsequent conditional branches. Only handle the case where all
243*9880d681SAndroid Build Coastguard Worker // conditional branches branch to the same destination.
244*9880d681SAndroid Build Coastguard Worker assert(Cond.size() == 1);
245*9880d681SAndroid Build Coastguard Worker assert(TBB);
246*9880d681SAndroid Build Coastguard Worker
247*9880d681SAndroid Build Coastguard Worker // Only handle the case where all conditional branches branch to
248*9880d681SAndroid Build Coastguard Worker // the same destination.
249*9880d681SAndroid Build Coastguard Worker if (TBB != I->getOperand(0).getMBB())
250*9880d681SAndroid Build Coastguard Worker return true;
251*9880d681SAndroid Build Coastguard Worker
252*9880d681SAndroid Build Coastguard Worker MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
253*9880d681SAndroid Build Coastguard Worker // If the conditions are the same, we can leave them alone.
254*9880d681SAndroid Build Coastguard Worker if (OldBranchCode == BranchCode)
255*9880d681SAndroid Build Coastguard Worker continue;
256*9880d681SAndroid Build Coastguard Worker
257*9880d681SAndroid Build Coastguard Worker return true;
258*9880d681SAndroid Build Coastguard Worker }
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker return false;
261*9880d681SAndroid Build Coastguard Worker }
262*9880d681SAndroid Build Coastguard Worker
InsertBranch(MachineBasicBlock & MBB,MachineBasicBlock * TBB,MachineBasicBlock * FBB,ArrayRef<MachineOperand> Cond,const DebugLoc & DL) const263*9880d681SAndroid Build Coastguard Worker unsigned MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB,
264*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *TBB,
265*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *FBB,
266*9880d681SAndroid Build Coastguard Worker ArrayRef<MachineOperand> Cond,
267*9880d681SAndroid Build Coastguard Worker const DebugLoc &DL) const {
268*9880d681SAndroid Build Coastguard Worker // Shouldn't be a fall through.
269*9880d681SAndroid Build Coastguard Worker assert(TBB && "InsertBranch must not be told to insert a fallthrough");
270*9880d681SAndroid Build Coastguard Worker assert((Cond.size() == 1 || Cond.size() == 0) &&
271*9880d681SAndroid Build Coastguard Worker "MSP430 branch conditions have one component!");
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker if (Cond.empty()) {
274*9880d681SAndroid Build Coastguard Worker // Unconditional branch?
275*9880d681SAndroid Build Coastguard Worker assert(!FBB && "Unconditional branch with multiple successors!");
276*9880d681SAndroid Build Coastguard Worker BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
277*9880d681SAndroid Build Coastguard Worker return 1;
278*9880d681SAndroid Build Coastguard Worker }
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard Worker // Conditional branch.
281*9880d681SAndroid Build Coastguard Worker unsigned Count = 0;
282*9880d681SAndroid Build Coastguard Worker BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
283*9880d681SAndroid Build Coastguard Worker ++Count;
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard Worker if (FBB) {
286*9880d681SAndroid Build Coastguard Worker // Two-way Conditional branch. Insert the second branch.
287*9880d681SAndroid Build Coastguard Worker BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
288*9880d681SAndroid Build Coastguard Worker ++Count;
289*9880d681SAndroid Build Coastguard Worker }
290*9880d681SAndroid Build Coastguard Worker return Count;
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker /// GetInstSize - Return the number of bytes of code the specified
294*9880d681SAndroid Build Coastguard Worker /// instruction may be. This returns the maximum number of bytes.
295*9880d681SAndroid Build Coastguard Worker ///
GetInstSizeInBytes(const MachineInstr & MI) const296*9880d681SAndroid Build Coastguard Worker unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr &MI) const {
297*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &Desc = MI.getDesc();
298*9880d681SAndroid Build Coastguard Worker
299*9880d681SAndroid Build Coastguard Worker switch (Desc.TSFlags & MSP430II::SizeMask) {
300*9880d681SAndroid Build Coastguard Worker default:
301*9880d681SAndroid Build Coastguard Worker switch (Desc.getOpcode()) {
302*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unknown instruction size!");
303*9880d681SAndroid Build Coastguard Worker case TargetOpcode::CFI_INSTRUCTION:
304*9880d681SAndroid Build Coastguard Worker case TargetOpcode::EH_LABEL:
305*9880d681SAndroid Build Coastguard Worker case TargetOpcode::IMPLICIT_DEF:
306*9880d681SAndroid Build Coastguard Worker case TargetOpcode::KILL:
307*9880d681SAndroid Build Coastguard Worker case TargetOpcode::DBG_VALUE:
308*9880d681SAndroid Build Coastguard Worker return 0;
309*9880d681SAndroid Build Coastguard Worker case TargetOpcode::INLINEASM: {
310*9880d681SAndroid Build Coastguard Worker const MachineFunction *MF = MI.getParent()->getParent();
311*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
312*9880d681SAndroid Build Coastguard Worker return TII.getInlineAsmLength(MI.getOperand(0).getSymbolName(),
313*9880d681SAndroid Build Coastguard Worker *MF->getTarget().getMCAsmInfo());
314*9880d681SAndroid Build Coastguard Worker }
315*9880d681SAndroid Build Coastguard Worker }
316*9880d681SAndroid Build Coastguard Worker case MSP430II::SizeSpecial:
317*9880d681SAndroid Build Coastguard Worker switch (MI.getOpcode()) {
318*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unknown instruction size!");
319*9880d681SAndroid Build Coastguard Worker case MSP430::SAR8r1c:
320*9880d681SAndroid Build Coastguard Worker case MSP430::SAR16r1c:
321*9880d681SAndroid Build Coastguard Worker return 4;
322*9880d681SAndroid Build Coastguard Worker }
323*9880d681SAndroid Build Coastguard Worker case MSP430II::Size2Bytes:
324*9880d681SAndroid Build Coastguard Worker return 2;
325*9880d681SAndroid Build Coastguard Worker case MSP430II::Size4Bytes:
326*9880d681SAndroid Build Coastguard Worker return 4;
327*9880d681SAndroid Build Coastguard Worker case MSP430II::Size6Bytes:
328*9880d681SAndroid Build Coastguard Worker return 6;
329*9880d681SAndroid Build Coastguard Worker }
330*9880d681SAndroid Build Coastguard Worker }
331