LLVM API Documentation

LowerSubregs.cpp

Go to the documentation of this file.
00001 //===-- LowerSubregs.cpp - Subregister Lowering instruction pass ----------===//
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 defines a MachineFunction pass which runs after register
00011 // allocation that turns subreg insert/extract instructions into register
00012 // copies, as needed. This ensures correct codegen even if the coalescer
00013 // isn't able to remove all subreg instructions.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #define DEBUG_TYPE "lowersubregs"
00018 #include "llvm/CodeGen/Passes.h"
00019 #include "llvm/Function.h"
00020 #include "llvm/CodeGen/MachineFunctionPass.h"
00021 #include "llvm/CodeGen/MachineInstr.h"
00022 #include "llvm/CodeGen/MachineInstrBuilder.h"
00023 #include "llvm/CodeGen/MachineRegisterInfo.h"
00024 #include "llvm/Target/TargetRegisterInfo.h"
00025 #include "llvm/Target/TargetInstrInfo.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include "llvm/Support/Debug.h"
00028 #include "llvm/Support/Compiler.h"
00029 #include "llvm/Support/raw_ostream.h"
00030 using namespace llvm;
00031 
00032 namespace {
00033   struct VISIBILITY_HIDDEN LowerSubregsInstructionPass
00034    : public MachineFunctionPass {
00035     static char ID; // Pass identification, replacement for typeid
00036     LowerSubregsInstructionPass() : MachineFunctionPass(&ID) {}
00037     
00038     const char *getPassName() const {
00039       return "Subregister lowering instruction pass";
00040     }
00041 
00042     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00043       AU.setPreservesCFG();
00044       AU.addPreservedID(MachineLoopInfoID);
00045       AU.addPreservedID(MachineDominatorsID);
00046       MachineFunctionPass::getAnalysisUsage(AU);
00047     }
00048 
00049     /// runOnMachineFunction - pass entry point
00050     bool runOnMachineFunction(MachineFunction&);
00051     
00052     bool LowerExtract(MachineInstr *MI);
00053     bool LowerInsert(MachineInstr *MI);
00054     bool LowerSubregToReg(MachineInstr *MI);
00055 
00056     void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
00057                           const TargetRegisterInfo &TRI);
00058     void TransferKillFlag(MachineInstr *MI, unsigned SrcReg,
00059                           const TargetRegisterInfo &TRI,
00060                           bool AddIfNotFound = false);
00061   };
00062 
00063   char LowerSubregsInstructionPass::ID = 0;
00064 }
00065 
00066 FunctionPass *llvm::createLowerSubregsPass() { 
00067   return new LowerSubregsInstructionPass(); 
00068 }
00069 
00070 /// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead,
00071 /// and the lowered replacement instructions immediately precede it.
00072 /// Mark the replacement instructions with the dead flag.
00073 void
00074 LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr *MI,
00075                                               unsigned DstReg,
00076                                               const TargetRegisterInfo &TRI) {
00077   for (MachineBasicBlock::iterator MII =
00078         prior(MachineBasicBlock::iterator(MI)); ; --MII) {
00079     if (MII->addRegisterDead(DstReg, &TRI))
00080       break;
00081     assert(MII != MI->getParent()->begin() &&
00082            "copyRegToReg output doesn't reference destination register!");
00083   }
00084 }
00085 
00086 /// TransferKillFlag - MI is a pseudo-instruction with SrcReg killed,
00087 /// and the lowered replacement instructions immediately precede it.
00088 /// Mark the replacement instructions with the kill flag.
00089 void
00090 LowerSubregsInstructionPass::TransferKillFlag(MachineInstr *MI,
00091                                               unsigned SrcReg,
00092                                               const TargetRegisterInfo &TRI,
00093                                               bool AddIfNotFound) {
00094   for (MachineBasicBlock::iterator MII =
00095         prior(MachineBasicBlock::iterator(MI)); ; --MII) {
00096     if (MII->addRegisterKilled(SrcReg, &TRI, AddIfNotFound))
00097       break;
00098     assert(MII != MI->getParent()->begin() &&
00099            "copyRegToReg output doesn't reference source register!");
00100   }
00101 }
00102 
00103 bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) {
00104   MachineBasicBlock *MBB = MI->getParent();
00105   MachineFunction &MF = *MBB->getParent();
00106   const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
00107   const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
00108 
00109   assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
00110          MI->getOperand(1).isReg() && MI->getOperand(1).isUse() &&
00111          MI->getOperand(2).isImm() && "Malformed extract_subreg");
00112 
00113   unsigned DstReg   = MI->getOperand(0).getReg();
00114   unsigned SuperReg = MI->getOperand(1).getReg();
00115   unsigned SubIdx   = MI->getOperand(2).getImm();
00116   unsigned SrcReg   = TRI.getSubReg(SuperReg, SubIdx);
00117 
00118   assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) &&
00119          "Extract supperg source must be a physical register");
00120   assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
00121          "Extract destination must be in a physical register");
00122   assert(SrcReg && "invalid subregister index for register");
00123 
00124   DOUT << "subreg: CONVERTING: " << *MI;
00125 
00126   if (SrcReg == DstReg) {
00127     // No need to insert an identity copy instruction.
00128     if (MI->getOperand(1).isKill()) {
00129       // We must make sure the super-register gets killed.Replace the
00130       // instruction with IMPLICIT_DEF.
00131       MI->setDesc(TII.get(TargetInstrInfo::IMPLICIT_DEF));
00132       MI->RemoveOperand(2);     // SubIdx
00133       DOUT << "subreg: replace by: " << *MI;
00134       return true;
00135     }
00136     DOUT << "subreg: eliminated!";
00137   } else {
00138     // Insert copy
00139     const TargetRegisterClass *TRCS = TRI.getPhysicalRegisterRegClass(DstReg);
00140     const TargetRegisterClass *TRCD = TRI.getPhysicalRegisterRegClass(SrcReg);
00141     bool Emitted = TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRCD, TRCS);
00142     (void)Emitted;
00143     assert(Emitted && "Subreg and Dst must be of compatible register class");
00144     // Transfer the kill/dead flags, if needed.
00145     if (MI->getOperand(0).isDead())
00146       TransferDeadFlag(MI, DstReg, TRI);
00147     if (MI->getOperand(1).isKill())
00148       TransferKillFlag(MI, SuperReg, TRI, true);
00149 
00150 #ifndef NDEBUG
00151     MachineBasicBlock::iterator dMI = MI;
00152     DOUT << "subreg: " << *(--dMI);
00153 #endif
00154   }
00155 
00156   DOUT << "\n";
00157   MBB->erase(MI);
00158   return true;
00159 }
00160 
00161 bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr *MI) {
00162   MachineBasicBlock *MBB = MI->getParent();
00163   MachineFunction &MF = *MBB->getParent();
00164   const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo(); 
00165   const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
00166   assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
00167          MI->getOperand(1).isImm() &&
00168          (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
00169           MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
00170           
00171   unsigned DstReg  = MI->getOperand(0).getReg();
00172   unsigned InsReg  = MI->getOperand(2).getReg();
00173   unsigned InsSIdx = MI->getOperand(2).getSubReg();
00174   unsigned SubIdx  = MI->getOperand(3).getImm();
00175 
00176   assert(SubIdx != 0 && "Invalid index for insert_subreg");
00177   unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx);
00178 
00179   assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
00180          "Insert destination must be in a physical register");
00181   assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
00182          "Inserted value must be in a physical register");
00183 
00184   DOUT << "subreg: CONVERTING: " << *MI;
00185 
00186   if (DstSubReg == InsReg && InsSIdx == 0) {
00187     // No need to insert an identify copy instruction.
00188     // Watch out for case like this:
00189     // %RAX<def> = ...
00190     // %RAX<def> = SUBREG_TO_REG 0, %EAX:3<kill>, 3
00191     // The first def is defining RAX, not EAX so the top bits were not
00192     // zero extended.
00193     DOUT << "subreg: eliminated!";
00194   } else {
00195     // Insert sub-register copy
00196     const TargetRegisterClass *TRC0= TRI.getPhysicalRegisterRegClass(DstSubReg);
00197     const TargetRegisterClass *TRC1= TRI.getPhysicalRegisterRegClass(InsReg);
00198     TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1);
00199     // Transfer the kill/dead flags, if needed.
00200     if (MI->getOperand(0).isDead())
00201       TransferDeadFlag(MI, DstSubReg, TRI);
00202     if (MI->getOperand(2).isKill())
00203       TransferKillFlag(MI, InsReg, TRI);
00204 
00205 #ifndef NDEBUG
00206     MachineBasicBlock::iterator dMI = MI;
00207     DOUT << "subreg: " << *(--dMI);
00208 #endif
00209   }
00210 
00211   DOUT << "\n";
00212   MBB->erase(MI);
00213   return true;                    
00214 }
00215 
00216 bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) {
00217   MachineBasicBlock *MBB = MI->getParent();
00218   MachineFunction &MF = *MBB->getParent();
00219   const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo(); 
00220   const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
00221   assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
00222          (MI->getOperand(1).isReg() && MI->getOperand(1).isUse()) &&
00223          (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
00224           MI->getOperand(3).isImm() && "Invalid insert_subreg");
00225           
00226   unsigned DstReg = MI->getOperand(0).getReg();
00227 #ifndef NDEBUG
00228   unsigned SrcReg = MI->getOperand(1).getReg();
00229 #endif
00230   unsigned InsReg = MI->getOperand(2).getReg();
00231   unsigned SubIdx = MI->getOperand(3).getImm();     
00232 
00233   assert(DstReg == SrcReg && "insert_subreg not a two-address instruction?");
00234   assert(SubIdx != 0 && "Invalid index for insert_subreg");
00235   unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx);
00236   assert(DstSubReg && "invalid subregister index for register");
00237   assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
00238          "Insert superreg source must be in a physical register");
00239   assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
00240          "Inserted value must be in a physical register");
00241 
00242   DOUT << "subreg: CONVERTING: " << *MI;
00243 
00244   if (DstSubReg == InsReg) {
00245     // No need to insert an identity copy instruction. If the SrcReg was
00246     // <undef>, we need to make sure it is alive by inserting an IMPLICIT_DEF
00247     if (MI->getOperand(1).isUndef() && !MI->getOperand(0).isDead()) {
00248       MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(),
00249                                 TII.get(TargetInstrInfo::IMPLICIT_DEF), DstReg);
00250       if (MI->getOperand(2).isUndef())
00251         MIB.addReg(InsReg, RegState::Implicit | RegState::Undef);
00252       else
00253         MIB.addReg(InsReg, RegState::ImplicitKill);
00254     } else {
00255       DOUT << "subreg: eliminated!\n";
00256       MBB->erase(MI);
00257       return true;
00258     }
00259   } else {
00260     // Insert sub-register copy
00261     const TargetRegisterClass *TRC0= TRI.getPhysicalRegisterRegClass(DstSubReg);
00262     const TargetRegisterClass *TRC1= TRI.getPhysicalRegisterRegClass(InsReg);
00263     if (MI->getOperand(2).isUndef())
00264       // If the source register being inserted is undef, then this becomes an
00265       // implicit_def.
00266       BuildMI(*MBB, MI, MI->getDebugLoc(),
00267               TII.get(TargetInstrInfo::IMPLICIT_DEF), DstSubReg);
00268     else
00269       TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1);
00270     MachineBasicBlock::iterator CopyMI = MI;
00271     --CopyMI;
00272 
00273     // INSERT_SUBREG is a two-address instruction so it implicitly kills SrcReg.
00274     if (!MI->getOperand(1).isUndef())
00275       CopyMI->addOperand(MachineOperand::CreateReg(DstReg, false, true, true));
00276 
00277     // Transfer the kill/dead flags, if needed.
00278     if (MI->getOperand(0).isDead()) {
00279       TransferDeadFlag(MI, DstSubReg, TRI);
00280     } else {
00281       // Make sure the full DstReg is live after this replacement.
00282       CopyMI->addOperand(MachineOperand::CreateReg(DstReg, true, true));
00283     }
00284 
00285     // Make sure the inserted register gets killed
00286     if (MI->getOperand(2).isKill() && !MI->getOperand(2).isUndef())
00287       TransferKillFlag(MI, InsReg, TRI);
00288   }
00289 
00290 #ifndef NDEBUG
00291   MachineBasicBlock::iterator dMI = MI;
00292   DOUT << "subreg: " << *(--dMI);
00293 #endif
00294 
00295   DOUT << "\n";
00296   MBB->erase(MI);
00297   return true;
00298 }
00299 
00300 /// runOnMachineFunction - Reduce subregister inserts and extracts to register
00301 /// copies.
00302 ///
00303 bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) {
00304   DOUT << "Machine Function\n";
00305   
00306   bool MadeChange = false;
00307 
00308   DOUT << "********** LOWERING SUBREG INSTRS **********\n";
00309   DEBUG(errs() << "********** Function: " 
00310         << MF.getFunction()->getName() << '\n');
00311 
00312   for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
00313        mbbi != mbbe; ++mbbi) {
00314     for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
00315          mi != me;) {
00316       MachineInstr *MI = mi++;
00317            
00318       if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
00319         MadeChange |= LowerExtract(MI);
00320       } else if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) {
00321         MadeChange |= LowerInsert(MI);
00322       } else if (MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) {
00323         MadeChange |= LowerSubregToReg(MI);
00324       }
00325     }
00326   }
00327 
00328   return MadeChange;
00329 }