blob: dad0266b7b3d55baf2c0b1bd863a9fd6d238de96 [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"
Torok Edwinc25e7582009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Duncan Sandsc6dbe7f2008-11-28 10:20:03 +000024#include <cstdio>
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000025
Sanjiv Gupta0e687712008-05-13 09:02:57 +000026
27using namespace llvm;
28
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000029// FIXME: Add the subtarget support on this constructor.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000030PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm)
31 : TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)),
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000032 TM(tm),
33 RegInfo(*this, *TM.getSubtargetImpl()) {}
Sanjiv Gupta0e687712008-05-13 09:02:57 +000034
Sanjiv Gupta0e687712008-05-13 09:02:57 +000035
36/// isStoreToStackSlot - If the specified machine instruction is a direct
37/// store to a stack slot, return the virtual or physical register number of
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000038/// the source reg along with the FrameIndex of the loaded stack slot.
39/// If not, return 0. This predicate must return 0 if the instruction has
Sanjiv Gupta0e687712008-05-13 09:02:57 +000040/// any side effects other than storing to the stack slot.
Sanjiv Gupta863d3e92008-11-19 11:27:59 +000041unsigned PIC16InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000042 int &FrameIndex) const {
43 if (MI->getOpcode() == PIC16::movwf
44 && MI->getOperand(0).isReg()
45 && MI->getOperand(1).isSymbol()) {
46 FrameIndex = MI->getOperand(1).getIndex();
47 return MI->getOperand(0).getReg();
Sanjiv Gupta0e687712008-05-13 09:02:57 +000048 }
49 return 0;
50}
51
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000052/// isLoadFromStackSlot - If the specified machine instruction is a direct
53/// load from a stack slot, return the virtual or physical register number of
54/// the dest reg along with the FrameIndex of the stack slot.
55/// If not, return 0. This predicate must return 0 if the instruction has
56/// any side effects other than storing to the stack slot.
Sanjiv Gupta863d3e92008-11-19 11:27:59 +000057unsigned PIC16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000058 int &FrameIndex) const {
59 if (MI->getOpcode() == PIC16::movf
60 && MI->getOperand(0).isReg()
61 && MI->getOperand(1).isSymbol()) {
62 FrameIndex = MI->getOperand(1).getIndex();
63 return MI->getOperand(0).getReg();
64 }
65 return 0;
66}
67
68
69void PIC16InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
70 MachineBasicBlock::iterator I,
71 unsigned SrcReg, bool isKill, int FI,
72 const TargetRegisterClass *RC) const {
Sanjiv Guptacae1b622009-04-06 10:54:50 +000073 PIC16TargetLowering *PTLI = TM.getTargetLowering();
Bill Wendlingd1c321a2009-02-12 00:02:55 +000074 DebugLoc DL = DebugLoc::getUnknownLoc();
75 if (I != MBB.end()) DL = I->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000076
Sanjiv Gupta0e687712008-05-13 09:02:57 +000077 const Function *Func = MBB.getParent()->getFunction();
78 const std::string FuncName = Func->getName();
79
Sanjiv Gupta211f3622009-05-10 05:23:47 +000080 const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
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))
Bill Wendling587daed2009-05-13 21:33:08 +000087 .addReg(SrcReg, getKillRegState(isKill))
Sanjiv Guptaa3613be2009-04-10 15:10:14 +000088 .addImm(PTLI->GetTmpOffsetForFI(FI, 1))
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 Guptaa3613be2009-04-10 15:10:14 +000092 else if (RC == PIC16::FSR16RegisterClass) {
93 // This is a 16-bit register and the frameindex given by llvm is of
94 // size two here. Break this index N into two zero based indexes and
95 // put one into the map. The second one is always obtained by adding 1
96 // to the first zero based index. In fact it is going to use 3 slots
97 // as saving FSRs corrupts W also and hence we need to save/restore W also.
98
99 unsigned opcode = (SrcReg == PIC16::FSR0) ? PIC16::save_fsr0
100 : PIC16::save_fsr1;
101 BuildMI(MBB, I, DL, get(opcode))
Bill Wendling587daed2009-05-13 21:33:08 +0000102 .addReg(SrcReg, getKillRegState(isKill))
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000103 .addImm(PTLI->GetTmpOffsetForFI(FI, 3))
104 .addExternalSymbol(tmpName)
105 .addImm(1); // Emit banksel for it.
106 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000107 else
Torok Edwinc25e7582009-07-11 20:10:48 +0000108 LLVM_UNREACHABLE("Can't store this register to stack slot");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000109}
110
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000111void PIC16InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
112 MachineBasicBlock::iterator I,
113 unsigned DestReg, int FI,
114 const TargetRegisterClass *RC) const {
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000115 PIC16TargetLowering *PTLI = TM.getTargetLowering();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000116 DebugLoc DL = DebugLoc::getUnknownLoc();
117 if (I != MBB.end()) DL = I->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000118
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000119 const Function *Func = MBB.getParent()->getFunction();
120 const std::string FuncName = Func->getName();
121
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000122 const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000123
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000124 // On the order of operands here: think "movf FrameIndex, W".
125 if (RC == PIC16::GPRRegisterClass) {
126 //MachineFunction &MF = *MBB.getParent();
127 //MachineRegisterInfo &RI = MF.getRegInfo();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000128 BuildMI(MBB, I, DL, get(PIC16::movf), DestReg)
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000129 .addImm(PTLI->GetTmpOffsetForFI(FI, 1))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000130 .addExternalSymbol(tmpName)
131 .addImm(1); // Emit banksel for it.
132 }
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000133 else if (RC == PIC16::FSR16RegisterClass) {
134 // This is a 16-bit register and the frameindex given by llvm is of
135 // size two here. Break this index N into two zero based indexes and
136 // put one into the map. The second one is always obtained by adding 1
137 // to the first zero based index. In fact it is going to use 3 slots
138 // as saving FSRs corrupts W also and hence we need to save/restore W also.
139
140 unsigned opcode = (DestReg == PIC16::FSR0) ? PIC16::restore_fsr0
141 : PIC16::restore_fsr1;
142 BuildMI(MBB, I, DL, get(opcode), DestReg)
143 .addImm(PTLI->GetTmpOffsetForFI(FI, 3))
144 .addExternalSymbol(tmpName)
145 .addImm(1); // Emit banksel for it.
146 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000147 else
Torok Edwinc25e7582009-07-11 20:10:48 +0000148 LLVM_UNREACHABLE("Can't load this register from stack slot");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000149}
150
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000151bool PIC16InstrInfo::copyRegToReg (MachineBasicBlock &MBB,
152 MachineBasicBlock::iterator I,
153 unsigned DestReg, unsigned SrcReg,
154 const TargetRegisterClass *DestRC,
155 const TargetRegisterClass *SrcRC) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000156 DebugLoc DL = DebugLoc::getUnknownLoc();
157 if (I != MBB.end()) DL = I->getDebugLoc();
158
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000159 if (DestRC == PIC16::FSR16RegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000160 BuildMI(MBB, I, DL, get(PIC16::copy_fsr), DestReg).addReg(SrcReg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000161 return true;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000162 }
163
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000164 if (DestRC == PIC16::GPRRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000165 BuildMI(MBB, I, DL, get(PIC16::copy_w), DestReg).addReg(SrcReg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000166 return true;
167 }
168
169 // Not yet supported.
170 return false;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000171}
172
173bool PIC16InstrInfo::isMoveInstr(const MachineInstr &MI,
Sanjiv Gupta25305662009-01-21 09:02:46 +0000174 unsigned &SrcReg, unsigned &DestReg,
175 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
176 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000177
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000178 if (MI.getOpcode() == PIC16::copy_fsr
179 || MI.getOpcode() == PIC16::copy_w) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000180 DestReg = MI.getOperand(0).getReg();
181 SrcReg = MI.getOperand(1).getReg();
182 return true;
183 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000184
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000185 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000186}
187
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000188/// InsertBranch - Insert a branch into the end of the specified
189/// MachineBasicBlock. This operands to this method are the same as those
190/// returned by AnalyzeBranch. This is invoked in cases where AnalyzeBranch
191/// returns success and when an unconditional branch (TBB is non-null, FBB is
192/// null, Cond is empty) needs to be inserted. It returns the number of
193/// instructions inserted.
194unsigned PIC16InstrInfo::
195InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
196 MachineBasicBlock *FBB,
197 const SmallVectorImpl<MachineOperand> &Cond) const {
198 // Shouldn't be a fall through.
199 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
200
201 if (FBB == 0) { // One way branch.
202 if (Cond.empty()) {
203 // Unconditional branch?
204 DebugLoc dl = DebugLoc::getUnknownLoc();
205 BuildMI(&MBB, dl, get(PIC16::br_uncond)).addMBB(TBB);
206 }
207 return 1;
208 }
209
210 // FIXME: If the there are some conditions specified then conditional branch
211 // should be generated.
212 // For the time being no instruction is being generated therefore
213 // returning NULL.
214 return 0;
215}