blob: 99c27fdac77fb0b67b50d268a527286121596c87 [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//===- PIC16InstrInfo.cpp - PIC16 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 PIC16 implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PIC16.h"
15#include "PIC16InstrInfo.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000016#include "PIC16TargetMachine.h"
17#include "PIC16GenInstrInfo.inc"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000018#include "llvm/Function.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Duncan Sandsc6dbe7f2008-11-28 10:20:03 +000023#include <cstdio>
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000024
Sanjiv Gupta0e687712008-05-13 09:02:57 +000025
26using namespace llvm;
27
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000028// FIXME: Add the subtarget support on this constructor.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm)
30 : TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)),
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000031 TM(tm),
32 RegInfo(*this, *TM.getSubtargetImpl()) {}
Sanjiv Gupta0e687712008-05-13 09:02:57 +000033
Sanjiv Gupta0e687712008-05-13 09:02:57 +000034
35/// isStoreToStackSlot - If the specified machine instruction is a direct
36/// store to a stack slot, return the virtual or physical register number of
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000037/// 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
Sanjiv Gupta0e687712008-05-13 09:02:57 +000039/// any side effects other than storing to the stack slot.
Sanjiv Gupta863d3e92008-11-19 11:27:59 +000040unsigned PIC16InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000041 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();
Sanjiv Gupta0e687712008-05-13 09:02:57 +000047 }
48 return 0;
49}
50
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000051/// 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.
Sanjiv Gupta863d3e92008-11-19 11:27:59 +000056unsigned PIC16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000057 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();
63 }
64 return 0;
65}
66
67
68void PIC16InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
69 MachineBasicBlock::iterator I,
70 unsigned SrcReg, bool isKill, int FI,
71 const TargetRegisterClass *RC) const {
Sanjiv Guptacae1b622009-04-06 10:54:50 +000072 PIC16TargetLowering *PTLI = TM.getTargetLowering();
Bill Wendlingd1c321a2009-02-12 00:02:55 +000073 DebugLoc DL = DebugLoc::getUnknownLoc();
74 if (I != MBB.end()) DL = I->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000075
Sanjiv Gupta0e687712008-05-13 09:02:57 +000076 const Function *Func = MBB.getParent()->getFunction();
77 const std::string FuncName = Func->getName();
78
79 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000080 sprintf(tmpName, "%s.tmp", FuncName.c_str());
Sanjiv Gupta0e687712008-05-13 09:02:57 +000081
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000082 // On the order of operands here: think "movwf SrcReg, tmp_slot, offset".
83 if (RC == PIC16::GPRRegisterClass) {
84 //MachineFunction &MF = *MBB.getParent();
85 //MachineRegisterInfo &RI = MF.getRegInfo();
Bill Wendlingd1c321a2009-02-12 00:02:55 +000086 BuildMI(MBB, I, DL, get(PIC16::movwf))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000087 .addReg(SrcReg, false, false, isKill)
Sanjiv Guptacae1b622009-04-06 10:54:50 +000088 .addImm(PTLI->GetTmpOffsetForFI(FI))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000089 .addExternalSymbol(tmpName)
90 .addImm(1); // Emit banksel for it.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000091 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000092 else if (RC == PIC16::FSR16RegisterClass)
93 assert(0 && "Don't know yet how to store a FSR16 to stack slot");
Sanjiv Gupta0e687712008-05-13 09:02:57 +000094 else
95 assert(0 && "Can't store this register to stack slot");
96}
97
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000098void PIC16InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
99 MachineBasicBlock::iterator I,
100 unsigned DestReg, int FI,
101 const TargetRegisterClass *RC) const {
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000102 PIC16TargetLowering *PTLI = TM.getTargetLowering();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000103 DebugLoc DL = DebugLoc::getUnknownLoc();
104 if (I != MBB.end()) DL = I->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000105
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000106 const Function *Func = MBB.getParent()->getFunction();
107 const std::string FuncName = Func->getName();
108
109 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000110 sprintf(tmpName, "%s.tmp", FuncName.c_str());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000111
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000112 // On the order of operands here: think "movf FrameIndex, W".
113 if (RC == PIC16::GPRRegisterClass) {
114 //MachineFunction &MF = *MBB.getParent();
115 //MachineRegisterInfo &RI = MF.getRegInfo();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000116 BuildMI(MBB, I, DL, get(PIC16::movf), DestReg)
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000117 .addImm(PTLI->GetTmpOffsetForFI(FI))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000118 .addExternalSymbol(tmpName)
119 .addImm(1); // Emit banksel for it.
120 }
121 else if (RC == PIC16::FSR16RegisterClass)
122 assert(0 && "Don't know yet how to load an FSR16 from stack slot");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000123 else
124 assert(0 && "Can't load this register from stack slot");
125}
126
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000127bool PIC16InstrInfo::copyRegToReg (MachineBasicBlock &MBB,
128 MachineBasicBlock::iterator I,
129 unsigned DestReg, unsigned SrcReg,
130 const TargetRegisterClass *DestRC,
131 const TargetRegisterClass *SrcRC) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000132 DebugLoc DL = DebugLoc::getUnknownLoc();
133 if (I != MBB.end()) DL = I->getDebugLoc();
134
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000135 if (DestRC == PIC16::FSR16RegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000136 BuildMI(MBB, I, DL, get(PIC16::copy_fsr), DestReg).addReg(SrcReg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000137 return true;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000138 }
139
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000140 if (DestRC == PIC16::GPRRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000141 BuildMI(MBB, I, DL, get(PIC16::copy_w), DestReg).addReg(SrcReg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000142 return true;
143 }
144
145 // Not yet supported.
146 return false;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000147}
148
149bool PIC16InstrInfo::isMoveInstr(const MachineInstr &MI,
Sanjiv Gupta25305662009-01-21 09:02:46 +0000150 unsigned &SrcReg, unsigned &DestReg,
151 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
152 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000153
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000154 if (MI.getOpcode() == PIC16::copy_fsr
155 || MI.getOpcode() == PIC16::copy_w) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000156 DestReg = MI.getOperand(0).getReg();
157 SrcReg = MI.getOperand(1).getReg();
158 return true;
159 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000160
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000161 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000162}
163