LLVM API Documentation

PIC16InstrInfo.cpp

Go to the documentation of this file.
00001 //===- PIC16InstrInfo.cpp - PIC16 Instruction Information -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source 
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the PIC16 implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "PIC16.h"
00015 #include "PIC16InstrInfo.h"
00016 #include "PIC16TargetMachine.h"
00017 #include "PIC16GenInstrInfo.inc"
00018 #include "llvm/Function.h"
00019 #include "llvm/ADT/STLExtras.h"
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/CodeGen/MachineInstrBuilder.h"
00022 #include "llvm/CodeGen/MachineRegisterInfo.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 #include <cstdio>
00025 
00026 
00027 using namespace llvm;
00028 
00029 // FIXME: Add the subtarget support on this constructor.
00030 PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm)
00031   : TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)),
00032     TM(tm), 
00033     RegInfo(*this, *TM.getSubtargetImpl()) {}
00034 
00035 
00036 /// isStoreToStackSlot - If the specified machine instruction is a direct
00037 /// store to a stack slot, return the virtual or physical register number of
00038 /// the source reg along with the FrameIndex of the loaded stack slot.  
00039 /// If not, return 0.  This predicate must return 0 if the instruction has
00040 /// any side effects other than storing to the stack slot.
00041 unsigned PIC16InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
00042                                             int &FrameIndex) const {
00043   if (MI->getOpcode() == PIC16::movwf 
00044       && MI->getOperand(0).isReg()
00045       && MI->getOperand(1).isSymbol()) {
00046     FrameIndex = MI->getOperand(1).getIndex();
00047     return MI->getOperand(0).getReg();
00048   }
00049   return 0;
00050 }
00051 
00052 /// isLoadFromStackSlot - If the specified machine instruction is a direct
00053 /// load from a stack slot, return the virtual or physical register number of
00054 /// the dest reg along with the FrameIndex of the stack slot.  
00055 /// If not, return 0.  This predicate must return 0 if the instruction has
00056 /// any side effects other than storing to the stack slot.
00057 unsigned PIC16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
00058                                             int &FrameIndex) const {
00059   if (MI->getOpcode() == PIC16::movf 
00060       && MI->getOperand(0).isReg()
00061       && MI->getOperand(1).isSymbol()) {
00062     FrameIndex = MI->getOperand(1).getIndex();
00063     return MI->getOperand(0).getReg();
00064   }
00065   return 0;
00066 }
00067 
00068 
00069 void PIC16InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 
00070                                          MachineBasicBlock::iterator I,
00071                                          unsigned SrcReg, bool isKill, int FI,
00072                                          const TargetRegisterClass *RC) const {
00073   PIC16TargetLowering *PTLI = TM.getTargetLowering();
00074   DebugLoc DL = DebugLoc::getUnknownLoc();
00075   if (I != MBB.end()) DL = I->getDebugLoc();
00076 
00077   const Function *Func = MBB.getParent()->getFunction();
00078   const std::string FuncName = Func->getName();
00079 
00080   const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
00081 
00082   // On the order of operands here: think "movwf SrcReg, tmp_slot, offset".
00083   if (RC == PIC16::GPRRegisterClass) {
00084     //MachineFunction &MF = *MBB.getParent();
00085     //MachineRegisterInfo &RI = MF.getRegInfo();
00086     BuildMI(MBB, I, DL, get(PIC16::movwf))
00087       .addReg(SrcReg, getKillRegState(isKill))
00088       .addImm(PTLI->GetTmpOffsetForFI(FI, 1))
00089       .addExternalSymbol(tmpName)
00090       .addImm(1); // Emit banksel for it.
00091   }
00092   else if (RC == PIC16::FSR16RegisterClass) {
00093     // This is a 16-bit register and the frameindex given by llvm is of
00094     // size two here. Break this index N into two zero based indexes and 
00095     // put one into the map. The second one is always obtained by adding 1
00096     // to the first zero based index. In fact it is going to use 3 slots
00097     // as saving FSRs corrupts W also and hence we need to save/restore W also.
00098 
00099     unsigned opcode = (SrcReg == PIC16::FSR0) ? PIC16::save_fsr0 
00100                                                  : PIC16::save_fsr1;
00101     BuildMI(MBB, I, DL, get(opcode))
00102       .addReg(SrcReg, getKillRegState(isKill))
00103       .addImm(PTLI->GetTmpOffsetForFI(FI, 3))
00104       .addExternalSymbol(tmpName)
00105       .addImm(1); // Emit banksel for it.
00106   }
00107   else
00108     llvm_unreachable("Can't store this register to stack slot");
00109 }
00110 
00111 void PIC16InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 
00112                                           MachineBasicBlock::iterator I,
00113                                           unsigned DestReg, int FI,
00114                                           const TargetRegisterClass *RC) const {
00115   PIC16TargetLowering *PTLI = TM.getTargetLowering();
00116   DebugLoc DL = DebugLoc::getUnknownLoc();
00117   if (I != MBB.end()) DL = I->getDebugLoc();
00118 
00119   const Function *Func = MBB.getParent()->getFunction();
00120   const std::string FuncName = Func->getName();
00121 
00122   const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
00123 
00124   // On the order of operands here: think "movf FrameIndex, W".
00125   if (RC == PIC16::GPRRegisterClass) {
00126     //MachineFunction &MF = *MBB.getParent();
00127     //MachineRegisterInfo &RI = MF.getRegInfo();
00128     BuildMI(MBB, I, DL, get(PIC16::movf), DestReg)
00129       .addImm(PTLI->GetTmpOffsetForFI(FI, 1))
00130       .addExternalSymbol(tmpName)
00131       .addImm(1); // Emit banksel for it.
00132   }
00133   else if (RC == PIC16::FSR16RegisterClass) {
00134     // This is a 16-bit register and the frameindex given by llvm is of
00135     // size two here. Break this index N into two zero based indexes and 
00136     // put one into the map. The second one is always obtained by adding 1
00137     // to the first zero based index. In fact it is going to use 3 slots
00138     // as saving FSRs corrupts W also and hence we need to save/restore W also.
00139 
00140     unsigned opcode = (DestReg == PIC16::FSR0) ? PIC16::restore_fsr0 
00141                                                  : PIC16::restore_fsr1;
00142     BuildMI(MBB, I, DL, get(opcode), DestReg)
00143       .addImm(PTLI->GetTmpOffsetForFI(FI, 3))
00144       .addExternalSymbol(tmpName)
00145       .addImm(1); // Emit banksel for it.
00146   }
00147   else
00148     llvm_unreachable("Can't load this register from stack slot");
00149 }
00150 
00151 bool PIC16InstrInfo::copyRegToReg (MachineBasicBlock &MBB,
00152                                    MachineBasicBlock::iterator I,
00153                                    unsigned DestReg, unsigned SrcReg,
00154                                    const TargetRegisterClass *DestRC,
00155                                    const TargetRegisterClass *SrcRC) const {
00156   DebugLoc DL = DebugLoc::getUnknownLoc();
00157   if (I != MBB.end()) DL = I->getDebugLoc();
00158 
00159   if (DestRC == PIC16::FSR16RegisterClass) {
00160     BuildMI(MBB, I, DL, get(PIC16::copy_fsr), DestReg).addReg(SrcReg);
00161     return true;
00162   }
00163 
00164   if (DestRC == PIC16::GPRRegisterClass) {
00165     BuildMI(MBB, I, DL, get(PIC16::copy_w), DestReg).addReg(SrcReg);
00166     return true;
00167   }
00168 
00169   // Not yet supported.
00170   return false;
00171 }
00172 
00173 bool PIC16InstrInfo::isMoveInstr(const MachineInstr &MI,
00174                                  unsigned &SrcReg, unsigned &DestReg,
00175                                  unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
00176   SrcSubIdx = DstSubIdx = 0; // No sub-registers.
00177 
00178   if (MI.getOpcode() == PIC16::copy_fsr
00179       || MI.getOpcode() == PIC16::copy_w) {
00180     DestReg = MI.getOperand(0).getReg();
00181     SrcReg = MI.getOperand(1).getReg();
00182     return true;
00183   }
00184 
00185   return false;
00186 }
00187 
00188 /// InsertBranch - Insert a branch into the end of the specified
00189 /// MachineBasicBlock.  This operands to this method are the same as those
00190 /// returned by AnalyzeBranch.  This is invoked in cases where AnalyzeBranch
00191 /// returns success and when an unconditional branch (TBB is non-null, FBB is
00192 /// null, Cond is empty) needs to be inserted. It returns the number of
00193 /// instructions inserted.
00194 unsigned PIC16InstrInfo::
00195 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, 
00196              MachineBasicBlock *FBB,
00197              const SmallVectorImpl<MachineOperand> &Cond) const {
00198   // Shouldn't be a fall through.
00199   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00200 
00201   if (FBB == 0) { // One way branch.
00202     if (Cond.empty()) {
00203       // Unconditional branch?
00204       DebugLoc dl = DebugLoc::getUnknownLoc();
00205       BuildMI(&MBB, dl, get(PIC16::br_uncond)).addMBB(TBB);
00206     }
00207     return 1;
00208   }
00209 
00210   // FIXME: If the there are some conditions specified then conditional branch
00211   // should be generated.   
00212   // For the time being no instruction is being generated therefore
00213   // returning NULL.
00214   return 0;
00215 }