Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 1 | //===- 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 Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 16 | #include "PIC16TargetMachine.h" |
| 17 | #include "PIC16GenInstrInfo.inc" |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 23 | #include "llvm/Support/ErrorHandling.h" |
Duncan Sands | c6dbe7f | 2008-11-28 10:20:03 +0000 | [diff] [blame] | 24 | #include <cstdio> |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 25 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 29 | // FIXME: Add the subtarget support on this constructor. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 30 | PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm) |
| 31 | : TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)), |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 32 | TM(tm), |
| 33 | RegInfo(*this, *TM.getSubtargetImpl()) {} |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 34 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 35 | |
| 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 Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 38 | /// 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 Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 40 | /// any side effects other than storing to the stack slot. |
Sanjiv Gupta | 863d3e9 | 2008-11-19 11:27:59 +0000 | [diff] [blame] | 41 | unsigned PIC16InstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 42 | 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 Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 52 | /// 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 Gupta | 863d3e9 | 2008-11-19 11:27:59 +0000 | [diff] [blame] | 57 | unsigned PIC16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 58 | 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 | |
| 69 | void PIC16InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
| 70 | MachineBasicBlock::iterator I, |
| 71 | unsigned SrcReg, bool isKill, int FI, |
| 72 | const TargetRegisterClass *RC) const { |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 73 | PIC16TargetLowering *PTLI = TM.getTargetLowering(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 74 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 75 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 76 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 77 | const Function *Func = MBB.getParent()->getFunction(); |
| 78 | const std::string FuncName = Func->getName(); |
| 79 | |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 80 | const char *tmpName = createESName(PAN::getTempdataLabel(FuncName)); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 81 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 82 | // 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 Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 86 | BuildMI(MBB, I, DL, get(PIC16::movwf)) |
Bill Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 87 | .addReg(SrcReg, getKillRegState(isKill)) |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 88 | .addImm(PTLI->GetTmpOffsetForFI(FI, 1)) |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 89 | .addExternalSymbol(tmpName) |
| 90 | .addImm(1); // Emit banksel for it. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 91 | } |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 92 | 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 Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 102 | .addReg(SrcReg, getKillRegState(isKill)) |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 103 | .addImm(PTLI->GetTmpOffsetForFI(FI, 3)) |
| 104 | .addExternalSymbol(tmpName) |
| 105 | .addImm(1); // Emit banksel for it. |
| 106 | } |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 107 | else |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 108 | LLVM_UNREACHABLE("Can't store this register to stack slot"); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 111 | void PIC16InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
| 112 | MachineBasicBlock::iterator I, |
| 113 | unsigned DestReg, int FI, |
| 114 | const TargetRegisterClass *RC) const { |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 115 | PIC16TargetLowering *PTLI = TM.getTargetLowering(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 116 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 117 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 118 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 119 | const Function *Func = MBB.getParent()->getFunction(); |
| 120 | const std::string FuncName = Func->getName(); |
| 121 | |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 122 | const char *tmpName = createESName(PAN::getTempdataLabel(FuncName)); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 123 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 124 | // 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 Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 128 | BuildMI(MBB, I, DL, get(PIC16::movf), DestReg) |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 129 | .addImm(PTLI->GetTmpOffsetForFI(FI, 1)) |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 130 | .addExternalSymbol(tmpName) |
| 131 | .addImm(1); // Emit banksel for it. |
| 132 | } |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 133 | 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 Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 147 | else |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 148 | LLVM_UNREACHABLE("Can't load this register from stack slot"); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 151 | bool PIC16InstrInfo::copyRegToReg (MachineBasicBlock &MBB, |
| 152 | MachineBasicBlock::iterator I, |
| 153 | unsigned DestReg, unsigned SrcReg, |
| 154 | const TargetRegisterClass *DestRC, |
| 155 | const TargetRegisterClass *SrcRC) const { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 156 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 157 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 158 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 159 | if (DestRC == PIC16::FSR16RegisterClass) { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 160 | BuildMI(MBB, I, DL, get(PIC16::copy_fsr), DestReg).addReg(SrcReg); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 161 | return true; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 164 | if (DestRC == PIC16::GPRRegisterClass) { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 165 | BuildMI(MBB, I, DL, get(PIC16::copy_w), DestReg).addReg(SrcReg); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
| 169 | // Not yet supported. |
| 170 | return false; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool PIC16InstrInfo::isMoveInstr(const MachineInstr &MI, |
Sanjiv Gupta | 2530566 | 2009-01-21 09:02:46 +0000 | [diff] [blame] | 174 | unsigned &SrcReg, unsigned &DestReg, |
| 175 | unsigned &SrcSubIdx, unsigned &DstSubIdx) const { |
| 176 | SrcSubIdx = DstSubIdx = 0; // No sub-registers. |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 177 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 178 | if (MI.getOpcode() == PIC16::copy_fsr |
| 179 | || MI.getOpcode() == PIC16::copy_w) { |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 180 | DestReg = MI.getOperand(0).getReg(); |
| 181 | SrcReg = MI.getOperand(1).getReg(); |
| 182 | return true; |
| 183 | } |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 184 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 185 | return false; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Sanjiv Gupta | 892c8ca | 2009-06-03 15:31:12 +0000 | [diff] [blame] | 188 | /// 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. |
| 194 | unsigned PIC16InstrInfo:: |
| 195 | InsertBranch(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 | } |