MSP430InstrInfo.cpp revision c23197a26f34f559ea9797de51e187087c039c42
1//===- 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"
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"
24#include "llvm/Support/ErrorHandling.h"
25
26using namespace llvm;
27
28MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
29  : TargetInstrInfoImpl(MSP430Insts, array_lengthof(MSP430Insts)),
30    RI(tm, *this), TM(tm) {}
31
32void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
33                                          MachineBasicBlock::iterator MI,
34                                    unsigned SrcReg, bool isKill, int FrameIdx,
35                                    const TargetRegisterClass *RC) const {
36  DebugLoc DL = DebugLoc::getUnknownLoc();
37  if (MI != MBB.end()) DL = MI->getDebugLoc();
38
39  if (RC == &MSP430::GR16RegClass)
40    BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
41      .addFrameIndex(FrameIdx).addImm(0)
42      .addReg(SrcReg, getKillRegState(isKill));
43  else if (RC == &MSP430::GR8RegClass)
44    BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
45      .addFrameIndex(FrameIdx).addImm(0)
46      .addReg(SrcReg, getKillRegState(isKill));
47  else
48    llvm_unreachable("Cannot store this register to stack slot!");
49}
50
51void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
52                                           MachineBasicBlock::iterator MI,
53                                           unsigned DestReg, int FrameIdx,
54                                           const TargetRegisterClass *RC) const{
55  DebugLoc DL = DebugLoc::getUnknownLoc();
56  if (MI != MBB.end()) DL = MI->getDebugLoc();
57
58  if (RC == &MSP430::GR16RegClass)
59    BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
60      .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0);
61  else if (RC == &MSP430::GR8RegClass)
62    BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
63      .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0);
64  else
65    llvm_unreachable("Cannot store this register to stack slot!");
66}
67
68bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
69                                   MachineBasicBlock::iterator I,
70                                   unsigned DestReg, unsigned SrcReg,
71                                   const TargetRegisterClass *DestRC,
72                                   const TargetRegisterClass *SrcRC) const {
73  DebugLoc DL = DebugLoc::getUnknownLoc();
74  if (I != MBB.end()) DL = I->getDebugLoc();
75
76  if (DestRC == SrcRC) {
77    unsigned Opc;
78    if (DestRC == &MSP430::GR16RegClass) {
79      Opc = MSP430::MOV16rr;
80    } else if (DestRC == &MSP430::GR8RegClass) {
81      Opc = MSP430::MOV8rr;
82    } else {
83      return false;
84    }
85
86    BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg);
87    return true;
88  }
89
90  return false;
91}
92
93bool
94MSP430InstrInfo::isMoveInstr(const MachineInstr& MI,
95                             unsigned &SrcReg, unsigned &DstReg,
96                             unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
97  SrcSubIdx = DstSubIdx = 0; // No sub-registers yet.
98
99  switch (MI.getOpcode()) {
100  default:
101    return false;
102  case MSP430::MOV8rr:
103  case MSP430::MOV16rr:
104   assert(MI.getNumOperands() >= 2 &&
105           MI.getOperand(0).isReg() &&
106           MI.getOperand(1).isReg() &&
107           "invalid register-register move instruction");
108    SrcReg = MI.getOperand(1).getReg();
109    DstReg = MI.getOperand(0).getReg();
110    return true;
111  }
112}
113
114bool
115MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
116                                           MachineBasicBlock::iterator MI,
117                                const std::vector<CalleeSavedInfo> &CSI) const {
118  if (CSI.empty())
119    return false;
120
121  DebugLoc DL = DebugLoc::getUnknownLoc();
122  if (MI != MBB.end()) DL = MI->getDebugLoc();
123
124  MachineFunction &MF = *MBB.getParent();
125  MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
126  MFI->setCalleeSavedFrameSize(CSI.size() * 2);
127
128  for (unsigned i = CSI.size(); i != 0; --i) {
129    unsigned Reg = CSI[i-1].getReg();
130    // Add the callee-saved register as live-in. It's killed at the spill.
131    MBB.addLiveIn(Reg);
132    BuildMI(MBB, MI, DL, get(MSP430::PUSH16r))
133      .addReg(Reg, RegState::Kill);
134  }
135  return true;
136}
137
138bool
139MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
140                                             MachineBasicBlock::iterator MI,
141                                const std::vector<CalleeSavedInfo> &CSI) const {
142  if (CSI.empty())
143    return false;
144
145  DebugLoc DL = DebugLoc::getUnknownLoc();
146  if (MI != MBB.end()) DL = MI->getDebugLoc();
147
148  for (unsigned i = 0, e = CSI.size(); i != e; ++i)
149    BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg());
150
151  return true;
152}
153
154unsigned
155MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
156                              MachineBasicBlock *FBB,
157                            const SmallVectorImpl<MachineOperand> &Cond) const {
158  // FIXME this should probably have a DebugLoc operand
159  DebugLoc dl = DebugLoc::getUnknownLoc();
160
161  // Shouldn't be a fall through.
162  assert(TBB && "InsertBranch must not be told to insert a fallthrough");
163  assert((Cond.size() == 1 || Cond.size() == 0) &&
164         "MSP430 branch conditions have one component!");
165
166  if (Cond.empty()) {
167    // Unconditional branch?
168    assert(!FBB && "Unconditional branch with multiple successors!");
169    BuildMI(&MBB, dl, get(MSP430::JMP)).addMBB(TBB);
170    return 1;
171  }
172
173  // Conditional branch.
174  unsigned Count = 0;
175  llvm_unreachable("Implement conditional branches!");
176
177  return Count;
178}
179