LLVM API Documentation

XCoreInstrInfo.cpp

Go to the documentation of this file.
00001 //===- XCoreInstrInfo.cpp - XCore 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 XCore implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "XCoreMachineFunctionInfo.h"
00015 #include "XCoreInstrInfo.h"
00016 #include "XCore.h"
00017 #include "llvm/ADT/STLExtras.h"
00018 #include "llvm/CodeGen/MachineInstrBuilder.h"
00019 #include "llvm/CodeGen/MachineFrameInfo.h"
00020 #include "llvm/CodeGen/MachineLocation.h"
00021 #include "llvm/CodeGen/MachineModuleInfo.h"
00022 #include "XCoreGenInstrInfo.inc"
00023 #include "llvm/Support/Debug.h"
00024 #include "llvm/Support/ErrorHandling.h"
00025 
00026 namespace llvm {
00027 namespace XCore {
00028 
00029   // XCore Condition Codes
00030   enum CondCode {
00031     COND_TRUE,
00032     COND_FALSE,
00033     COND_INVALID
00034   };
00035 }
00036 }
00037 
00038 using namespace llvm;
00039 
00040 XCoreInstrInfo::XCoreInstrInfo()
00041   : TargetInstrInfoImpl(XCoreInsts, array_lengthof(XCoreInsts)),
00042     RI(*this) {
00043 }
00044 
00045 static bool isZeroImm(const MachineOperand &op) {
00046   return op.isImm() && op.getImm() == 0;
00047 }
00048 
00049 /// Return true if the instruction is a register to register move and
00050 /// leave the source and dest operands in the passed parameters.
00051 ///
00052 bool XCoreInstrInfo::isMoveInstr(const MachineInstr &MI,
00053                                  unsigned &SrcReg, unsigned &DstReg,
00054                                  unsigned &SrcSR, unsigned &DstSR) const {
00055   SrcSR = DstSR = 0; // No sub-registers.
00056 
00057   // We look for 4 kinds of patterns here:
00058   // add dst, src, 0
00059   // sub dst, src, 0
00060   // or dst, src, src
00061   // and dst, src, src
00062   if ((MI.getOpcode() == XCore::ADD_2rus || MI.getOpcode() == XCore::SUB_2rus)
00063       && isZeroImm(MI.getOperand(2))) {
00064     DstReg = MI.getOperand(0).getReg();
00065     SrcReg = MI.getOperand(1).getReg();
00066     return true;
00067   } else if ((MI.getOpcode() == XCore::OR_3r || MI.getOpcode() == XCore::AND_3r)
00068       && MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
00069     DstReg = MI.getOperand(0).getReg();
00070     SrcReg = MI.getOperand(1).getReg();
00071     return true;
00072   }
00073   return false;
00074 }
00075 
00076 /// isLoadFromStackSlot - If the specified machine instruction is a direct
00077 /// load from a stack slot, return the virtual or physical register number of
00078 /// the destination along with the FrameIndex of the loaded stack slot.  If
00079 /// not, return 0.  This predicate must return 0 if the instruction has
00080 /// any side effects other than loading from the stack slot.
00081 unsigned
00082 XCoreInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const{
00083   int Opcode = MI->getOpcode();
00084   if (Opcode == XCore::LDWFI) 
00085   {
00086     if ((MI->getOperand(1).isFI()) && // is a stack slot
00087         (MI->getOperand(2).isImm()) &&  // the imm is zero
00088         (isZeroImm(MI->getOperand(2)))) 
00089     {
00090       FrameIndex = MI->getOperand(1).getIndex();
00091       return MI->getOperand(0).getReg();
00092     }
00093   }
00094   return 0;
00095 }
00096   
00097   /// isStoreToStackSlot - If the specified machine instruction is a direct
00098   /// store to a stack slot, return the virtual or physical register number of
00099   /// the source reg along with the FrameIndex of the loaded stack slot.  If
00100   /// not, return 0.  This predicate must return 0 if the instruction has
00101   /// any side effects other than storing to the stack slot.
00102 unsigned
00103 XCoreInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
00104                                    int &FrameIndex) const {
00105   int Opcode = MI->getOpcode();
00106   if (Opcode == XCore::STWFI)
00107   {
00108     if ((MI->getOperand(1).isFI()) && // is a stack slot
00109         (MI->getOperand(2).isImm()) &&  // the imm is zero
00110         (isZeroImm(MI->getOperand(2))))
00111     {
00112       FrameIndex = MI->getOperand(1).getIndex();
00113       return MI->getOperand(0).getReg();
00114     }
00115   }
00116   return 0;
00117 }
00118 
00119 /// isInvariantLoad - Return true if the specified instruction (which is marked
00120 /// mayLoad) is loading from a location whose value is invariant across the
00121 /// function.  For example, loading a value from the constant pool or from
00122 /// from the argument area of a function if it does not change.  This should
00123 /// only return true of *all* loads the instruction does are invariant (if it
00124 /// does multiple loads).
00125 bool
00126 XCoreInstrInfo::isInvariantLoad(const MachineInstr *MI) const {
00127   // Loads from constants pools and loads from invariant argument slots are
00128   // invariant
00129   int Opcode = MI->getOpcode();
00130   if (Opcode == XCore::LDWCP_ru6 || Opcode == XCore::LDWCP_lru6) {
00131     return MI->getOperand(1).isCPI();
00132   }
00133   int FrameIndex;
00134   if (isLoadFromStackSlot(MI, FrameIndex)) {
00135     const MachineFrameInfo &MFI =
00136       *MI->getParent()->getParent()->getFrameInfo();
00137     return MFI.isFixedObjectIndex(FrameIndex) &&
00138            MFI.isImmutableObjectIndex(FrameIndex);
00139   }
00140   return false;
00141 }
00142 
00143 //===----------------------------------------------------------------------===//
00144 // Branch Analysis
00145 //===----------------------------------------------------------------------===//
00146 
00147 static inline bool IsBRU(unsigned BrOpc) {
00148   return BrOpc == XCore::BRFU_u6
00149       || BrOpc == XCore::BRFU_lu6
00150       || BrOpc == XCore::BRBU_u6
00151       || BrOpc == XCore::BRBU_lu6;
00152 }
00153 
00154 static inline bool IsBRT(unsigned BrOpc) {
00155   return BrOpc == XCore::BRFT_ru6
00156       || BrOpc == XCore::BRFT_lru6
00157       || BrOpc == XCore::BRBT_ru6
00158       || BrOpc == XCore::BRBT_lru6;
00159 }
00160 
00161 static inline bool IsBRF(unsigned BrOpc) {
00162   return BrOpc == XCore::BRFF_ru6
00163       || BrOpc == XCore::BRFF_lru6
00164       || BrOpc == XCore::BRBF_ru6
00165       || BrOpc == XCore::BRBF_lru6;
00166 }
00167 
00168 static inline bool IsCondBranch(unsigned BrOpc) {
00169   return IsBRF(BrOpc) || IsBRT(BrOpc);
00170 }
00171 
00172 /// GetCondFromBranchOpc - Return the XCore CC that matches 
00173 /// the correspondent Branch instruction opcode.
00174 static XCore::CondCode GetCondFromBranchOpc(unsigned BrOpc) 
00175 {
00176   if (IsBRT(BrOpc)) {
00177     return XCore::COND_TRUE;
00178   } else if (IsBRF(BrOpc)) {
00179     return XCore::COND_FALSE;
00180   } else {
00181     return XCore::COND_INVALID;
00182   }
00183 }
00184 
00185 /// GetCondBranchFromCond - Return the Branch instruction
00186 /// opcode that matches the cc.
00187 static inline unsigned GetCondBranchFromCond(XCore::CondCode CC) 
00188 {
00189   switch (CC) {
00190   default: llvm_unreachable("Illegal condition code!");
00191   case XCore::COND_TRUE   : return XCore::BRFT_lru6;
00192   case XCore::COND_FALSE  : return XCore::BRFF_lru6;
00193   }
00194 }
00195 
00196 /// GetOppositeBranchCondition - Return the inverse of the specified 
00197 /// condition, e.g. turning COND_E to COND_NE.
00198 static inline XCore::CondCode GetOppositeBranchCondition(XCore::CondCode CC)
00199 {
00200   switch (CC) {
00201   default: llvm_unreachable("Illegal condition code!");
00202   case XCore::COND_TRUE   : return XCore::COND_FALSE;
00203   case XCore::COND_FALSE  : return XCore::COND_TRUE;
00204   }
00205 }
00206 
00207 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
00208 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
00209 /// implemented for a target).  Upon success, this returns false and returns
00210 /// with the following information in various cases:
00211 ///
00212 /// 1. If this block ends with no branches (it just falls through to its succ)
00213 ///    just return false, leaving TBB/FBB null.
00214 /// 2. If this block ends with only an unconditional branch, it sets TBB to be
00215 ///    the destination block.
00216 /// 3. If this block ends with an conditional branch and it falls through to
00217 ///    an successor block, it sets TBB to be the branch destination block and a
00218 ///    list of operands that evaluate the condition. These
00219 ///    operands can be passed to other TargetInstrInfo methods to create new
00220 ///    branches.
00221 /// 4. If this block ends with an conditional branch and an unconditional
00222 ///    block, it returns the 'true' destination in TBB, the 'false' destination
00223 ///    in FBB, and a list of operands that evaluate the condition. These
00224 ///    operands can be passed to other TargetInstrInfo methods to create new
00225 ///    branches.
00226 ///
00227 /// Note that RemoveBranch and InsertBranch must be implemented to support
00228 /// cases where this method returns success.
00229 ///
00230 bool
00231 XCoreInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
00232                               MachineBasicBlock *&FBB,
00233                               SmallVectorImpl<MachineOperand> &Cond,
00234                               bool AllowModify) const {
00235   // If the block has no terminators, it just falls into the block after it.
00236   MachineBasicBlock::iterator I = MBB.end();
00237   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
00238     return false;
00239 
00240   // Get the last instruction in the block.
00241   MachineInstr *LastInst = I;
00242   
00243   // If there is only one terminator instruction, process it.
00244   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
00245     if (IsBRU(LastInst->getOpcode())) {
00246       TBB = LastInst->getOperand(0).getMBB();
00247       return false;
00248     }
00249     
00250     XCore::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
00251     if (BranchCode == XCore::COND_INVALID)
00252       return true;  // Can't handle indirect branch.
00253     
00254     // Conditional branch
00255     // Block ends with fall-through condbranch.
00256 
00257     TBB = LastInst->getOperand(1).getMBB();
00258     Cond.push_back(MachineOperand::CreateImm(BranchCode));
00259     Cond.push_back(LastInst->getOperand(0));
00260     return false;
00261   }
00262   
00263   // Get the instruction before it if it's a terminator.
00264   MachineInstr *SecondLastInst = I;
00265 
00266   // If there are three terminators, we don't know what sort of block this is.
00267   if (SecondLastInst && I != MBB.begin() &&
00268       isUnpredicatedTerminator(--I))
00269     return true;
00270   
00271   unsigned SecondLastOpc    = SecondLastInst->getOpcode();
00272   XCore::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
00273   
00274   // If the block ends with conditional branch followed by unconditional,
00275   // handle it.
00276   if (BranchCode != XCore::COND_INVALID
00277     && IsBRU(LastInst->getOpcode())) {
00278 
00279     TBB = SecondLastInst->getOperand(1).getMBB();
00280     Cond.push_back(MachineOperand::CreateImm(BranchCode));
00281     Cond.push_back(SecondLastInst->getOperand(0));
00282 
00283     FBB = LastInst->getOperand(0).getMBB();
00284     return false;
00285   }
00286   
00287   // If the block ends with two unconditional branches, handle it.  The second
00288   // one is not executed, so remove it.
00289   if (IsBRU(SecondLastInst->getOpcode()) && 
00290       IsBRU(LastInst->getOpcode())) {
00291     TBB = SecondLastInst->getOperand(0).getMBB();
00292     I = LastInst;
00293     if (AllowModify)
00294       I->eraseFromParent();
00295     return false;
00296   }
00297 
00298   // Otherwise, can't handle this.
00299   return true;
00300 }
00301 
00302 unsigned
00303 XCoreInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
00304                              MachineBasicBlock *FBB,
00305                              const SmallVectorImpl<MachineOperand> &Cond)const{
00306   // FIXME there should probably be a DebugLoc argument here
00307   DebugLoc dl = DebugLoc::getUnknownLoc();
00308   // Shouldn't be a fall through.
00309   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
00310   assert((Cond.size() == 2 || Cond.size() == 0) &&
00311          "Unexpected number of components!");
00312   
00313   if (FBB == 0) { // One way branch.
00314     if (Cond.empty()) {
00315       // Unconditional branch
00316       BuildMI(&MBB, dl, get(XCore::BRFU_lu6)).addMBB(TBB);
00317     } else {
00318       // Conditional branch.
00319       unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
00320       BuildMI(&MBB, dl, get(Opc)).addReg(Cond[1].getReg())
00321                              .addMBB(TBB);
00322     }
00323     return 1;
00324   }
00325   
00326   // Two-way Conditional branch.
00327   assert(Cond.size() == 2 && "Unexpected number of components!");
00328   unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm());
00329   BuildMI(&MBB, dl, get(Opc)).addReg(Cond[1].getReg())
00330                          .addMBB(TBB);
00331   BuildMI(&MBB, dl, get(XCore::BRFU_lu6)).addMBB(FBB);
00332   return 2;
00333 }
00334 
00335 unsigned
00336 XCoreInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
00337   MachineBasicBlock::iterator I = MBB.end();
00338   if (I == MBB.begin()) return 0;
00339   --I;
00340   if (!IsBRU(I->getOpcode()) && !IsCondBranch(I->getOpcode()))
00341     return 0;
00342   
00343   // Remove the branch.
00344   I->eraseFromParent();
00345   
00346   I = MBB.end();
00347 
00348   if (I == MBB.begin()) return 1;
00349   --I;
00350   if (!IsCondBranch(I->getOpcode()))
00351     return 1;
00352   
00353   // Remove the branch.
00354   I->eraseFromParent();
00355   return 2;
00356 }
00357 
00358 bool XCoreInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
00359                                   MachineBasicBlock::iterator I,
00360                                   unsigned DestReg, unsigned SrcReg,
00361                                   const TargetRegisterClass *DestRC,
00362                                   const TargetRegisterClass *SrcRC) const {
00363   DebugLoc DL = DebugLoc::getUnknownLoc();
00364   if (I != MBB.end()) DL = I->getDebugLoc();
00365 
00366   if (DestRC == SrcRC) {
00367     if (DestRC == XCore::GRRegsRegisterClass) {
00368       BuildMI(MBB, I, DL, get(XCore::ADD_2rus), DestReg)
00369         .addReg(SrcReg)
00370         .addImm(0);
00371       return true;
00372     } else {
00373       return false;
00374     }
00375   }
00376   
00377   if (SrcRC == XCore::RRegsRegisterClass && SrcReg == XCore::SP &&
00378     DestRC == XCore::GRRegsRegisterClass) {
00379     BuildMI(MBB, I, DL, get(XCore::LDAWSP_ru6), DestReg)
00380       .addImm(0);
00381     return true;
00382   }
00383   if (DestRC == XCore::RRegsRegisterClass && DestReg == XCore::SP &&
00384     SrcRC == XCore::GRRegsRegisterClass) {
00385     BuildMI(MBB, I, DL, get(XCore::SETSP_1r))
00386       .addReg(SrcReg);
00387     return true;
00388   }
00389   return false;
00390 }
00391 
00392 void XCoreInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
00393                                          MachineBasicBlock::iterator I,
00394                                          unsigned SrcReg, bool isKill,
00395                                          int FrameIndex,
00396                                          const TargetRegisterClass *RC) const
00397 {
00398   DebugLoc DL = DebugLoc::getUnknownLoc();
00399   if (I != MBB.end()) DL = I->getDebugLoc();
00400   BuildMI(MBB, I, DL, get(XCore::STWFI))
00401     .addReg(SrcReg, getKillRegState(isKill))
00402     .addFrameIndex(FrameIndex)
00403     .addImm(0);
00404 }
00405 
00406 void XCoreInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
00407                                           MachineBasicBlock::iterator I,
00408                                           unsigned DestReg, int FrameIndex,
00409                                           const TargetRegisterClass *RC) const
00410 {
00411   DebugLoc DL = DebugLoc::getUnknownLoc();
00412   if (I != MBB.end()) DL = I->getDebugLoc();
00413   BuildMI(MBB, I, DL, get(XCore::LDWFI), DestReg)
00414     .addFrameIndex(FrameIndex)
00415     .addImm(0);
00416 }
00417 
00418 bool XCoreInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
00419                                                MachineBasicBlock::iterator MI,
00420                                   const std::vector<CalleeSavedInfo> &CSI) const
00421 {
00422   if (CSI.empty()) {
00423     return true;
00424   }
00425   MachineFunction *MF = MBB.getParent();
00426   const MachineFrameInfo *MFI = MF->getFrameInfo();
00427   MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
00428   XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
00429   
00430   bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
00431 
00432   DebugLoc DL = DebugLoc::getUnknownLoc();
00433   if (MI != MBB.end()) DL = MI->getDebugLoc();
00434   
00435   for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
00436                                                     it != CSI.end(); ++it) {
00437     // Add the callee-saved register as live-in. It's killed at the spill.
00438     MBB.addLiveIn(it->getReg());
00439 
00440     storeRegToStackSlot(MBB, MI, it->getReg(), true,
00441                         it->getFrameIdx(), it->getRegClass());
00442     if (emitFrameMoves) {
00443       unsigned SaveLabelId = MMI->NextLabelID();
00444       BuildMI(MBB, MI, DL, get(XCore::DBG_LABEL)).addImm(SaveLabelId);
00445       XFI->getSpillLabels().push_back(
00446           std::pair<unsigned, CalleeSavedInfo>(SaveLabelId, *it));
00447     }
00448   }
00449   return true;
00450 }
00451 
00452 bool XCoreInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
00453                                          MachineBasicBlock::iterator MI,
00454                                const std::vector<CalleeSavedInfo> &CSI) const
00455 {
00456   bool AtStart = MI == MBB.begin();
00457   MachineBasicBlock::iterator BeforeI = MI;
00458   if (!AtStart)
00459     --BeforeI;
00460   for (std::vector<CalleeSavedInfo>::const_iterator it = CSI.begin();
00461                                                     it != CSI.end(); ++it) {
00462     
00463     loadRegFromStackSlot(MBB, MI, it->getReg(),
00464                                   it->getFrameIdx(),
00465                                   it->getRegClass());
00466     assert(MI != MBB.begin() &&
00467            "loadRegFromStackSlot didn't insert any code!");
00468     // Insert in reverse order.  loadRegFromStackSlot can insert multiple
00469     // instructions.
00470     if (AtStart)
00471       MI = MBB.begin();
00472     else {
00473       MI = BeforeI;
00474       ++MI;
00475     }
00476   }
00477   return true;
00478 }
00479 
00480 /// BlockHasNoFallThrough - Analyse if MachineBasicBlock does not
00481 /// fall-through into its successor block.
00482 bool XCoreInstrInfo::
00483 BlockHasNoFallThrough(const MachineBasicBlock &MBB) const 
00484 {
00485   if (MBB.empty()) return false;
00486   
00487   switch (MBB.back().getOpcode()) {
00488   case XCore::RETSP_u6:     // Return.
00489   case XCore::RETSP_lu6:
00490   case XCore::BAU_1r:       // Indirect branch.
00491   case XCore::BRFU_u6:      // Uncond branch.
00492   case XCore::BRFU_lu6:
00493   case XCore::BRBU_u6:
00494   case XCore::BRBU_lu6:
00495     return true;
00496   default: return false;
00497   }
00498 }
00499 
00500 /// ReverseBranchCondition - Return the inverse opcode of the 
00501 /// specified Branch instruction.
00502 bool XCoreInstrInfo::
00503 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const 
00504 {
00505   assert((Cond.size() == 2) && 
00506           "Invalid XCore branch condition!");
00507   Cond[0].setImm(GetOppositeBranchCondition((XCore::CondCode)Cond[0].getImm()));
00508   return false;
00509 }