LLVM API Documentation

BlackfinInstrInfo.cpp

Go to the documentation of this file.
00001 //===- BlackfinInstrInfo.cpp - Blackfin Instruction Information -*- C++ -*-===//
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 Blackfin implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "BlackfinInstrInfo.h"
00015 #include "BlackfinSubtarget.h"
00016 #include "Blackfin.h"
00017 #include "llvm/ADT/STLExtras.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/CodeGen/MachineRegisterInfo.h"
00020 #include "llvm/CodeGen/MachineInstrBuilder.h"
00021 #include "llvm/Support/ErrorHandling.h"
00022 #include "BlackfinGenInstrInfo.inc"
00023 
00024 using namespace llvm;
00025 
00026 BlackfinInstrInfo::BlackfinInstrInfo(BlackfinSubtarget &ST)
00027   : TargetInstrInfoImpl(BlackfinInsts, array_lengthof(BlackfinInsts)),
00028     RI(ST, *this),
00029     Subtarget(ST) {}
00030 
00031 /// Return true if the instruction is a register to register move and
00032 /// leave the source and dest operands in the passed parameters.
00033 bool BlackfinInstrInfo::isMoveInstr(const MachineInstr &MI,
00034                                     unsigned &SrcReg,
00035                                     unsigned &DstReg,
00036                                     unsigned &SrcSR,
00037                                     unsigned &DstSR) const {
00038   SrcSR = DstSR = 0; // No sub-registers.
00039   switch (MI.getOpcode()) {
00040   case BF::MOVE:
00041   case BF::MOVE_ncccc:
00042   case BF::MOVE_ccncc:
00043   case BF::MOVECC_zext:
00044   case BF::MOVECC_nz:
00045     DstReg = MI.getOperand(0).getReg();
00046     SrcReg = MI.getOperand(1).getReg();
00047     return true;
00048   case BF::SLL16i:
00049     if (MI.getOperand(2).getImm()!=0)
00050       return false;
00051     DstReg = MI.getOperand(0).getReg();
00052     SrcReg = MI.getOperand(1).getReg();
00053     return true;
00054   default:
00055     return false;
00056   }
00057 }
00058 
00059 /// isLoadFromStackSlot - If the specified machine instruction is a direct
00060 /// load from a stack slot, return the virtual or physical register number of
00061 /// the destination along with the FrameIndex of the loaded stack slot.  If
00062 /// not, return 0.  This predicate must return 0 if the instruction has
00063 /// any side effects other than loading from the stack slot.
00064 unsigned BlackfinInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
00065                                                 int &FrameIndex) const {
00066   switch (MI->getOpcode()) {
00067   default: break;
00068   case BF::LOAD32fi:
00069   case BF::LOAD16fi:
00070     if (MI->getOperand(1).isFI() &&
00071         MI->getOperand(2).isImm() &&
00072         MI->getOperand(2).getImm() == 0) {
00073       FrameIndex = MI->getOperand(1).getIndex();
00074       return MI->getOperand(0).getReg();
00075     }
00076     break;
00077   }
00078   return 0;
00079 }
00080 
00081 /// isStoreToStackSlot - If the specified machine instruction is a direct
00082 /// store to a stack slot, return the virtual or physical register number of
00083 /// the source reg along with the FrameIndex of the loaded stack slot.  If
00084 /// not, return 0.  This predicate must return 0 if the instruction has
00085 /// any side effects other than storing to the stack slot.
00086 unsigned BlackfinInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
00087                                                int &FrameIndex) const {
00088   switch (MI->getOpcode()) {
00089   default: break;
00090   case BF::STORE32fi:
00091   case BF::STORE16fi:
00092     if (MI->getOperand(1).isFI() &&
00093         MI->getOperand(2).isImm() &&
00094         MI->getOperand(2).getImm() == 0) {
00095       FrameIndex = MI->getOperand(1).getIndex();
00096       return MI->getOperand(0).getReg();
00097     }
00098     break;
00099   }
00100   return 0;
00101 }
00102 
00103 unsigned BlackfinInstrInfo::
00104 InsertBranch(MachineBasicBlock &MBB,
00105              MachineBasicBlock *TBB,
00106              MachineBasicBlock *FBB,
00107              const SmallVectorImpl<MachineOperand> &Cond) const {
00108   // FIXME this should probably have a DebugLoc operand
00109   DebugLoc dl = DebugLoc::getUnknownLoc();
00110 
00111   // Shouldn't be a fall through.
00112   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00113   assert((Cond.size() == 1 || Cond.size() == 0) &&
00114          "Branch conditions have one component!");
00115 
00116   if (Cond.empty()) {
00117     // Unconditional branch?
00118     assert(!FBB && "Unconditional branch with multiple successors!");
00119     BuildMI(&MBB, dl, get(BF::JUMPa)).addMBB(TBB);
00120     return 1;
00121   }
00122 
00123   // Conditional branch.
00124   llvm_unreachable("Implement conditional branches!");
00125 }
00126 
00127 static bool inClass(const TargetRegisterClass &Test,
00128                     unsigned Reg,
00129                     const TargetRegisterClass *RC) {
00130   if (TargetRegisterInfo::isPhysicalRegister(Reg))
00131     return Test.contains(Reg);
00132   else
00133     return &Test==RC || Test.hasSubClass(RC);
00134 }
00135 
00136 bool BlackfinInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
00137                                      MachineBasicBlock::iterator I,
00138                                      unsigned DestReg,
00139                                      unsigned SrcReg,
00140                                      const TargetRegisterClass *DestRC,
00141                                      const TargetRegisterClass *SrcRC) const {
00142   DebugLoc dl = DebugLoc::getUnknownLoc();
00143 
00144   if (inClass(BF::ALLRegClass, DestReg, DestRC) &&
00145       inClass(BF::ALLRegClass, SrcReg,  SrcRC)) {
00146     BuildMI(MBB, I, dl, get(BF::MOVE), DestReg).addReg(SrcReg);
00147     return true;
00148   }
00149 
00150   if (inClass(BF::D16RegClass, DestReg, DestRC) &&
00151       inClass(BF::D16RegClass, SrcReg,  SrcRC)) {
00152     BuildMI(MBB, I, dl, get(BF::SLL16i), DestReg).addReg(SrcReg).addImm(0);
00153     return true;
00154   }
00155 
00156   if (inClass(BF::AnyCCRegClass, SrcReg, SrcRC) &&
00157       inClass(BF::DRegClass, DestReg, DestRC)) {
00158     if (inClass(BF::NotCCRegClass, SrcReg, SrcRC)) {
00159       BuildMI(MBB, I, dl, get(BF::MOVENCC_z), DestReg).addReg(SrcReg);
00160       BuildMI(MBB, I, dl, get(BF::BITTGL), DestReg).addReg(DestReg).addImm(0);
00161     } else {
00162       BuildMI(MBB, I, dl, get(BF::MOVECC_zext), DestReg).addReg(SrcReg);
00163     }
00164     return true;
00165   }
00166 
00167   if (inClass(BF::AnyCCRegClass, DestReg, DestRC) &&
00168       inClass(BF::DRegClass, SrcReg,  SrcRC)) {
00169     if (inClass(BF::NotCCRegClass, DestReg, DestRC))
00170       BuildMI(MBB, I, dl, get(BF::SETEQri_not), DestReg).addReg(SrcReg);
00171     else
00172       BuildMI(MBB, I, dl, get(BF::MOVECC_nz), DestReg).addReg(SrcReg);
00173     return true;
00174   }
00175 
00176   if (inClass(BF::NotCCRegClass, DestReg, DestRC) &&
00177       inClass(BF::JustCCRegClass, SrcReg,  SrcRC)) {
00178     BuildMI(MBB, I, dl, get(BF::MOVE_ncccc), DestReg).addReg(SrcReg);
00179     return true;
00180   }
00181 
00182   if (inClass(BF::JustCCRegClass, DestReg, DestRC) &&
00183       inClass(BF::NotCCRegClass, SrcReg,  SrcRC)) {
00184     BuildMI(MBB, I, dl, get(BF::MOVE_ccncc), DestReg).addReg(SrcReg);
00185     return true;
00186   }
00187 
00188   llvm_unreachable((std::string("Bad regclasses for reg-to-reg copy: ")+
00189                     SrcRC->getName() + " -> " + DestRC->getName()).c_str());
00190   return false;
00191 }
00192 
00193 void
00194 BlackfinInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
00195                                        MachineBasicBlock::iterator I,
00196                                        unsigned SrcReg,
00197                                        bool isKill,
00198                                        int FI,
00199                                        const TargetRegisterClass *RC) const {
00200   DebugLoc DL = I != MBB.end() ?
00201     I->getDebugLoc() : DebugLoc::getUnknownLoc();
00202 
00203   if (inClass(BF::DPRegClass, SrcReg, RC)) {
00204     BuildMI(MBB, I, DL, get(BF::STORE32fi))
00205       .addReg(SrcReg, getKillRegState(isKill))
00206       .addFrameIndex(FI)
00207       .addImm(0);
00208     return;
00209   }
00210 
00211   if (inClass(BF::D16RegClass, SrcReg, RC)) {
00212     BuildMI(MBB, I, DL, get(BF::STORE16fi))
00213       .addReg(SrcReg, getKillRegState(isKill))
00214       .addFrameIndex(FI)
00215       .addImm(0);
00216     return;
00217   }
00218 
00219   if (inClass(BF::AnyCCRegClass, SrcReg, RC)) {
00220     BuildMI(MBB, I, DL, get(BF::STORE8fi))
00221       .addReg(SrcReg, getKillRegState(isKill))
00222       .addFrameIndex(FI)
00223       .addImm(0);
00224     return;
00225   }
00226 
00227   llvm_unreachable((std::string("Cannot store regclass to stack slot: ")+
00228                     RC->getName()).c_str());
00229 }
00230 
00231 void BlackfinInstrInfo::
00232 storeRegToAddr(MachineFunction &MF,
00233                unsigned SrcReg,
00234                bool isKill,
00235                SmallVectorImpl<MachineOperand> &Addr,
00236                const TargetRegisterClass *RC,
00237                SmallVectorImpl<MachineInstr*> &NewMIs) const {
00238   llvm_unreachable("storeRegToAddr not implemented");
00239 }
00240 
00241 void
00242 BlackfinInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
00243                                         MachineBasicBlock::iterator I,
00244                                         unsigned DestReg,
00245                                         int FI,
00246                                         const TargetRegisterClass *RC) const {
00247   DebugLoc DL = I != MBB.end() ?
00248     I->getDebugLoc() : DebugLoc::getUnknownLoc();
00249   if (inClass(BF::DPRegClass, DestReg, RC)) {
00250     BuildMI(MBB, I, DL, get(BF::LOAD32fi), DestReg)
00251       .addFrameIndex(FI)
00252       .addImm(0);
00253     return;
00254   }
00255 
00256   if (inClass(BF::D16RegClass, DestReg, RC)) {
00257     BuildMI(MBB, I, DL, get(BF::LOAD16fi), DestReg)
00258       .addFrameIndex(FI)
00259       .addImm(0);
00260     return;
00261   }
00262 
00263   if (inClass(BF::AnyCCRegClass, DestReg, RC)) {
00264     BuildMI(MBB, I, DL, get(BF::LOAD8fi), DestReg)
00265       .addFrameIndex(FI)
00266       .addImm(0);
00267     return;
00268   }
00269 
00270   llvm_unreachable("Cannot load regclass from stack slot");
00271 }
00272 
00273 void BlackfinInstrInfo::
00274 loadRegFromAddr(MachineFunction &MF,
00275                 unsigned DestReg,
00276                 SmallVectorImpl<MachineOperand> &Addr,
00277                 const TargetRegisterClass *RC,
00278                 SmallVectorImpl<MachineInstr*> &NewMIs) const {
00279   llvm_unreachable("loadRegFromAddr not implemented");
00280 }