LLVM API Documentation
00001 //===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion 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 pass that expands COPY and SUBREG_TO_REG pseudo 00011 // instructions after register allocation. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/CodeGen/Passes.h" 00016 #include "llvm/CodeGen/MachineFunctionPass.h" 00017 #include "llvm/CodeGen/MachineInstr.h" 00018 #include "llvm/CodeGen/MachineInstrBuilder.h" 00019 #include "llvm/CodeGen/MachineRegisterInfo.h" 00020 #include "llvm/Support/Debug.h" 00021 #include "llvm/Support/raw_ostream.h" 00022 #include "llvm/Target/TargetInstrInfo.h" 00023 #include "llvm/Target/TargetMachine.h" 00024 #include "llvm/Target/TargetRegisterInfo.h" 00025 #include "llvm/Target/TargetSubtargetInfo.h" 00026 00027 using namespace llvm; 00028 00029 #define DEBUG_TYPE "postrapseudos" 00030 00031 namespace { 00032 struct ExpandPostRA : public MachineFunctionPass { 00033 private: 00034 const TargetRegisterInfo *TRI; 00035 const TargetInstrInfo *TII; 00036 00037 public: 00038 static char ID; // Pass identification, replacement for typeid 00039 ExpandPostRA() : MachineFunctionPass(ID) {} 00040 00041 void getAnalysisUsage(AnalysisUsage &AU) const override { 00042 AU.setPreservesCFG(); 00043 AU.addPreservedID(MachineLoopInfoID); 00044 AU.addPreservedID(MachineDominatorsID); 00045 MachineFunctionPass::getAnalysisUsage(AU); 00046 } 00047 00048 /// runOnMachineFunction - pass entry point 00049 bool runOnMachineFunction(MachineFunction&) override; 00050 00051 private: 00052 bool LowerSubregToReg(MachineInstr *MI); 00053 bool LowerCopy(MachineInstr *MI); 00054 00055 void TransferImplicitDefs(MachineInstr *MI); 00056 }; 00057 } // end anonymous namespace 00058 00059 char ExpandPostRA::ID = 0; 00060 char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID; 00061 00062 INITIALIZE_PASS(ExpandPostRA, "postrapseudos", 00063 "Post-RA pseudo instruction expansion pass", false, false) 00064 00065 /// TransferImplicitDefs - MI is a pseudo-instruction, and the lowered 00066 /// replacement instructions immediately precede it. Copy any implicit-def 00067 /// operands from MI to the replacement instruction. 00068 void 00069 ExpandPostRA::TransferImplicitDefs(MachineInstr *MI) { 00070 MachineBasicBlock::iterator CopyMI = MI; 00071 --CopyMI; 00072 00073 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00074 MachineOperand &MO = MI->getOperand(i); 00075 if (!MO.isReg() || !MO.isImplicit() || MO.isUse()) 00076 continue; 00077 CopyMI->addOperand(MachineOperand::CreateReg(MO.getReg(), true, true)); 00078 } 00079 } 00080 00081 bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) { 00082 MachineBasicBlock *MBB = MI->getParent(); 00083 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) && 00084 MI->getOperand(1).isImm() && 00085 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) && 00086 MI->getOperand(3).isImm() && "Invalid subreg_to_reg"); 00087 00088 unsigned DstReg = MI->getOperand(0).getReg(); 00089 unsigned InsReg = MI->getOperand(2).getReg(); 00090 assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?"); 00091 unsigned SubIdx = MI->getOperand(3).getImm(); 00092 00093 assert(SubIdx != 0 && "Invalid index for insert_subreg"); 00094 unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx); 00095 00096 assert(TargetRegisterInfo::isPhysicalRegister(DstReg) && 00097 "Insert destination must be in a physical register"); 00098 assert(TargetRegisterInfo::isPhysicalRegister(InsReg) && 00099 "Inserted value must be in a physical register"); 00100 00101 DEBUG(dbgs() << "subreg: CONVERTING: " << *MI); 00102 00103 if (MI->allDefsAreDead()) { 00104 MI->setDesc(TII->get(TargetOpcode::KILL)); 00105 DEBUG(dbgs() << "subreg: replaced by: " << *MI); 00106 return true; 00107 } 00108 00109 if (DstSubReg == InsReg) { 00110 // No need to insert an identity copy instruction. 00111 // Watch out for case like this: 00112 // %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3 00113 // We must leave %RAX live. 00114 if (DstReg != InsReg) { 00115 MI->setDesc(TII->get(TargetOpcode::KILL)); 00116 MI->RemoveOperand(3); // SubIdx 00117 MI->RemoveOperand(1); // Imm 00118 DEBUG(dbgs() << "subreg: replace by: " << *MI); 00119 return true; 00120 } 00121 DEBUG(dbgs() << "subreg: eliminated!"); 00122 } else { 00123 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg, 00124 MI->getOperand(2).isKill()); 00125 00126 // Implicitly define DstReg for subsequent uses. 00127 MachineBasicBlock::iterator CopyMI = MI; 00128 --CopyMI; 00129 CopyMI->addRegisterDefined(DstReg); 00130 DEBUG(dbgs() << "subreg: " << *CopyMI); 00131 } 00132 00133 DEBUG(dbgs() << '\n'); 00134 MBB->erase(MI); 00135 return true; 00136 } 00137 00138 bool ExpandPostRA::LowerCopy(MachineInstr *MI) { 00139 00140 if (MI->allDefsAreDead()) { 00141 DEBUG(dbgs() << "dead copy: " << *MI); 00142 MI->setDesc(TII->get(TargetOpcode::KILL)); 00143 DEBUG(dbgs() << "replaced by: " << *MI); 00144 return true; 00145 } 00146 00147 MachineOperand &DstMO = MI->getOperand(0); 00148 MachineOperand &SrcMO = MI->getOperand(1); 00149 00150 if (SrcMO.getReg() == DstMO.getReg()) { 00151 DEBUG(dbgs() << "identity copy: " << *MI); 00152 // No need to insert an identity copy instruction, but replace with a KILL 00153 // if liveness is changed. 00154 if (SrcMO.isUndef() || MI->getNumOperands() > 2) { 00155 // We must make sure the super-register gets killed. Replace the 00156 // instruction with KILL. 00157 MI->setDesc(TII->get(TargetOpcode::KILL)); 00158 DEBUG(dbgs() << "replaced by: " << *MI); 00159 return true; 00160 } 00161 // Vanilla identity copy. 00162 MI->eraseFromParent(); 00163 return true; 00164 } 00165 00166 DEBUG(dbgs() << "real copy: " << *MI); 00167 TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(), 00168 DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill()); 00169 00170 if (MI->getNumOperands() > 2) 00171 TransferImplicitDefs(MI); 00172 DEBUG({ 00173 MachineBasicBlock::iterator dMI = MI; 00174 dbgs() << "replaced by: " << *(--dMI); 00175 }); 00176 MI->eraseFromParent(); 00177 return true; 00178 } 00179 00180 /// runOnMachineFunction - Reduce subregister inserts and extracts to register 00181 /// copies. 00182 /// 00183 bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) { 00184 DEBUG(dbgs() << "Machine Function\n" 00185 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n" 00186 << "********** Function: " << MF.getName() << '\n'); 00187 TRI = MF.getSubtarget().getRegisterInfo(); 00188 TII = MF.getSubtarget().getInstrInfo(); 00189 00190 bool MadeChange = false; 00191 00192 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); 00193 mbbi != mbbe; ++mbbi) { 00194 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); 00195 mi != me;) { 00196 MachineInstr *MI = mi; 00197 // Advance iterator here because MI may be erased. 00198 ++mi; 00199 00200 // Only expand pseudos. 00201 if (!MI->isPseudo()) 00202 continue; 00203 00204 // Give targets a chance to expand even standard pseudos. 00205 if (TII->expandPostRAPseudo(MI)) { 00206 MadeChange = true; 00207 continue; 00208 } 00209 00210 // Expand standard pseudos. 00211 switch (MI->getOpcode()) { 00212 case TargetOpcode::SUBREG_TO_REG: 00213 MadeChange |= LowerSubregToReg(MI); 00214 break; 00215 case TargetOpcode::COPY: 00216 MadeChange |= LowerCopy(MI); 00217 break; 00218 case TargetOpcode::DBG_VALUE: 00219 continue; 00220 case TargetOpcode::INSERT_SUBREG: 00221 case TargetOpcode::EXTRACT_SUBREG: 00222 llvm_unreachable("Sub-register pseudos should have been eliminated."); 00223 } 00224 } 00225 } 00226 00227 return MadeChange; 00228 }