LLVM API Documentation

MipsSERegisterInfo.cpp
Go to the documentation of this file.
00001 //===-- MipsSERegisterInfo.cpp - MIPS32/64 Register 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 MIPS32/64 implementation of the TargetRegisterInfo
00011 // class.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "MipsSERegisterInfo.h"
00016 #include "Mips.h"
00017 #include "MipsAnalyzeImmediate.h"
00018 #include "MipsMachineFunction.h"
00019 #include "MipsSEInstrInfo.h"
00020 #include "MipsSubtarget.h"
00021 #include "llvm/ADT/BitVector.h"
00022 #include "llvm/ADT/STLExtras.h"
00023 #include "llvm/CodeGen/MachineFrameInfo.h"
00024 #include "llvm/CodeGen/MachineFunction.h"
00025 #include "llvm/CodeGen/MachineInstrBuilder.h"
00026 #include "llvm/CodeGen/MachineRegisterInfo.h"
00027 #include "llvm/IR/Constants.h"
00028 #include "llvm/IR/DebugInfo.h"
00029 #include "llvm/IR/Function.h"
00030 #include "llvm/IR/Type.h"
00031 #include "llvm/Support/CommandLine.h"
00032 #include "llvm/Support/Debug.h"
00033 #include "llvm/Support/ErrorHandling.h"
00034 #include "llvm/Support/raw_ostream.h"
00035 #include "llvm/Target/TargetFrameLowering.h"
00036 #include "llvm/Target/TargetInstrInfo.h"
00037 #include "llvm/Target/TargetMachine.h"
00038 #include "llvm/Target/TargetOptions.h"
00039 
00040 using namespace llvm;
00041 
00042 #define DEBUG_TYPE "mips-reg-info"
00043 
00044 MipsSERegisterInfo::MipsSERegisterInfo(const MipsSubtarget &ST)
00045   : MipsRegisterInfo(ST) {}
00046 
00047 bool MipsSERegisterInfo::
00048 requiresRegisterScavenging(const MachineFunction &MF) const {
00049   return true;
00050 }
00051 
00052 bool MipsSERegisterInfo::
00053 requiresFrameIndexScavenging(const MachineFunction &MF) const {
00054   return true;
00055 }
00056 
00057 const TargetRegisterClass *
00058 MipsSERegisterInfo::intRegClass(unsigned Size) const {
00059   if (Size == 4)
00060     return &Mips::GPR32RegClass;
00061 
00062   assert(Size == 8);
00063   return &Mips::GPR64RegClass;
00064 }
00065 
00066 /// Get the size of the offset supported by the given load/store.
00067 /// The result includes the effects of any scale factors applied to the
00068 /// instruction immediate.
00069 static inline unsigned getLoadStoreOffsetSizeInBits(const unsigned Opcode) {
00070   switch (Opcode) {
00071   case Mips::LD_B:
00072   case Mips::ST_B:
00073     return 10;
00074   case Mips::LD_H:
00075   case Mips::ST_H:
00076     return 10 + 1 /* scale factor */;
00077   case Mips::LD_W:
00078   case Mips::ST_W:
00079     return 10 + 2 /* scale factor */;
00080   case Mips::LD_D:
00081   case Mips::ST_D:
00082     return 10 + 3 /* scale factor */;
00083   default:
00084     return 16;
00085   }
00086 }
00087 
00088 /// Get the scale factor applied to the immediate in the given load/store.
00089 static inline unsigned getLoadStoreOffsetAlign(const unsigned Opcode) {
00090   switch (Opcode) {
00091   case Mips::LD_H:
00092   case Mips::ST_H:
00093     return 2;
00094   case Mips::LD_W:
00095   case Mips::ST_W:
00096     return 4;
00097   case Mips::LD_D:
00098   case Mips::ST_D:
00099     return 8;
00100   default:
00101     return 1;
00102   }
00103 }
00104 
00105 void MipsSERegisterInfo::eliminateFI(MachineBasicBlock::iterator II,
00106                                      unsigned OpNo, int FrameIndex,
00107                                      uint64_t StackSize,
00108                                      int64_t SPOffset) const {
00109   MachineInstr &MI = *II;
00110   MachineFunction &MF = *MI.getParent()->getParent();
00111   MachineFrameInfo *MFI = MF.getFrameInfo();
00112   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
00113 
00114   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
00115   int MinCSFI = 0;
00116   int MaxCSFI = -1;
00117 
00118   if (CSI.size()) {
00119     MinCSFI = CSI[0].getFrameIdx();
00120     MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
00121   }
00122 
00123   bool EhDataRegFI = MipsFI->isEhDataRegFI(FrameIndex);
00124 
00125   // The following stack frame objects are always referenced relative to $sp:
00126   //  1. Outgoing arguments.
00127   //  2. Pointer to dynamically allocated stack space.
00128   //  3. Locations for callee-saved registers.
00129   //  4. Locations for eh data registers.
00130   // Everything else is referenced relative to whatever register
00131   // getFrameRegister() returns.
00132   unsigned FrameReg;
00133 
00134   if ((FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI) || EhDataRegFI)
00135     FrameReg = Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP;
00136   else
00137     FrameReg = getFrameRegister(MF);
00138 
00139   // Calculate final offset.
00140   // - There is no need to change the offset if the frame object is one of the
00141   //   following: an outgoing argument, pointer to a dynamically allocated
00142   //   stack space or a $gp restore location,
00143   // - If the frame object is any of the following, its offset must be adjusted
00144   //   by adding the size of the stack:
00145   //   incoming argument, callee-saved register location or local variable.
00146   bool IsKill = false;
00147   int64_t Offset;
00148 
00149   Offset = SPOffset + (int64_t)StackSize;
00150   Offset += MI.getOperand(OpNo + 1).getImm();
00151 
00152   DEBUG(errs() << "Offset     : " << Offset << "\n" << "<--------->\n");
00153 
00154   if (!MI.isDebugValue()) {
00155     // Make sure Offset fits within the field available.
00156     // For MSA instructions, this is a 10-bit signed immediate (scaled by
00157     // element size), otherwise it is a 16-bit signed immediate.
00158     unsigned OffsetBitSize = getLoadStoreOffsetSizeInBits(MI.getOpcode());
00159     unsigned OffsetAlign = getLoadStoreOffsetAlign(MI.getOpcode());
00160 
00161     if (OffsetBitSize < 16 && isInt<16>(Offset) &&
00162         (!isIntN(OffsetBitSize, Offset) ||
00163          OffsetToAlignment(Offset, OffsetAlign) != 0)) {
00164       // If we have an offset that needs to fit into a signed n-bit immediate
00165       // (where n < 16) and doesn't, but does fit into 16-bits then use an ADDiu
00166       MachineBasicBlock &MBB = *MI.getParent();
00167       DebugLoc DL = II->getDebugLoc();
00168       unsigned ADDiu = Subtarget.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
00169       const TargetRegisterClass *RC =
00170           Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
00171       MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
00172       unsigned Reg = RegInfo.createVirtualRegister(RC);
00173       const MipsSEInstrInfo &TII =
00174           *static_cast<const MipsSEInstrInfo *>(
00175               MBB.getParent()->getSubtarget().getInstrInfo());
00176       BuildMI(MBB, II, DL, TII.get(ADDiu), Reg).addReg(FrameReg).addImm(Offset);
00177 
00178       FrameReg = Reg;
00179       Offset = 0;
00180       IsKill = true;
00181     } else if (!isInt<16>(Offset)) {
00182       // Otherwise split the offset into 16-bit pieces and add it in multiple
00183       // instructions.
00184       MachineBasicBlock &MBB = *MI.getParent();
00185       DebugLoc DL = II->getDebugLoc();
00186       unsigned ADDu = Subtarget.isABI_N64() ? Mips::DADDu : Mips::ADDu;
00187       unsigned NewImm = 0;
00188       const MipsSEInstrInfo &TII =
00189           *static_cast<const MipsSEInstrInfo *>(
00190               MBB.getParent()->getSubtarget().getInstrInfo());
00191       unsigned Reg = TII.loadImmediate(Offset, MBB, II, DL,
00192                                        OffsetBitSize == 16 ? &NewImm : nullptr);
00193       BuildMI(MBB, II, DL, TII.get(ADDu), Reg).addReg(FrameReg)
00194         .addReg(Reg, RegState::Kill);
00195 
00196       FrameReg = Reg;
00197       Offset = SignExtend64<16>(NewImm);
00198       IsKill = true;
00199     }
00200   }
00201 
00202   MI.getOperand(OpNo).ChangeToRegister(FrameReg, false, false, IsKill);
00203   MI.getOperand(OpNo + 1).ChangeToImmediate(Offset);
00204 }