LLVM API Documentation
00001 //===- MachineCopyPropagation.cpp - Machine Copy Propagation 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 is an extremely simple MachineInstr-level copy propagation pass. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/Passes.h" 00015 #include "llvm/ADT/DenseMap.h" 00016 #include "llvm/ADT/SetVector.h" 00017 #include "llvm/ADT/SmallVector.h" 00018 #include "llvm/ADT/Statistic.h" 00019 #include "llvm/CodeGen/MachineFunction.h" 00020 #include "llvm/CodeGen/MachineFunctionPass.h" 00021 #include "llvm/CodeGen/MachineRegisterInfo.h" 00022 #include "llvm/Pass.h" 00023 #include "llvm/Support/Debug.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 #include "llvm/Support/raw_ostream.h" 00026 #include "llvm/Target/TargetInstrInfo.h" 00027 #include "llvm/Target/TargetRegisterInfo.h" 00028 #include "llvm/Target/TargetSubtargetInfo.h" 00029 using namespace llvm; 00030 00031 #define DEBUG_TYPE "codegen-cp" 00032 00033 STATISTIC(NumDeletes, "Number of dead copies deleted"); 00034 00035 namespace { 00036 class MachineCopyPropagation : public MachineFunctionPass { 00037 const TargetRegisterInfo *TRI; 00038 const TargetInstrInfo *TII; 00039 MachineRegisterInfo *MRI; 00040 00041 public: 00042 static char ID; // Pass identification, replacement for typeid 00043 MachineCopyPropagation() : MachineFunctionPass(ID) { 00044 initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry()); 00045 } 00046 00047 bool runOnMachineFunction(MachineFunction &MF) override; 00048 00049 private: 00050 typedef SmallVector<unsigned, 4> DestList; 00051 typedef DenseMap<unsigned, DestList> SourceMap; 00052 00053 void SourceNoLongerAvailable(unsigned Reg, 00054 SourceMap &SrcMap, 00055 DenseMap<unsigned, MachineInstr*> &AvailCopyMap); 00056 bool CopyPropagateBlock(MachineBasicBlock &MBB); 00057 void removeCopy(MachineInstr *MI); 00058 }; 00059 } 00060 char MachineCopyPropagation::ID = 0; 00061 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID; 00062 00063 INITIALIZE_PASS(MachineCopyPropagation, "machine-cp", 00064 "Machine Copy Propagation Pass", false, false) 00065 00066 void 00067 MachineCopyPropagation::SourceNoLongerAvailable(unsigned Reg, 00068 SourceMap &SrcMap, 00069 DenseMap<unsigned, MachineInstr*> &AvailCopyMap) { 00070 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { 00071 SourceMap::iterator SI = SrcMap.find(*AI); 00072 if (SI != SrcMap.end()) { 00073 const DestList& Defs = SI->second; 00074 for (DestList::const_iterator I = Defs.begin(), E = Defs.end(); 00075 I != E; ++I) { 00076 unsigned MappedDef = *I; 00077 // Source of copy is no longer available for propagation. 00078 if (AvailCopyMap.erase(MappedDef)) { 00079 for (MCSubRegIterator SR(MappedDef, TRI); SR.isValid(); ++SR) 00080 AvailCopyMap.erase(*SR); 00081 } 00082 } 00083 } 00084 } 00085 } 00086 00087 static bool NoInterveningSideEffect(const MachineInstr *CopyMI, 00088 const MachineInstr *MI) { 00089 const MachineBasicBlock *MBB = CopyMI->getParent(); 00090 if (MI->getParent() != MBB) 00091 return false; 00092 MachineBasicBlock::const_iterator I = CopyMI; 00093 MachineBasicBlock::const_iterator E = MBB->end(); 00094 MachineBasicBlock::const_iterator E2 = MI; 00095 00096 ++I; 00097 while (I != E && I != E2) { 00098 if (I->hasUnmodeledSideEffects() || I->isCall() || 00099 I->isTerminator()) 00100 return false; 00101 ++I; 00102 } 00103 return true; 00104 } 00105 00106 /// isNopCopy - Return true if the specified copy is really a nop. That is 00107 /// if the source of the copy is the same of the definition of the copy that 00108 /// supplied the source. If the source of the copy is a sub-register than it 00109 /// must check the sub-indices match. e.g. 00110 /// ecx = mov eax 00111 /// al = mov cl 00112 /// But not 00113 /// ecx = mov eax 00114 /// al = mov ch 00115 static bool isNopCopy(MachineInstr *CopyMI, unsigned Def, unsigned Src, 00116 const TargetRegisterInfo *TRI) { 00117 unsigned SrcSrc = CopyMI->getOperand(1).getReg(); 00118 if (Def == SrcSrc) 00119 return true; 00120 if (TRI->isSubRegister(SrcSrc, Def)) { 00121 unsigned SrcDef = CopyMI->getOperand(0).getReg(); 00122 unsigned SubIdx = TRI->getSubRegIndex(SrcSrc, Def); 00123 if (!SubIdx) 00124 return false; 00125 return SubIdx == TRI->getSubRegIndex(SrcDef, Src); 00126 } 00127 00128 return false; 00129 } 00130 00131 // Remove MI from the function because it has been determined it is dead. 00132 // Turn it into a noop KILL instruction as opposed to removing it to 00133 // maintain imp-use/imp-def chains. 00134 void MachineCopyPropagation::removeCopy(MachineInstr *MI) { 00135 MI->setDesc(TII->get(TargetOpcode::KILL)); 00136 } 00137 00138 bool MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) { 00139 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies; // Candidates for deletion 00140 DenseMap<unsigned, MachineInstr*> AvailCopyMap; // Def -> available copies map 00141 DenseMap<unsigned, MachineInstr*> CopyMap; // Def -> copies map 00142 SourceMap SrcMap; // Src -> Def map 00143 00144 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n"); 00145 00146 bool Changed = false; 00147 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) { 00148 MachineInstr *MI = &*I; 00149 ++I; 00150 00151 if (MI->isCopy()) { 00152 unsigned Def = MI->getOperand(0).getReg(); 00153 unsigned Src = MI->getOperand(1).getReg(); 00154 00155 if (TargetRegisterInfo::isVirtualRegister(Def) || 00156 TargetRegisterInfo::isVirtualRegister(Src)) 00157 report_fatal_error("MachineCopyPropagation should be run after" 00158 " register allocation!"); 00159 00160 DenseMap<unsigned, MachineInstr*>::iterator CI = AvailCopyMap.find(Src); 00161 if (CI != AvailCopyMap.end()) { 00162 MachineInstr *CopyMI = CI->second; 00163 if (!MRI->isReserved(Def) && 00164 (!MRI->isReserved(Src) || NoInterveningSideEffect(CopyMI, MI)) && 00165 isNopCopy(CopyMI, Def, Src, TRI)) { 00166 // The two copies cancel out and the source of the first copy 00167 // hasn't been overridden, eliminate the second one. e.g. 00168 // %ECX<def> = COPY %EAX<kill> 00169 // ... nothing clobbered EAX. 00170 // %EAX<def> = COPY %ECX 00171 // => 00172 // %ECX<def> = COPY %EAX 00173 // 00174 // Also avoid eliminating a copy from reserved registers unless the 00175 // definition is proven not clobbered. e.g. 00176 // %RSP<def> = COPY %RAX 00177 // CALL 00178 // %RAX<def> = COPY %RSP 00179 00180 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; MI->dump()); 00181 00182 // Clear any kills of Def between CopyMI and MI. This extends the 00183 // live range. 00184 for (MachineBasicBlock::iterator I = CopyMI, E = MI; I != E; ++I) 00185 I->clearRegisterKills(Def, TRI); 00186 00187 removeCopy(MI); 00188 Changed = true; 00189 ++NumDeletes; 00190 continue; 00191 } 00192 } 00193 00194 // If Src is defined by a previous copy, it cannot be eliminated. 00195 for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) { 00196 CI = CopyMap.find(*AI); 00197 if (CI != CopyMap.end()) { 00198 DEBUG(dbgs() << "MCP: Copy is no longer dead: "; CI->second->dump()); 00199 MaybeDeadCopies.remove(CI->second); 00200 } 00201 } 00202 00203 DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump()); 00204 00205 // Copy is now a candidate for deletion. 00206 MaybeDeadCopies.insert(MI); 00207 00208 // If 'Src' is previously source of another copy, then this earlier copy's 00209 // source is no longer available. e.g. 00210 // %xmm9<def> = copy %xmm2 00211 // ... 00212 // %xmm2<def> = copy %xmm0 00213 // ... 00214 // %xmm2<def> = copy %xmm9 00215 SourceNoLongerAvailable(Def, SrcMap, AvailCopyMap); 00216 00217 // Remember Def is defined by the copy. 00218 // ... Make sure to clear the def maps of aliases first. 00219 for (MCRegAliasIterator AI(Def, TRI, false); AI.isValid(); ++AI) { 00220 CopyMap.erase(*AI); 00221 AvailCopyMap.erase(*AI); 00222 } 00223 for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid(); 00224 ++SR) { 00225 CopyMap[*SR] = MI; 00226 AvailCopyMap[*SR] = MI; 00227 } 00228 00229 // Remember source that's copied to Def. Once it's clobbered, then 00230 // it's no longer available for copy propagation. 00231 if (std::find(SrcMap[Src].begin(), SrcMap[Src].end(), Def) == 00232 SrcMap[Src].end()) { 00233 SrcMap[Src].push_back(Def); 00234 } 00235 00236 continue; 00237 } 00238 00239 // Not a copy. 00240 SmallVector<unsigned, 2> Defs; 00241 int RegMaskOpNum = -1; 00242 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00243 MachineOperand &MO = MI->getOperand(i); 00244 if (MO.isRegMask()) 00245 RegMaskOpNum = i; 00246 if (!MO.isReg()) 00247 continue; 00248 unsigned Reg = MO.getReg(); 00249 if (!Reg) 00250 continue; 00251 00252 if (TargetRegisterInfo::isVirtualRegister(Reg)) 00253 report_fatal_error("MachineCopyPropagation should be run after" 00254 " register allocation!"); 00255 00256 if (MO.isDef()) { 00257 Defs.push_back(Reg); 00258 continue; 00259 } 00260 00261 // If 'Reg' is defined by a copy, the copy is no longer a candidate 00262 // for elimination. 00263 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { 00264 DenseMap<unsigned, MachineInstr*>::iterator CI = CopyMap.find(*AI); 00265 if (CI != CopyMap.end()) { 00266 DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump()); 00267 MaybeDeadCopies.remove(CI->second); 00268 } 00269 } 00270 } 00271 00272 // The instruction has a register mask operand which means that it clobbers 00273 // a large set of registers. It is possible to use the register mask to 00274 // prune the available copies, but treat it like a basic block boundary for 00275 // now. 00276 if (RegMaskOpNum >= 0) { 00277 // Erase any MaybeDeadCopies whose destination register is clobbered. 00278 const MachineOperand &MaskMO = MI->getOperand(RegMaskOpNum); 00279 for (SmallSetVector<MachineInstr*, 8>::iterator 00280 DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end(); 00281 DI != DE; ++DI) { 00282 unsigned Reg = (*DI)->getOperand(0).getReg(); 00283 if (MRI->isReserved(Reg) || !MaskMO.clobbersPhysReg(Reg)) 00284 continue; 00285 DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: "; 00286 (*DI)->dump()); 00287 removeCopy(*DI); 00288 Changed = true; 00289 ++NumDeletes; 00290 } 00291 00292 // Clear all data structures as if we were beginning a new basic block. 00293 MaybeDeadCopies.clear(); 00294 AvailCopyMap.clear(); 00295 CopyMap.clear(); 00296 SrcMap.clear(); 00297 continue; 00298 } 00299 00300 for (unsigned i = 0, e = Defs.size(); i != e; ++i) { 00301 unsigned Reg = Defs[i]; 00302 00303 // No longer defined by a copy. 00304 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { 00305 CopyMap.erase(*AI); 00306 AvailCopyMap.erase(*AI); 00307 } 00308 00309 // If 'Reg' is previously source of a copy, it is no longer available for 00310 // copy propagation. 00311 SourceNoLongerAvailable(Reg, SrcMap, AvailCopyMap); 00312 } 00313 } 00314 00315 // If MBB doesn't have successors, delete the copies whose defs are not used. 00316 // If MBB does have successors, then conservative assume the defs are live-out 00317 // since we don't want to trust live-in lists. 00318 if (MBB.succ_empty()) { 00319 for (SmallSetVector<MachineInstr*, 8>::iterator 00320 DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end(); 00321 DI != DE; ++DI) { 00322 if (!MRI->isReserved((*DI)->getOperand(0).getReg())) { 00323 removeCopy(*DI); 00324 Changed = true; 00325 ++NumDeletes; 00326 } 00327 } 00328 } 00329 00330 return Changed; 00331 } 00332 00333 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) { 00334 if (skipOptnoneFunction(*MF.getFunction())) 00335 return false; 00336 00337 bool Changed = false; 00338 00339 TRI = MF.getSubtarget().getRegisterInfo(); 00340 TII = MF.getSubtarget().getInstrInfo(); 00341 MRI = &MF.getRegInfo(); 00342 00343 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) 00344 Changed |= CopyPropagateBlock(*I); 00345 00346 return Changed; 00347 }