blob: 793dd9f029f93bf6d964c391bc9a7928ede2f0b0 [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"
Sanjiv Gupta753ec152009-10-15 19:26:25 +000015#include "PIC16ABINames.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000016#include "PIC16InstrInfo.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000017#include "PIC16TargetMachine.h"
18#include "PIC16GenInstrInfo.inc"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000019#include "llvm/Function.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Duncan Sandsc6dbe7f2008-11-28 10:20:03 +000025#include <cstdio>
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000026
Sanjiv Gupta0e687712008-05-13 09:02:57 +000027
28using namespace llvm;
29
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000030// FIXME: Add the subtarget support on this constructor.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000031PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm)
32 : TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)),
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000033 TM(tm),
34 RegInfo(*this, *TM.getSubtargetImpl()) {}
Sanjiv Gupta0e687712008-05-13 09:02:57 +000035
Sanjiv Gupta0e687712008-05-13 09:02:57 +000036
37/// isStoreToStackSlot - If the specified machine instruction is a direct
38/// store to a stack slot, return the virtual or physical register number of
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000039/// the source reg along with the FrameIndex of the loaded stack slot.
40/// If not, return 0. This predicate must return 0 if the instruction has
Sanjiv Gupta0e687712008-05-13 09:02:57 +000041/// any side effects other than storing to the stack slot.
Sanjiv Gupta863d3e92008-11-19 11:27:59 +000042unsigned PIC16InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000043 int &FrameIndex) const {
44 if (MI->getOpcode() == PIC16::movwf
45 && MI->getOperand(0).isReg()
46 && MI->getOperand(1).isSymbol()) {
47 FrameIndex = MI->getOperand(1).getIndex();
48 return MI->getOperand(0).getReg();
Sanjiv Gupta0e687712008-05-13 09:02:57 +000049 }
50 return 0;
51}
52
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000053/// isLoadFromStackSlot - If the specified machine instruction is a direct
54/// load from a stack slot, return the virtual or physical register number of
55/// the dest reg along with the FrameIndex of the stack slot.
56/// If not, return 0. This predicate must return 0 if the instruction has
57/// any side effects other than storing to the stack slot.
Sanjiv Gupta863d3e92008-11-19 11:27:59 +000058unsigned PIC16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000059 int &FrameIndex) const {
60 if (MI->getOpcode() == PIC16::movf
61 && MI->getOperand(0).isReg()
62 && MI->getOperand(1).isSymbol()) {
63 FrameIndex = MI->getOperand(1).getIndex();
64 return MI->getOperand(0).getReg();
65 }
66 return 0;
67}
68
69
70void PIC16InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
71 MachineBasicBlock::iterator I,
72 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +000073 const TargetRegisterClass *RC,
74 const TargetRegisterInfo *TRI) const {
Dan Gohmand858e902010-04-17 15:26:15 +000075 const PIC16TargetLowering *PTLI = TM.getTargetLowering();
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000076 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000077 if (I != MBB.end()) DL = I->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000078
Sanjiv Gupta0e687712008-05-13 09:02:57 +000079 const Function *Func = MBB.getParent()->getFunction();
80 const std::string FuncName = Func->getName();
81
Sanjiv Guptad49baef2010-04-07 03:36:01 +000082 const char *tmpName = ESNames::createESName(PAN::getTempdataLabel(FuncName));
Sanjiv Gupta0e687712008-05-13 09:02:57 +000083
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000084 // On the order of operands here: think "movwf SrcReg, tmp_slot, offset".
85 if (RC == PIC16::GPRRegisterClass) {
86 //MachineFunction &MF = *MBB.getParent();
87 //MachineRegisterInfo &RI = MF.getRegInfo();
Bill Wendlingd1c321a2009-02-12 00:02:55 +000088 BuildMI(MBB, I, DL, get(PIC16::movwf))
Bill Wendling587daed2009-05-13 21:33:08 +000089 .addReg(SrcReg, getKillRegState(isKill))
Dan Gohman1e93df62010-04-17 14:41:14 +000090 .addImm(PTLI->GetTmpOffsetForFI(FI, 1, *MBB.getParent()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000091 .addExternalSymbol(tmpName)
92 .addImm(1); // Emit banksel for it.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000093 }
Sanjiv Guptaa3613be2009-04-10 15:10:14 +000094 else if (RC == PIC16::FSR16RegisterClass) {
95 // This is a 16-bit register and the frameindex given by llvm is of
96 // size two here. Break this index N into two zero based indexes and
97 // put one into the map. The second one is always obtained by adding 1
98 // to the first zero based index. In fact it is going to use 3 slots
99 // as saving FSRs corrupts W also and hence we need to save/restore W also.
100
101 unsigned opcode = (SrcReg == PIC16::FSR0) ? PIC16::save_fsr0
102 : PIC16::save_fsr1;
103 BuildMI(MBB, I, DL, get(opcode))
Bill Wendling587daed2009-05-13 21:33:08 +0000104 .addReg(SrcReg, getKillRegState(isKill))
Dan Gohman1e93df62010-04-17 14:41:14 +0000105 .addImm(PTLI->GetTmpOffsetForFI(FI, 3, *MBB.getParent()))
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000106 .addExternalSymbol(tmpName)
107 .addImm(1); // Emit banksel for it.
108 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000109 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000110 llvm_unreachable("Can't store this register to stack slot");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000111}
112
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000113void PIC16InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
114 MachineBasicBlock::iterator I,
115 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000116 const TargetRegisterClass *RC,
117 const TargetRegisterInfo *TRI) const {
Dan Gohmand858e902010-04-17 15:26:15 +0000118 const PIC16TargetLowering *PTLI = TM.getTargetLowering();
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000119 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000120 if (I != MBB.end()) DL = I->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000121
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000122 const Function *Func = MBB.getParent()->getFunction();
123 const std::string FuncName = Func->getName();
124
Sanjiv Guptad49baef2010-04-07 03:36:01 +0000125 const char *tmpName = ESNames::createESName(PAN::getTempdataLabel(FuncName));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000126
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000127 // On the order of operands here: think "movf FrameIndex, W".
128 if (RC == PIC16::GPRRegisterClass) {
129 //MachineFunction &MF = *MBB.getParent();
130 //MachineRegisterInfo &RI = MF.getRegInfo();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000131 BuildMI(MBB, I, DL, get(PIC16::movf), DestReg)
Dan Gohman1e93df62010-04-17 14:41:14 +0000132 .addImm(PTLI->GetTmpOffsetForFI(FI, 1, *MBB.getParent()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000133 .addExternalSymbol(tmpName)
134 .addImm(1); // Emit banksel for it.
135 }
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000136 else if (RC == PIC16::FSR16RegisterClass) {
137 // This is a 16-bit register and the frameindex given by llvm is of
138 // size two here. Break this index N into two zero based indexes and
139 // put one into the map. The second one is always obtained by adding 1
140 // to the first zero based index. In fact it is going to use 3 slots
141 // as saving FSRs corrupts W also and hence we need to save/restore W also.
142
143 unsigned opcode = (DestReg == PIC16::FSR0) ? PIC16::restore_fsr0
144 : PIC16::restore_fsr1;
145 BuildMI(MBB, I, DL, get(opcode), DestReg)
Dan Gohman1e93df62010-04-17 14:41:14 +0000146 .addImm(PTLI->GetTmpOffsetForFI(FI, 3, *MBB.getParent()))
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000147 .addExternalSymbol(tmpName)
148 .addImm(1); // Emit banksel for it.
149 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000150 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000151 llvm_unreachable("Can't load this register from stack slot");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000152}
153
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000154bool PIC16InstrInfo::copyRegToReg (MachineBasicBlock &MBB,
155 MachineBasicBlock::iterator I,
156 unsigned DestReg, unsigned SrcReg,
157 const TargetRegisterClass *DestRC,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000158 const TargetRegisterClass *SrcRC,
159 DebugLoc DL) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000160
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000161 if (DestRC == PIC16::FSR16RegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000162 BuildMI(MBB, I, DL, get(PIC16::copy_fsr), DestReg).addReg(SrcReg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000163 return true;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000164 }
165
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000166 if (DestRC == PIC16::GPRRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000167 BuildMI(MBB, I, DL, get(PIC16::copy_w), DestReg).addReg(SrcReg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000168 return true;
169 }
170
171 // Not yet supported.
172 return false;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000173}
174
175bool PIC16InstrInfo::isMoveInstr(const MachineInstr &MI,
Sanjiv Gupta25305662009-01-21 09:02:46 +0000176 unsigned &SrcReg, unsigned &DestReg,
177 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
178 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000179
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000180 if (MI.getOpcode() == PIC16::copy_fsr
181 || MI.getOpcode() == PIC16::copy_w) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000182 DestReg = MI.getOperand(0).getReg();
183 SrcReg = MI.getOperand(1).getReg();
184 return true;
185 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000186
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000187 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000188}
189
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000190/// InsertBranch - Insert a branch into the end of the specified
191/// MachineBasicBlock. This operands to this method are the same as those
192/// returned by AnalyzeBranch. This is invoked in cases where AnalyzeBranch
193/// returns success and when an unconditional branch (TBB is non-null, FBB is
194/// null, Cond is empty) needs to be inserted. It returns the number of
195/// instructions inserted.
196unsigned PIC16InstrInfo::
197InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
198 MachineBasicBlock *FBB,
199 const SmallVectorImpl<MachineOperand> &Cond) const {
200 // Shouldn't be a fall through.
201 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
202
203 if (FBB == 0) { // One way branch.
204 if (Cond.empty()) {
205 // Unconditional branch?
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000206 DebugLoc dl;
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000207 BuildMI(&MBB, dl, get(PIC16::br_uncond)).addMBB(TBB);
208 }
209 return 1;
210 }
211
212 // FIXME: If the there are some conditions specified then conditional branch
213 // should be generated.
214 // For the time being no instruction is being generated therefore
215 // returning NULL.
216 return 0;
217}
Sanjiv Guptae70b8972009-10-27 17:40:24 +0000218
219bool PIC16InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
220 MachineBasicBlock *&TBB,
221 MachineBasicBlock *&FBB,
222 SmallVectorImpl<MachineOperand> &Cond,
223 bool AllowModify) const {
224 MachineBasicBlock::iterator I = MBB.end();
225 if (I == MBB.begin())
226 return true;
227
228 // Get the terminator instruction.
229 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000230 while (I->isDebugValue()) {
231 if (I == MBB.begin())
232 return true;
233 --I;
234 }
Sanjiv Guptae70b8972009-10-27 17:40:24 +0000235 // Handle unconditional branches. If the unconditional branch's target is
236 // successor basic block then remove the unconditional branch.
237 if (I->getOpcode() == PIC16::br_uncond && AllowModify) {
238 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
239 TBB = 0;
240 I->eraseFromParent();
241 }
242 }
243 return true;
244}