1 //===- PIC16InstrInfo.cpp - PIC16 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 PIC16 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "PIC16InstrInfo.h"
16 #include "PIC16TargetMachine.h"
17 #include "PIC16GenInstrInfo.inc"
18 #include "llvm/Function.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 // FIXME: Add the subtarget support on this constructor.
29 PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm)
30 : TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)),
32 RegInfo(*this, *TM.getSubtargetImpl()) {}
35 /// isStoreToStackSlot - If the specified machine instruction is a direct
36 /// store to a stack slot, return the virtual or physical register number of
37 /// the source reg along with the FrameIndex of the loaded stack slot.
38 /// If not, return 0. This predicate must return 0 if the instruction has
39 /// any side effects other than storing to the stack slot.
40 unsigned PIC16InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
41 int &FrameIndex) const {
42 if (MI->getOpcode() == PIC16::movwf
43 && MI->getOperand(0).isReg()
44 && MI->getOperand(1).isSymbol()) {
45 FrameIndex = MI->getOperand(1).getIndex();
46 return MI->getOperand(0).getReg();
51 /// isLoadFromStackSlot - If the specified machine instruction is a direct
52 /// load from a stack slot, return the virtual or physical register number of
53 /// the dest reg along with the FrameIndex of the stack slot.
54 /// If not, return 0. This predicate must return 0 if the instruction has
55 /// any side effects other than storing to the stack slot.
56 unsigned PIC16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
57 int &FrameIndex) const {
58 if (MI->getOpcode() == PIC16::movf
59 && MI->getOperand(0).isReg()
60 && MI->getOperand(1).isSymbol()) {
61 FrameIndex = MI->getOperand(1).getIndex();
62 return MI->getOperand(0).getReg();
68 void PIC16InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
69 MachineBasicBlock::iterator I,
70 unsigned SrcReg, bool isKill, int FI,
71 const TargetRegisterClass *RC) const {
72 DebugLoc DL = DebugLoc::getUnknownLoc();
73 if (I != MBB.end()) DL = I->getDebugLoc();
75 const Function *Func = MBB.getParent()->getFunction();
76 const std::string FuncName = Func->getName();
78 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
79 sprintf(tmpName, "%s.tmp", FuncName.c_str());
81 // On the order of operands here: think "movwf SrcReg, tmp_slot, offset".
82 if (RC == PIC16::GPRRegisterClass) {
83 //MachineFunction &MF = *MBB.getParent();
84 //MachineRegisterInfo &RI = MF.getRegInfo();
85 BuildMI(MBB, I, DL, get(PIC16::movwf))
86 .addReg(SrcReg, false, false, isKill)
88 .addExternalSymbol(tmpName)
89 .addImm(1); // Emit banksel for it.
91 else if (RC == PIC16::FSR16RegisterClass)
92 assert(0 && "Don't know yet how to store a FSR16 to stack slot");
94 assert(0 && "Can't store this register to stack slot");
97 void PIC16InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
98 MachineBasicBlock::iterator I,
99 unsigned DestReg, int FI,
100 const TargetRegisterClass *RC) const {
101 DebugLoc DL = DebugLoc::getUnknownLoc();
102 if (I != MBB.end()) DL = I->getDebugLoc();
104 const Function *Func = MBB.getParent()->getFunction();
105 const std::string FuncName = Func->getName();
107 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
108 sprintf(tmpName, "%s.tmp", FuncName.c_str());
110 // On the order of operands here: think "movf FrameIndex, W".
111 if (RC == PIC16::GPRRegisterClass) {
112 //MachineFunction &MF = *MBB.getParent();
113 //MachineRegisterInfo &RI = MF.getRegInfo();
114 BuildMI(MBB, I, DL, get(PIC16::movf), DestReg)
116 .addExternalSymbol(tmpName)
117 .addImm(1); // Emit banksel for it.
119 else if (RC == PIC16::FSR16RegisterClass)
120 assert(0 && "Don't know yet how to load an FSR16 from stack slot");
122 assert(0 && "Can't load this register from stack slot");
125 bool PIC16InstrInfo::copyRegToReg (MachineBasicBlock &MBB,
126 MachineBasicBlock::iterator I,
127 unsigned DestReg, unsigned SrcReg,
128 const TargetRegisterClass *DestRC,
129 const TargetRegisterClass *SrcRC) const {
130 DebugLoc DL = DebugLoc::getUnknownLoc();
131 if (I != MBB.end()) DL = I->getDebugLoc();
133 if (DestRC == PIC16::FSR16RegisterClass) {
134 BuildMI(MBB, I, DL, get(PIC16::copy_fsr), DestReg).addReg(SrcReg);
138 if (DestRC == PIC16::GPRRegisterClass) {
139 BuildMI(MBB, I, DL, get(PIC16::copy_w), DestReg).addReg(SrcReg);
143 // Not yet supported.
147 bool PIC16InstrInfo::isMoveInstr(const MachineInstr &MI,
148 unsigned &SrcReg, unsigned &DestReg,
149 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
150 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
152 if (MI.getOpcode() == PIC16::copy_fsr
153 || MI.getOpcode() == PIC16::copy_w) {
154 DestReg = MI.getOperand(0).getReg();
155 SrcReg = MI.getOperand(1).getReg();