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" |
Sanjiv Gupta | 753ec15 | 2009-10-15 19:26:25 +0000 | [diff] [blame] | 15 | #include "PIC16ABINames.h" |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 16 | #include "PIC16InstrInfo.h" |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 17 | #include "PIC16TargetMachine.h" |
| 18 | #include "PIC16GenInstrInfo.inc" |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Duncan Sands | c6dbe7f | 2008-11-28 10:20:03 +0000 | [diff] [blame] | 25 | #include <cstdio> |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 26 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 30 | // FIXME: Add the subtarget support on this constructor. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 31 | PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm) |
| 32 | : TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)), |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 33 | TM(tm), |
| 34 | RegInfo(*this, *TM.getSubtargetImpl()) {} |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 35 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 36 | |
| 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 Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 39 | /// 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 Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 41 | /// any side effects other than storing to the stack slot. |
Sanjiv Gupta | 863d3e9 | 2008-11-19 11:27:59 +0000 | [diff] [blame] | 42 | unsigned PIC16InstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 43 | 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 Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 53 | /// 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 Gupta | 863d3e9 | 2008-11-19 11:27:59 +0000 | [diff] [blame] | 58 | unsigned PIC16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 59 | 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 | |
| 70 | void PIC16InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
| 71 | MachineBasicBlock::iterator I, |
| 72 | unsigned SrcReg, bool isKill, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 73 | const TargetRegisterClass *RC, |
| 74 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 75 | const PIC16TargetLowering *PTLI = TM.getTargetLowering(); |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 76 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 77 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 78 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 79 | const Function *Func = MBB.getParent()->getFunction(); |
| 80 | const std::string FuncName = Func->getName(); |
| 81 | |
Sanjiv Gupta | d49baef | 2010-04-07 03:36:01 +0000 | [diff] [blame] | 82 | const char *tmpName = ESNames::createESName(PAN::getTempdataLabel(FuncName)); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 83 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 84 | // 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 Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 88 | BuildMI(MBB, I, DL, get(PIC16::movwf)) |
Bill Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 89 | .addReg(SrcReg, getKillRegState(isKill)) |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 90 | .addImm(PTLI->GetTmpOffsetForFI(FI, 1, *MBB.getParent())) |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 91 | .addExternalSymbol(tmpName) |
| 92 | .addImm(1); // Emit banksel for it. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 93 | } |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 94 | 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 Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 104 | .addReg(SrcReg, getKillRegState(isKill)) |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 105 | .addImm(PTLI->GetTmpOffsetForFI(FI, 3, *MBB.getParent())) |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 106 | .addExternalSymbol(tmpName) |
| 107 | .addImm(1); // Emit banksel for it. |
| 108 | } |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 109 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 110 | llvm_unreachable("Can't store this register to stack slot"); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 113 | void PIC16InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
| 114 | MachineBasicBlock::iterator I, |
| 115 | unsigned DestReg, int FI, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 116 | const TargetRegisterClass *RC, |
| 117 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 118 | const PIC16TargetLowering *PTLI = TM.getTargetLowering(); |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 119 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 120 | if (I != MBB.end()) DL = I->getDebugLoc(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 121 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 122 | const Function *Func = MBB.getParent()->getFunction(); |
| 123 | const std::string FuncName = Func->getName(); |
| 124 | |
Sanjiv Gupta | d49baef | 2010-04-07 03:36:01 +0000 | [diff] [blame] | 125 | const char *tmpName = ESNames::createESName(PAN::getTempdataLabel(FuncName)); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 126 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 127 | // 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 Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 131 | BuildMI(MBB, I, DL, get(PIC16::movf), DestReg) |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 132 | .addImm(PTLI->GetTmpOffsetForFI(FI, 1, *MBB.getParent())) |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 133 | .addExternalSymbol(tmpName) |
| 134 | .addImm(1); // Emit banksel for it. |
| 135 | } |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 136 | 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 Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 146 | .addImm(PTLI->GetTmpOffsetForFI(FI, 3, *MBB.getParent())) |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 147 | .addExternalSymbol(tmpName) |
| 148 | .addImm(1); // Emit banksel for it. |
| 149 | } |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 150 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 151 | llvm_unreachable("Can't load this register from stack slot"); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Jakob Stoklund Olesen | d86adf3 | 2010-07-11 06:53:33 +0000 | [diff] [blame] | 154 | void PIC16InstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 155 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 156 | unsigned DestReg, unsigned SrcReg, |
| 157 | bool KillSrc) const { |
| 158 | unsigned Opc; |
| 159 | if (PIC16::FSR16RegClass.contains(DestReg, SrcReg)) |
| 160 | Opc = PIC16::copy_fsr; |
| 161 | else if (PIC16::GPRRegClass.contains(DestReg, SrcReg)) |
| 162 | Opc = PIC16::copy_w; |
| 163 | else |
| 164 | llvm_unreachable("Impossible reg-to-reg copy"); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 165 | |
Jakob Stoklund Olesen | d86adf3 | 2010-07-11 06:53:33 +0000 | [diff] [blame] | 166 | BuildMI(MBB, I, DL, get(Opc), DestReg) |
| 167 | .addReg(SrcReg, getKillRegState(KillSrc)); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Sanjiv Gupta | 892c8ca | 2009-06-03 15:31:12 +0000 | [diff] [blame] | 170 | /// InsertBranch - Insert a branch into the end of the specified |
| 171 | /// MachineBasicBlock. This operands to this method are the same as those |
| 172 | /// returned by AnalyzeBranch. This is invoked in cases where AnalyzeBranch |
| 173 | /// returns success and when an unconditional branch (TBB is non-null, FBB is |
| 174 | /// null, Cond is empty) needs to be inserted. It returns the number of |
| 175 | /// instructions inserted. |
| 176 | unsigned PIC16InstrInfo:: |
| 177 | InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 178 | MachineBasicBlock *FBB, |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 179 | const SmallVectorImpl<MachineOperand> &Cond, |
| 180 | DebugLoc DL) const { |
Sanjiv Gupta | 892c8ca | 2009-06-03 15:31:12 +0000 | [diff] [blame] | 181 | // Shouldn't be a fall through. |
| 182 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
| 183 | |
| 184 | if (FBB == 0) { // One way branch. |
| 185 | if (Cond.empty()) { |
| 186 | // Unconditional branch? |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 187 | BuildMI(&MBB, DL, get(PIC16::br_uncond)).addMBB(TBB); |
Sanjiv Gupta | 892c8ca | 2009-06-03 15:31:12 +0000 | [diff] [blame] | 188 | } |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | // FIXME: If the there are some conditions specified then conditional branch |
| 193 | // should be generated. |
| 194 | // For the time being no instruction is being generated therefore |
| 195 | // returning NULL. |
| 196 | return 0; |
| 197 | } |
Sanjiv Gupta | e70b897 | 2009-10-27 17:40:24 +0000 | [diff] [blame] | 198 | |
| 199 | bool PIC16InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
| 200 | MachineBasicBlock *&TBB, |
| 201 | MachineBasicBlock *&FBB, |
| 202 | SmallVectorImpl<MachineOperand> &Cond, |
| 203 | bool AllowModify) const { |
| 204 | MachineBasicBlock::iterator I = MBB.end(); |
| 205 | if (I == MBB.begin()) |
| 206 | return true; |
| 207 | |
| 208 | // Get the terminator instruction. |
| 209 | --I; |
Dale Johannesen | 93d6a7e | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 210 | while (I->isDebugValue()) { |
| 211 | if (I == MBB.begin()) |
| 212 | return true; |
| 213 | --I; |
| 214 | } |
Sanjiv Gupta | e70b897 | 2009-10-27 17:40:24 +0000 | [diff] [blame] | 215 | // Handle unconditional branches. If the unconditional branch's target is |
| 216 | // successor basic block then remove the unconditional branch. |
| 217 | if (I->getOpcode() == PIC16::br_uncond && AllowModify) { |
| 218 | if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) { |
| 219 | TBB = 0; |
| 220 | I->eraseFromParent(); |
| 221 | } |
| 222 | } |
| 223 | return true; |
| 224 | } |