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" |
| 23 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 27 | // FIXME: Add the subtarget support on this constructor. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 28 | PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm) |
| 29 | : TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)), |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 30 | TM(tm), |
| 31 | RegInfo(*this, *TM.getSubtargetImpl()) {} |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 32 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 33 | |
| 34 | /// isStoreToStackSlot - If the specified machine instruction is a direct |
| 35 | /// 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] | 36 | /// the source reg along with the FrameIndex of the loaded stack slot. |
| 37 | /// 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] | 38 | /// any side effects other than storing to the stack slot. |
Sanjiv Gupta | 863d3e9 | 2008-11-19 11:27:59 +0000 | [diff] [blame] | 39 | unsigned PIC16InstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 40 | int &FrameIndex) const { |
| 41 | if (MI->getOpcode() == PIC16::movwf |
| 42 | && MI->getOperand(0).isReg() |
| 43 | && MI->getOperand(1).isSymbol()) { |
| 44 | FrameIndex = MI->getOperand(1).getIndex(); |
| 45 | return MI->getOperand(0).getReg(); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 46 | } |
| 47 | return 0; |
| 48 | } |
| 49 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 50 | /// isLoadFromStackSlot - If the specified machine instruction is a direct |
| 51 | /// load from a stack slot, return the virtual or physical register number of |
| 52 | /// the dest reg along with the FrameIndex of the stack slot. |
| 53 | /// If not, return 0. This predicate must return 0 if the instruction has |
| 54 | /// any side effects other than storing to the stack slot. |
Sanjiv Gupta | 863d3e9 | 2008-11-19 11:27:59 +0000 | [diff] [blame] | 55 | unsigned PIC16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 56 | int &FrameIndex) const { |
| 57 | if (MI->getOpcode() == PIC16::movf |
| 58 | && MI->getOperand(0).isReg() |
| 59 | && MI->getOperand(1).isSymbol()) { |
| 60 | FrameIndex = MI->getOperand(1).getIndex(); |
| 61 | return MI->getOperand(0).getReg(); |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void PIC16InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
| 68 | MachineBasicBlock::iterator I, |
| 69 | unsigned SrcReg, bool isKill, int FI, |
| 70 | const TargetRegisterClass *RC) const { |
| 71 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 72 | const Function *Func = MBB.getParent()->getFunction(); |
| 73 | const std::string FuncName = Func->getName(); |
| 74 | |
| 75 | char *tmpName = new char [strlen(FuncName.c_str()) + 6]; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 76 | sprintf(tmpName, "%s.tmp", FuncName.c_str()); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 77 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 78 | // On the order of operands here: think "movwf SrcReg, tmp_slot, offset". |
| 79 | if (RC == PIC16::GPRRegisterClass) { |
| 80 | //MachineFunction &MF = *MBB.getParent(); |
| 81 | //MachineRegisterInfo &RI = MF.getRegInfo(); |
| 82 | BuildMI(MBB, I, get(PIC16::movwf)) |
| 83 | .addReg(SrcReg, false, false, isKill) |
| 84 | .addImm(FI) |
| 85 | .addExternalSymbol(tmpName) |
| 86 | .addImm(1); // Emit banksel for it. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 87 | } |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 88 | else if (RC == PIC16::FSR16RegisterClass) |
| 89 | assert(0 && "Don't know yet how to store a FSR16 to stack slot"); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 90 | else |
| 91 | assert(0 && "Can't store this register to stack slot"); |
| 92 | } |
| 93 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 94 | void PIC16InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
| 95 | MachineBasicBlock::iterator I, |
| 96 | unsigned DestReg, int FI, |
| 97 | const TargetRegisterClass *RC) const { |
| 98 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 99 | const Function *Func = MBB.getParent()->getFunction(); |
| 100 | const std::string FuncName = Func->getName(); |
| 101 | |
| 102 | char *tmpName = new char [strlen(FuncName.c_str()) + 6]; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 103 | sprintf(tmpName, "%s.tmp", FuncName.c_str()); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 104 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 105 | // On the order of operands here: think "movf FrameIndex, W". |
| 106 | if (RC == PIC16::GPRRegisterClass) { |
| 107 | //MachineFunction &MF = *MBB.getParent(); |
| 108 | //MachineRegisterInfo &RI = MF.getRegInfo(); |
| 109 | BuildMI(MBB, I, get(PIC16::movf), DestReg) |
| 110 | .addImm(FI) |
| 111 | .addExternalSymbol(tmpName) |
| 112 | .addImm(1); // Emit banksel for it. |
| 113 | } |
| 114 | else if (RC == PIC16::FSR16RegisterClass) |
| 115 | assert(0 && "Don't know yet how to load an FSR16 from stack slot"); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 116 | else |
| 117 | assert(0 && "Can't load this register from stack slot"); |
| 118 | } |
| 119 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 120 | bool PIC16InstrInfo::copyRegToReg (MachineBasicBlock &MBB, |
| 121 | MachineBasicBlock::iterator I, |
| 122 | unsigned DestReg, unsigned SrcReg, |
| 123 | const TargetRegisterClass *DestRC, |
| 124 | const TargetRegisterClass *SrcRC) const { |
| 125 | if (DestRC == PIC16::FSR16RegisterClass) { |
| 126 | BuildMI(MBB, I, get(PIC16::copy_fsr), DestReg).addReg(SrcReg); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | |
| 132 | bool PIC16InstrInfo::isMoveInstr(const MachineInstr &MI, |
| 133 | unsigned &SrcReg, |
| 134 | unsigned &DestReg) const { |
| 135 | |
| 136 | if (MI.getOpcode() == PIC16::copy_fsr) { |
| 137 | DestReg = MI.getOperand(0).getReg(); |
| 138 | SrcReg = MI.getOperand(1).getReg(); |
| 139 | return true; |
| 140 | } |
| 141 | return false; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 142 | } |
| 143 | |