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" |
Duncan Sands | c6dbe7f | 2008-11-28 10:20:03 +0000 | [diff] [blame] | 23 | #include <cstdio> |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 24 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 28 | // FIXME: Add the subtarget support on this constructor. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 29 | PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm) |
| 30 | : TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)), |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 31 | TM(tm), |
| 32 | RegInfo(*this, *TM.getSubtargetImpl()) {} |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 33 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 34 | |
| 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 Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 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 |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 39 | /// any side effects other than storing to the stack slot. |
Sanjiv Gupta | 863d3e9 | 2008-11-19 11:27:59 +0000 | [diff] [blame] | 40 | unsigned PIC16InstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 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(); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 47 | } |
| 48 | return 0; |
| 49 | } |
| 50 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 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. |
Sanjiv Gupta | 863d3e9 | 2008-11-19 11:27:59 +0000 | [diff] [blame] | 56 | unsigned PIC16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 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(); |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | void PIC16InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
| 69 | MachineBasicBlock::iterator I, |
| 70 | unsigned SrcReg, bool isKill, int FI, |
| 71 | const TargetRegisterClass *RC) const { |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 72 | PIC16TargetLowering *PTLI = TM.getTargetLowering(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 73 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 74 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 75 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 76 | const Function *Func = MBB.getParent()->getFunction(); |
| 77 | const std::string FuncName = Func->getName(); |
| 78 | |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 79 | const char *tmpName = createESName(PAN::getTempdataLabel(FuncName)); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 80 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 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(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 85 | BuildMI(MBB, I, DL, get(PIC16::movwf)) |
Bill Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 86 | .addReg(SrcReg, getKillRegState(isKill)) |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 87 | .addImm(PTLI->GetTmpOffsetForFI(FI, 1)) |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 88 | .addExternalSymbol(tmpName) |
| 89 | .addImm(1); // Emit banksel for it. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 90 | } |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 91 | else if (RC == PIC16::FSR16RegisterClass) { |
| 92 | // This is a 16-bit register and the frameindex given by llvm is of |
| 93 | // size two here. Break this index N into two zero based indexes and |
| 94 | // put one into the map. The second one is always obtained by adding 1 |
| 95 | // to the first zero based index. In fact it is going to use 3 slots |
| 96 | // as saving FSRs corrupts W also and hence we need to save/restore W also. |
| 97 | |
| 98 | unsigned opcode = (SrcReg == PIC16::FSR0) ? PIC16::save_fsr0 |
| 99 | : PIC16::save_fsr1; |
| 100 | BuildMI(MBB, I, DL, get(opcode)) |
Bill Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 101 | .addReg(SrcReg, getKillRegState(isKill)) |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 102 | .addImm(PTLI->GetTmpOffsetForFI(FI, 3)) |
| 103 | .addExternalSymbol(tmpName) |
| 104 | .addImm(1); // Emit banksel for it. |
| 105 | } |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 106 | else |
| 107 | assert(0 && "Can't store this register to stack slot"); |
| 108 | } |
| 109 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 110 | void PIC16InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
| 111 | MachineBasicBlock::iterator I, |
| 112 | unsigned DestReg, int FI, |
| 113 | const TargetRegisterClass *RC) const { |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 114 | PIC16TargetLowering *PTLI = TM.getTargetLowering(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 115 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 116 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 117 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 118 | const Function *Func = MBB.getParent()->getFunction(); |
| 119 | const std::string FuncName = Func->getName(); |
| 120 | |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 121 | const char *tmpName = createESName(PAN::getTempdataLabel(FuncName)); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 122 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 123 | // On the order of operands here: think "movf FrameIndex, W". |
| 124 | if (RC == PIC16::GPRRegisterClass) { |
| 125 | //MachineFunction &MF = *MBB.getParent(); |
| 126 | //MachineRegisterInfo &RI = MF.getRegInfo(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 127 | BuildMI(MBB, I, DL, get(PIC16::movf), DestReg) |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 128 | .addImm(PTLI->GetTmpOffsetForFI(FI, 1)) |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 129 | .addExternalSymbol(tmpName) |
| 130 | .addImm(1); // Emit banksel for it. |
| 131 | } |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 132 | else if (RC == PIC16::FSR16RegisterClass) { |
| 133 | // This is a 16-bit register and the frameindex given by llvm is of |
| 134 | // size two here. Break this index N into two zero based indexes and |
| 135 | // put one into the map. The second one is always obtained by adding 1 |
| 136 | // to the first zero based index. In fact it is going to use 3 slots |
| 137 | // as saving FSRs corrupts W also and hence we need to save/restore W also. |
| 138 | |
| 139 | unsigned opcode = (DestReg == PIC16::FSR0) ? PIC16::restore_fsr0 |
| 140 | : PIC16::restore_fsr1; |
| 141 | BuildMI(MBB, I, DL, get(opcode), DestReg) |
| 142 | .addImm(PTLI->GetTmpOffsetForFI(FI, 3)) |
| 143 | .addExternalSymbol(tmpName) |
| 144 | .addImm(1); // Emit banksel for it. |
| 145 | } |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 146 | else |
| 147 | assert(0 && "Can't load this register from stack slot"); |
| 148 | } |
| 149 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 150 | bool PIC16InstrInfo::copyRegToReg (MachineBasicBlock &MBB, |
| 151 | MachineBasicBlock::iterator I, |
| 152 | unsigned DestReg, unsigned SrcReg, |
| 153 | const TargetRegisterClass *DestRC, |
| 154 | const TargetRegisterClass *SrcRC) const { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 155 | DebugLoc DL = DebugLoc::getUnknownLoc(); |
| 156 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 157 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 158 | if (DestRC == PIC16::FSR16RegisterClass) { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 159 | BuildMI(MBB, I, DL, get(PIC16::copy_fsr), DestReg).addReg(SrcReg); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 160 | return true; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 163 | if (DestRC == PIC16::GPRRegisterClass) { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 164 | BuildMI(MBB, I, DL, get(PIC16::copy_w), DestReg).addReg(SrcReg); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
| 168 | // Not yet supported. |
| 169 | return false; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | bool PIC16InstrInfo::isMoveInstr(const MachineInstr &MI, |
Sanjiv Gupta | 2530566 | 2009-01-21 09:02:46 +0000 | [diff] [blame] | 173 | unsigned &SrcReg, unsigned &DestReg, |
| 174 | unsigned &SrcSubIdx, unsigned &DstSubIdx) const { |
| 175 | SrcSubIdx = DstSubIdx = 0; // No sub-registers. |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 176 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 177 | if (MI.getOpcode() == PIC16::copy_fsr |
| 178 | || MI.getOpcode() == PIC16::copy_w) { |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 179 | DestReg = MI.getOperand(0).getReg(); |
| 180 | SrcReg = MI.getOperand(1).getReg(); |
| 181 | return true; |
| 182 | } |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 183 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 184 | return false; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Sanjiv Gupta | 892c8ca | 2009-06-03 15:31:12 +0000 | [diff] [blame] | 187 | /// InsertBranch - Insert a branch into the end of the specified |
| 188 | /// MachineBasicBlock. This operands to this method are the same as those |
| 189 | /// returned by AnalyzeBranch. This is invoked in cases where AnalyzeBranch |
| 190 | /// returns success and when an unconditional branch (TBB is non-null, FBB is |
| 191 | /// null, Cond is empty) needs to be inserted. It returns the number of |
| 192 | /// instructions inserted. |
| 193 | unsigned PIC16InstrInfo:: |
| 194 | InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 195 | MachineBasicBlock *FBB, |
| 196 | const SmallVectorImpl<MachineOperand> &Cond) const { |
| 197 | // Shouldn't be a fall through. |
| 198 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
| 199 | |
| 200 | if (FBB == 0) { // One way branch. |
| 201 | if (Cond.empty()) { |
| 202 | // Unconditional branch? |
| 203 | DebugLoc dl = DebugLoc::getUnknownLoc(); |
| 204 | BuildMI(&MBB, dl, get(PIC16::br_uncond)).addMBB(TBB); |
| 205 | } |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | // FIXME: If the there are some conditions specified then conditional branch |
| 210 | // should be generated. |
| 211 | // For the time being no instruction is being generated therefore |
| 212 | // returning NULL. |
| 213 | return 0; |
| 214 | } |