1 //===- MSP430InstrInfo.cpp - MSP430 Instruction Information ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the MSP430 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "MSP430InstrInfo.h"
16 #include "MSP430MachineFunctionInfo.h"
17 #include "MSP430TargetMachine.h"
18 #include "MSP430GenInstrInfo.inc"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
27 MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine
&tm
)
28 : TargetInstrInfoImpl(MSP430Insts
, array_lengthof(MSP430Insts
)),
29 RI(tm
, *this), TM(tm
) {}
31 void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock
&MBB
,
32 MachineBasicBlock::iterator MI
,
33 unsigned SrcReg
, bool isKill
, int FrameIdx
,
34 const TargetRegisterClass
*RC
) const {
35 DebugLoc DL
= DebugLoc::getUnknownLoc();
36 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
38 if (RC
== &MSP430::GR16RegClass
)
39 BuildMI(MBB
, MI
, DL
, get(MSP430::MOV16mr
))
40 .addFrameIndex(FrameIdx
).addImm(0)
41 .addReg(SrcReg
, getKillRegState(isKill
));
42 else if (RC
== &MSP430::GR8RegClass
)
43 BuildMI(MBB
, MI
, DL
, get(MSP430::MOV8mr
))
44 .addFrameIndex(FrameIdx
).addImm(0)
45 .addReg(SrcReg
, getKillRegState(isKill
));
47 assert(0 && "Cannot store this register to stack slot!");
50 void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
51 MachineBasicBlock::iterator MI
,
52 unsigned DestReg
, int FrameIdx
,
53 const TargetRegisterClass
*RC
) const{
54 DebugLoc DL
= DebugLoc::getUnknownLoc();
55 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
57 if (RC
== &MSP430::GR16RegClass
)
58 BuildMI(MBB
, MI
, DL
, get(MSP430::MOV16rm
))
59 .addReg(DestReg
).addFrameIndex(FrameIdx
).addImm(0);
60 else if (RC
== &MSP430::GR8RegClass
)
61 BuildMI(MBB
, MI
, DL
, get(MSP430::MOV8rm
))
62 .addReg(DestReg
).addFrameIndex(FrameIdx
).addImm(0);
64 assert(0 && "Cannot store this register to stack slot!");
67 bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock
&MBB
,
68 MachineBasicBlock::iterator I
,
69 unsigned DestReg
, unsigned SrcReg
,
70 const TargetRegisterClass
*DestRC
,
71 const TargetRegisterClass
*SrcRC
) const {
72 DebugLoc DL
= DebugLoc::getUnknownLoc();
73 if (I
!= MBB
.end()) DL
= I
->getDebugLoc();
75 if (DestRC
== SrcRC
) {
77 if (DestRC
== &MSP430::GR16RegClass
) {
78 Opc
= MSP430::MOV16rr
;
79 } else if (DestRC
== &MSP430::GR8RegClass
) {
85 BuildMI(MBB
, I
, DL
, get(Opc
), DestReg
).addReg(SrcReg
);
93 MSP430InstrInfo::isMoveInstr(const MachineInstr
& MI
,
94 unsigned &SrcReg
, unsigned &DstReg
,
95 unsigned &SrcSubIdx
, unsigned &DstSubIdx
) const {
96 SrcSubIdx
= DstSubIdx
= 0; // No sub-registers yet.
98 switch (MI
.getOpcode()) {
102 case MSP430::MOV16rr
:
103 assert(MI
.getNumOperands() >= 2 &&
104 MI
.getOperand(0).isReg() &&
105 MI
.getOperand(1).isReg() &&
106 "invalid register-register move instruction");
107 SrcReg
= MI
.getOperand(1).getReg();
108 DstReg
= MI
.getOperand(0).getReg();
114 MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock
&MBB
,
115 MachineBasicBlock::iterator MI
,
116 const std::vector
<CalleeSavedInfo
> &CSI
) const {
120 DebugLoc DL
= DebugLoc::getUnknownLoc();
121 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
123 MachineFunction
&MF
= *MBB
.getParent();
124 MSP430MachineFunctionInfo
*MFI
= MF
.getInfo
<MSP430MachineFunctionInfo
>();
125 MFI
->setCalleeSavedFrameSize(CSI
.size() * 2);
127 for (unsigned i
= CSI
.size(); i
!= 0; --i
) {
128 unsigned Reg
= CSI
[i
-1].getReg();
129 // Add the callee-saved register as live-in. It's killed at the spill.
131 BuildMI(MBB
, MI
, DL
, get(MSP430::PUSH16r
))
132 .addReg(Reg
, RegState::Kill
);
138 MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock
&MBB
,
139 MachineBasicBlock::iterator MI
,
140 const std::vector
<CalleeSavedInfo
> &CSI
) const {
144 DebugLoc DL
= DebugLoc::getUnknownLoc();
145 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
147 for (unsigned i
= 0, e
= CSI
.size(); i
!= e
; ++i
)
148 BuildMI(MBB
, MI
, DL
, get(MSP430::POP16r
), CSI
[i
].getReg());
154 MSP430InstrInfo::InsertBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
*TBB
,
155 MachineBasicBlock
*FBB
,
156 const SmallVectorImpl
<MachineOperand
> &Cond
) const {
157 // FIXME this should probably have a DebugLoc operand
158 DebugLoc dl
= DebugLoc::getUnknownLoc();
160 // Shouldn't be a fall through.
161 assert(TBB
&& "InsertBranch must not be told to insert a fallthrough");
162 assert((Cond
.size() == 1 || Cond
.size() == 0) &&
163 "MSP430 branch conditions have one component!");
166 // Unconditional branch?
167 assert(!FBB
&& "Unconditional branch with multiple successors!");
168 BuildMI(&MBB
, dl
, get(MSP430::JMP
)).addMBB(TBB
);
172 // Conditional branch.
174 assert(0 && "Implement conditional branches!");