LLVM API Documentation

MSP430InstrInfo.cpp
Go to the documentation of this file.
00001 //===-- MSP430InstrInfo.cpp - MSP430 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 MSP430 implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MSP430InstrInfo.h"
00015 #include "MSP430.h"
00016 #include "MSP430MachineFunctionInfo.h"
00017 #include "MSP430TargetMachine.h"
00018 #include "llvm/CodeGen/MachineFrameInfo.h"
00019 #include "llvm/CodeGen/MachineInstrBuilder.h"
00020 #include "llvm/CodeGen/MachineRegisterInfo.h"
00021 #include "llvm/IR/Function.h"
00022 #include "llvm/Support/ErrorHandling.h"
00023 #include "llvm/Support/TargetRegistry.h"
00024 
00025 using namespace llvm;
00026 
00027 #define GET_INSTRINFO_CTOR_DTOR
00028 #include "MSP430GenInstrInfo.inc"
00029 
00030 // Pin the vtable to this file.
00031 void MSP430InstrInfo::anchor() {}
00032 
00033 MSP430InstrInfo::MSP430InstrInfo(MSP430Subtarget &STI)
00034   : MSP430GenInstrInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
00035     RI() {}
00036 
00037 void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
00038                                           MachineBasicBlock::iterator MI,
00039                                     unsigned SrcReg, bool isKill, int FrameIdx,
00040                                           const TargetRegisterClass *RC,
00041                                           const TargetRegisterInfo *TRI) const {
00042   DebugLoc DL;
00043   if (MI != MBB.end()) DL = MI->getDebugLoc();
00044   MachineFunction &MF = *MBB.getParent();
00045   MachineFrameInfo &MFI = *MF.getFrameInfo();
00046 
00047   MachineMemOperand *MMO =
00048     MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
00049                             MachineMemOperand::MOStore,
00050                             MFI.getObjectSize(FrameIdx),
00051                             MFI.getObjectAlignment(FrameIdx));
00052 
00053   if (RC == &MSP430::GR16RegClass)
00054     BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
00055       .addFrameIndex(FrameIdx).addImm(0)
00056       .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
00057   else if (RC == &MSP430::GR8RegClass)
00058     BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
00059       .addFrameIndex(FrameIdx).addImm(0)
00060       .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
00061   else
00062     llvm_unreachable("Cannot store this register to stack slot!");
00063 }
00064 
00065 void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
00066                                            MachineBasicBlock::iterator MI,
00067                                            unsigned DestReg, int FrameIdx,
00068                                            const TargetRegisterClass *RC,
00069                                            const TargetRegisterInfo *TRI) const{
00070   DebugLoc DL;
00071   if (MI != MBB.end()) DL = MI->getDebugLoc();
00072   MachineFunction &MF = *MBB.getParent();
00073   MachineFrameInfo &MFI = *MF.getFrameInfo();
00074 
00075   MachineMemOperand *MMO =
00076     MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
00077                             MachineMemOperand::MOLoad,
00078                             MFI.getObjectSize(FrameIdx),
00079                             MFI.getObjectAlignment(FrameIdx));
00080 
00081   if (RC == &MSP430::GR16RegClass)
00082     BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
00083       .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
00084   else if (RC == &MSP430::GR8RegClass)
00085     BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
00086       .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
00087   else
00088     llvm_unreachable("Cannot store this register to stack slot!");
00089 }
00090 
00091 void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
00092                                   MachineBasicBlock::iterator I, DebugLoc DL,
00093                                   unsigned DestReg, unsigned SrcReg,
00094                                   bool KillSrc) const {
00095   unsigned Opc;
00096   if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
00097     Opc = MSP430::MOV16rr;
00098   else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
00099     Opc = MSP430::MOV8rr;
00100   else
00101     llvm_unreachable("Impossible reg-to-reg copy");
00102 
00103   BuildMI(MBB, I, DL, get(Opc), DestReg)
00104     .addReg(SrcReg, getKillRegState(KillSrc));
00105 }
00106 
00107 unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
00108   MachineBasicBlock::iterator I = MBB.end();
00109   unsigned Count = 0;
00110 
00111   while (I != MBB.begin()) {
00112     --I;
00113     if (I->isDebugValue())
00114       continue;
00115     if (I->getOpcode() != MSP430::JMP &&
00116         I->getOpcode() != MSP430::JCC &&
00117         I->getOpcode() != MSP430::Br &&
00118         I->getOpcode() != MSP430::Bm)
00119       break;
00120     // Remove the branch.
00121     I->eraseFromParent();
00122     I = MBB.end();
00123     ++Count;
00124   }
00125 
00126   return Count;
00127 }
00128 
00129 bool MSP430InstrInfo::
00130 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
00131   assert(Cond.size() == 1 && "Invalid Xbranch condition!");
00132 
00133   MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
00134 
00135   switch (CC) {
00136   default: llvm_unreachable("Invalid branch condition!");
00137   case MSP430CC::COND_E:
00138     CC = MSP430CC::COND_NE;
00139     break;
00140   case MSP430CC::COND_NE:
00141     CC = MSP430CC::COND_E;
00142     break;
00143   case MSP430CC::COND_L:
00144     CC = MSP430CC::COND_GE;
00145     break;
00146   case MSP430CC::COND_GE:
00147     CC = MSP430CC::COND_L;
00148     break;
00149   case MSP430CC::COND_HS:
00150     CC = MSP430CC::COND_LO;
00151     break;
00152   case MSP430CC::COND_LO:
00153     CC = MSP430CC::COND_HS;
00154     break;
00155   }
00156 
00157   Cond[0].setImm(CC);
00158   return false;
00159 }
00160 
00161 bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
00162   if (!MI->isTerminator()) return false;
00163 
00164   // Conditional branch is a special case.
00165   if (MI->isBranch() && !MI->isBarrier())
00166     return true;
00167   if (!MI->isPredicable())
00168     return true;
00169   return !isPredicated(MI);
00170 }
00171 
00172 bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
00173                                     MachineBasicBlock *&TBB,
00174                                     MachineBasicBlock *&FBB,
00175                                     SmallVectorImpl<MachineOperand> &Cond,
00176                                     bool AllowModify) const {
00177   // Start from the bottom of the block and work up, examining the
00178   // terminator instructions.
00179   MachineBasicBlock::iterator I = MBB.end();
00180   while (I != MBB.begin()) {
00181     --I;
00182     if (I->isDebugValue())
00183       continue;
00184 
00185     // Working from the bottom, when we see a non-terminator
00186     // instruction, we're done.
00187     if (!isUnpredicatedTerminator(I))
00188       break;
00189 
00190     // A terminator that isn't a branch can't easily be handled
00191     // by this analysis.
00192     if (!I->isBranch())
00193       return true;
00194 
00195     // Cannot handle indirect branches.
00196     if (I->getOpcode() == MSP430::Br ||
00197         I->getOpcode() == MSP430::Bm)
00198       return true;
00199 
00200     // Handle unconditional branches.
00201     if (I->getOpcode() == MSP430::JMP) {
00202       if (!AllowModify) {
00203         TBB = I->getOperand(0).getMBB();
00204         continue;
00205       }
00206 
00207       // If the block has any instructions after a JMP, delete them.
00208       while (std::next(I) != MBB.end())
00209         std::next(I)->eraseFromParent();
00210       Cond.clear();
00211       FBB = nullptr;
00212 
00213       // Delete the JMP if it's equivalent to a fall-through.
00214       if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
00215         TBB = nullptr;
00216         I->eraseFromParent();
00217         I = MBB.end();
00218         continue;
00219       }
00220 
00221       // TBB is used to indicate the unconditinal destination.
00222       TBB = I->getOperand(0).getMBB();
00223       continue;
00224     }
00225 
00226     // Handle conditional branches.
00227     assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
00228     MSP430CC::CondCodes BranchCode =
00229       static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
00230     if (BranchCode == MSP430CC::COND_INVALID)
00231       return true;  // Can't handle weird stuff.
00232 
00233     // Working from the bottom, handle the first conditional branch.
00234     if (Cond.empty()) {
00235       FBB = TBB;
00236       TBB = I->getOperand(0).getMBB();
00237       Cond.push_back(MachineOperand::CreateImm(BranchCode));
00238       continue;
00239     }
00240 
00241     // Handle subsequent conditional branches. Only handle the case where all
00242     // conditional branches branch to the same destination.
00243     assert(Cond.size() == 1);
00244     assert(TBB);
00245 
00246     // Only handle the case where all conditional branches branch to
00247     // the same destination.
00248     if (TBB != I->getOperand(0).getMBB())
00249       return true;
00250 
00251     MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
00252     // If the conditions are the same, we can leave them alone.
00253     if (OldBranchCode == BranchCode)
00254       continue;
00255 
00256     return true;
00257   }
00258 
00259   return false;
00260 }
00261 
00262 unsigned
00263 MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
00264                               MachineBasicBlock *FBB,
00265                               const SmallVectorImpl<MachineOperand> &Cond,
00266                               DebugLoc DL) const {
00267   // Shouldn't be a fall through.
00268   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00269   assert((Cond.size() == 1 || Cond.size() == 0) &&
00270          "MSP430 branch conditions have one component!");
00271 
00272   if (Cond.empty()) {
00273     // Unconditional branch?
00274     assert(!FBB && "Unconditional branch with multiple successors!");
00275     BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
00276     return 1;
00277   }
00278 
00279   // Conditional branch.
00280   unsigned Count = 0;
00281   BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
00282   ++Count;
00283 
00284   if (FBB) {
00285     // Two-way Conditional branch. Insert the second branch.
00286     BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
00287     ++Count;
00288   }
00289   return Count;
00290 }
00291 
00292 /// GetInstSize - Return the number of bytes of code the specified
00293 /// instruction may be.  This returns the maximum number of bytes.
00294 ///
00295 unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
00296   const MCInstrDesc &Desc = MI->getDesc();
00297 
00298   switch (Desc.TSFlags & MSP430II::SizeMask) {
00299   default:
00300     switch (Desc.getOpcode()) {
00301     default: llvm_unreachable("Unknown instruction size!");
00302     case TargetOpcode::CFI_INSTRUCTION:
00303     case TargetOpcode::EH_LABEL:
00304     case TargetOpcode::IMPLICIT_DEF:
00305     case TargetOpcode::KILL:
00306     case TargetOpcode::DBG_VALUE:
00307       return 0;
00308     case TargetOpcode::INLINEASM: {
00309       const MachineFunction *MF = MI->getParent()->getParent();
00310       const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
00311       return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
00312                                     *MF->getTarget().getMCAsmInfo());
00313     }
00314     }
00315   case MSP430II::SizeSpecial:
00316     switch (MI->getOpcode()) {
00317     default: llvm_unreachable("Unknown instruction size!");
00318     case MSP430::SAR8r1c:
00319     case MSP430::SAR16r1c:
00320       return 4;
00321     }
00322   case MSP430II::Size2Bytes:
00323     return 2;
00324   case MSP430II::Size4Bytes:
00325     return 4;
00326   case MSP430II::Size6Bytes:
00327     return 6;
00328   }
00329 }