blob: 199a323bf2be04f99e0860707236d0874bf3eec1 [file] [log] [blame]
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +00001//===- MSP430InstrInfo.cpp - MSP430 Instruction Information ---------------===//
2//
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
14#include "MSP430.h"
15#include "MSP430InstrInfo.h"
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +000016#include "MSP430MachineFunctionInfo.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000017#include "MSP430TargetMachine.h"
18#include "MSP430GenInstrInfo.inc"
19#include "llvm/Function.h"
Anton Korobeynikovaa299152009-05-03 13:09:57 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikovaa299152009-05-03 13:09:57 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000024
25using namespace llvm;
26
27MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
28 : TargetInstrInfoImpl(MSP430Insts, array_lengthof(MSP430Insts)),
Anton Korobeynikovb5612642009-05-03 13:07:54 +000029 RI(tm, *this), TM(tm) {}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000030
Anton Korobeynikovaa299152009-05-03 13:09:57 +000031void 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();
37
38 if (RC == &MSP430::GR16RegClass)
39 BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
40 .addFrameIndex(FrameIdx).addImm(0)
41 .addReg(SrcReg, false, false, isKill);
42 else if (RC == &MSP430::GR8RegClass)
43 BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
44 .addFrameIndex(FrameIdx).addImm(0)
45 .addReg(SrcReg, false, false, isKill);
46 else
47 assert(0 && "Cannot store this register to stack slot!");
48}
49
50void 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();
56
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);
63 else
64 assert(0 && "Cannot store this register to stack slot!");
65}
66
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000067bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
68 MachineBasicBlock::iterator I,
69 unsigned DestReg, unsigned SrcReg,
70 const TargetRegisterClass *DestRC,
71 const TargetRegisterClass *SrcRC) const {
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000072 DebugLoc DL = DebugLoc::getUnknownLoc();
73 if (I != MBB.end()) DL = I->getDebugLoc();
74
Anton Korobeynikov51c31d62009-05-03 13:05:42 +000075 if (DestRC == SrcRC) {
76 unsigned Opc;
77 if (DestRC == &MSP430::GR16RegClass) {
78 Opc = MSP430::MOV16rr;
79 } else if (DestRC == &MSP430::GR8RegClass) {
80 Opc = MSP430::MOV8rr;
81 } else {
82 return false;
83 }
84
85 BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg);
86 return true;
87 }
88
89 return false;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000090}
91
92bool
93MSP430InstrInfo::isMoveInstr(const MachineInstr& MI,
94 unsigned &SrcReg, unsigned &DstReg,
95 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
96 SrcSubIdx = DstSubIdx = 0; // No sub-registers yet.
97
98 switch (MI.getOpcode()) {
99 default:
100 return false;
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000101 case MSP430::MOV8rr:
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000102 case MSP430::MOV16rr:
Anton Korobeynikov51c31d62009-05-03 13:05:42 +0000103 assert(MI.getNumOperands() >= 2 &&
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000104 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();
109 return true;
110 }
111}
Anton Korobeynikovd5047cb2009-05-03 13:11:04 +0000112
113bool
114MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
115 MachineBasicBlock::iterator MI,
116 const std::vector<CalleeSavedInfo> &CSI) const {
117 if (CSI.empty())
118 return false;
119
120 DebugLoc DL = DebugLoc::getUnknownLoc();
121 if (MI != MBB.end()) DL = MI->getDebugLoc();
122
123 MachineFunction &MF = *MBB.getParent();
124 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
125 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
126
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.
130 MBB.addLiveIn(Reg);
131 BuildMI(MBB, MI, DL, get(MSP430::PUSH16r))
132 .addReg(Reg, /*isDef=*/false, /*isImp=*/false, /*isKill=*/true);
133 }
134 return true;
135}
136
137bool
138MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
139 MachineBasicBlock::iterator MI,
140 const std::vector<CalleeSavedInfo> &CSI) const {
141 if (CSI.empty())
142 return false;
143
144 DebugLoc DL = DebugLoc::getUnknownLoc();
145 if (MI != MBB.end()) DL = MI->getDebugLoc();
146
147 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
148 BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg());
149
150 return true;
151}