LLVM API Documentation
00001 //===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===// 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 // The LiveRangeEdit class represents changes done to a virtual register when it 00011 // is spilled or split. 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "regalloc" 00015 #include "llvm/CodeGen/LiveRangeEdit.h" 00016 #include "llvm/ADT/SetVector.h" 00017 #include "llvm/ADT/Statistic.h" 00018 #include "llvm/CodeGen/CalcSpillWeights.h" 00019 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 00020 #include "llvm/CodeGen/MachineRegisterInfo.h" 00021 #include "llvm/CodeGen/VirtRegMap.h" 00022 #include "llvm/Support/Debug.h" 00023 #include "llvm/Support/raw_ostream.h" 00024 #include "llvm/Target/TargetInstrInfo.h" 00025 00026 using namespace llvm; 00027 00028 STATISTIC(NumDCEDeleted, "Number of instructions deleted by DCE"); 00029 STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE"); 00030 STATISTIC(NumFracRanges, "Number of live ranges fractured by DCE"); 00031 00032 void LiveRangeEdit::Delegate::anchor() { } 00033 00034 LiveInterval &LiveRangeEdit::createFrom(unsigned OldReg) { 00035 unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg)); 00036 if (VRM) { 00037 VRM->grow(); 00038 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg)); 00039 } 00040 LiveInterval &LI = LIS.getOrCreateInterval(VReg); 00041 NewRegs.push_back(&LI); 00042 return LI; 00043 } 00044 00045 bool LiveRangeEdit::checkRematerializable(VNInfo *VNI, 00046 const MachineInstr *DefMI, 00047 AliasAnalysis *aa) { 00048 assert(DefMI && "Missing instruction"); 00049 ScannedRemattable = true; 00050 if (!TII.isTriviallyReMaterializable(DefMI, aa)) 00051 return false; 00052 Remattable.insert(VNI); 00053 return true; 00054 } 00055 00056 void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) { 00057 for (LiveInterval::vni_iterator I = getParent().vni_begin(), 00058 E = getParent().vni_end(); I != E; ++I) { 00059 VNInfo *VNI = *I; 00060 if (VNI->isUnused()) 00061 continue; 00062 MachineInstr *DefMI = LIS.getInstructionFromIndex(VNI->def); 00063 if (!DefMI) 00064 continue; 00065 checkRematerializable(VNI, DefMI, aa); 00066 } 00067 ScannedRemattable = true; 00068 } 00069 00070 bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) { 00071 if (!ScannedRemattable) 00072 scanRemattable(aa); 00073 return !Remattable.empty(); 00074 } 00075 00076 /// allUsesAvailableAt - Return true if all registers used by OrigMI at 00077 /// OrigIdx are also available with the same value at UseIdx. 00078 bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI, 00079 SlotIndex OrigIdx, 00080 SlotIndex UseIdx) const { 00081 OrigIdx = OrigIdx.getRegSlot(true); 00082 UseIdx = UseIdx.getRegSlot(true); 00083 for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) { 00084 const MachineOperand &MO = OrigMI->getOperand(i); 00085 if (!MO.isReg() || !MO.getReg() || !MO.readsReg()) 00086 continue; 00087 00088 // We can't remat physreg uses, unless it is a constant. 00089 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) { 00090 if (MRI.isConstantPhysReg(MO.getReg(), *OrigMI->getParent()->getParent())) 00091 continue; 00092 return false; 00093 } 00094 00095 LiveInterval &li = LIS.getInterval(MO.getReg()); 00096 const VNInfo *OVNI = li.getVNInfoAt(OrigIdx); 00097 if (!OVNI) 00098 continue; 00099 00100 // Don't allow rematerialization immediately after the original def. 00101 // It would be incorrect if OrigMI redefines the register. 00102 // See PR14098. 00103 if (SlotIndex::isSameInstr(OrigIdx, UseIdx)) 00104 return false; 00105 00106 if (OVNI != li.getVNInfoAt(UseIdx)) 00107 return false; 00108 } 00109 return true; 00110 } 00111 00112 bool LiveRangeEdit::canRematerializeAt(Remat &RM, 00113 SlotIndex UseIdx, 00114 bool cheapAsAMove) { 00115 assert(ScannedRemattable && "Call anyRematerializable first"); 00116 00117 // Use scanRemattable info. 00118 if (!Remattable.count(RM.ParentVNI)) 00119 return false; 00120 00121 // No defining instruction provided. 00122 SlotIndex DefIdx; 00123 if (RM.OrigMI) 00124 DefIdx = LIS.getInstructionIndex(RM.OrigMI); 00125 else { 00126 DefIdx = RM.ParentVNI->def; 00127 RM.OrigMI = LIS.getInstructionFromIndex(DefIdx); 00128 assert(RM.OrigMI && "No defining instruction for remattable value"); 00129 } 00130 00131 // If only cheap remats were requested, bail out early. 00132 if (cheapAsAMove && !RM.OrigMI->isAsCheapAsAMove()) 00133 return false; 00134 00135 // Verify that all used registers are available with the same values. 00136 if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx)) 00137 return false; 00138 00139 return true; 00140 } 00141 00142 SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB, 00143 MachineBasicBlock::iterator MI, 00144 unsigned DestReg, 00145 const Remat &RM, 00146 const TargetRegisterInfo &tri, 00147 bool Late) { 00148 assert(RM.OrigMI && "Invalid remat"); 00149 TII.reMaterialize(MBB, MI, DestReg, 0, RM.OrigMI, tri); 00150 Rematted.insert(RM.ParentVNI); 00151 return LIS.getSlotIndexes()->insertMachineInstrInMaps(--MI, Late) 00152 .getRegSlot(); 00153 } 00154 00155 void LiveRangeEdit::eraseVirtReg(unsigned Reg) { 00156 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg)) 00157 LIS.removeInterval(Reg); 00158 } 00159 00160 bool LiveRangeEdit::foldAsLoad(LiveInterval *LI, 00161 SmallVectorImpl<MachineInstr*> &Dead) { 00162 MachineInstr *DefMI = 0, *UseMI = 0; 00163 00164 // Check that there is a single def and a single use. 00165 for (MachineRegisterInfo::reg_nodbg_iterator I = MRI.reg_nodbg_begin(LI->reg), 00166 E = MRI.reg_nodbg_end(); I != E; ++I) { 00167 MachineOperand &MO = I.getOperand(); 00168 MachineInstr *MI = MO.getParent(); 00169 if (MO.isDef()) { 00170 if (DefMI && DefMI != MI) 00171 return false; 00172 if (!MI->canFoldAsLoad()) 00173 return false; 00174 DefMI = MI; 00175 } else if (!MO.isUndef()) { 00176 if (UseMI && UseMI != MI) 00177 return false; 00178 // FIXME: Targets don't know how to fold subreg uses. 00179 if (MO.getSubReg()) 00180 return false; 00181 UseMI = MI; 00182 } 00183 } 00184 if (!DefMI || !UseMI) 00185 return false; 00186 00187 // Since we're moving the DefMI load, make sure we're not extending any live 00188 // ranges. 00189 if (!allUsesAvailableAt(DefMI, 00190 LIS.getInstructionIndex(DefMI), 00191 LIS.getInstructionIndex(UseMI))) 00192 return false; 00193 00194 // We also need to make sure it is safe to move the load. 00195 // Assume there are stores between DefMI and UseMI. 00196 bool SawStore = true; 00197 if (!DefMI->isSafeToMove(&TII, 0, SawStore)) 00198 return false; 00199 00200 DEBUG(dbgs() << "Try to fold single def: " << *DefMI 00201 << " into single use: " << *UseMI); 00202 00203 SmallVector<unsigned, 8> Ops; 00204 if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second) 00205 return false; 00206 00207 MachineInstr *FoldMI = TII.foldMemoryOperand(UseMI, Ops, DefMI); 00208 if (!FoldMI) 00209 return false; 00210 DEBUG(dbgs() << " folded: " << *FoldMI); 00211 LIS.ReplaceMachineInstrInMaps(UseMI, FoldMI); 00212 UseMI->eraseFromParent(); 00213 DefMI->addRegisterDead(LI->reg, 0); 00214 Dead.push_back(DefMI); 00215 ++NumDCEFoldedLoads; 00216 return true; 00217 } 00218 00219 void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead, 00220 ArrayRef<unsigned> RegsBeingSpilled) { 00221 SetVector<LiveInterval*, 00222 SmallVector<LiveInterval*, 8>, 00223 SmallPtrSet<LiveInterval*, 8> > ToShrink; 00224 00225 for (;;) { 00226 // Erase all dead defs. 00227 while (!Dead.empty()) { 00228 MachineInstr *MI = Dead.pop_back_val(); 00229 assert(MI->allDefsAreDead() && "Def isn't really dead"); 00230 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot(); 00231 00232 // Never delete inline asm. 00233 if (MI->isInlineAsm()) { 00234 DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI); 00235 continue; 00236 } 00237 00238 // Use the same criteria as DeadMachineInstructionElim. 00239 bool SawStore = false; 00240 if (!MI->isSafeToMove(&TII, 0, SawStore)) { 00241 DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI); 00242 continue; 00243 } 00244 00245 DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI); 00246 00247 // Collect virtual registers to be erased after MI is gone. 00248 SmallVector<unsigned, 8> RegsToErase; 00249 bool ReadsPhysRegs = false; 00250 00251 // Check for live intervals that may shrink 00252 for (MachineInstr::mop_iterator MOI = MI->operands_begin(), 00253 MOE = MI->operands_end(); MOI != MOE; ++MOI) { 00254 if (!MOI->isReg()) 00255 continue; 00256 unsigned Reg = MOI->getReg(); 00257 if (!TargetRegisterInfo::isVirtualRegister(Reg)) { 00258 // Check if MI reads any unreserved physregs. 00259 if (Reg && MOI->readsReg() && !MRI.isReserved(Reg)) 00260 ReadsPhysRegs = true; 00261 continue; 00262 } 00263 LiveInterval &LI = LIS.getInterval(Reg); 00264 00265 // Shrink read registers, unless it is likely to be expensive and 00266 // unlikely to change anything. We typically don't want to shrink the 00267 // PIC base register that has lots of uses everywhere. 00268 // Always shrink COPY uses that probably come from live range splitting. 00269 if (MI->readsVirtualRegister(Reg) && 00270 (MI->isCopy() || MOI->isDef() || MRI.hasOneNonDBGUse(Reg) || 00271 LI.killedAt(Idx))) 00272 ToShrink.insert(&LI); 00273 00274 // Remove defined value. 00275 if (MOI->isDef()) { 00276 if (VNInfo *VNI = LI.getVNInfoAt(Idx)) { 00277 if (TheDelegate) 00278 TheDelegate->LRE_WillShrinkVirtReg(LI.reg); 00279 LI.removeValNo(VNI); 00280 if (LI.empty()) 00281 RegsToErase.push_back(Reg); 00282 } 00283 } 00284 } 00285 00286 // Currently, we don't support DCE of physreg live ranges. If MI reads 00287 // any unreserved physregs, don't erase the instruction, but turn it into 00288 // a KILL instead. This way, the physreg live ranges don't end up 00289 // dangling. 00290 // FIXME: It would be better to have something like shrinkToUses() for 00291 // physregs. That could potentially enable more DCE and it would free up 00292 // the physreg. It would not happen often, though. 00293 if (ReadsPhysRegs) { 00294 MI->setDesc(TII.get(TargetOpcode::KILL)); 00295 // Remove all operands that aren't physregs. 00296 for (unsigned i = MI->getNumOperands(); i; --i) { 00297 const MachineOperand &MO = MI->getOperand(i-1); 00298 if (MO.isReg() && TargetRegisterInfo::isPhysicalRegister(MO.getReg())) 00299 continue; 00300 MI->RemoveOperand(i-1); 00301 } 00302 DEBUG(dbgs() << "Converted physregs to:\t" << *MI); 00303 } else { 00304 if (TheDelegate) 00305 TheDelegate->LRE_WillEraseInstruction(MI); 00306 LIS.RemoveMachineInstrFromMaps(MI); 00307 MI->eraseFromParent(); 00308 ++NumDCEDeleted; 00309 } 00310 00311 // Erase any virtregs that are now empty and unused. There may be <undef> 00312 // uses around. Keep the empty live range in that case. 00313 for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) { 00314 unsigned Reg = RegsToErase[i]; 00315 if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) { 00316 ToShrink.remove(&LIS.getInterval(Reg)); 00317 eraseVirtReg(Reg); 00318 } 00319 } 00320 } 00321 00322 if (ToShrink.empty()) 00323 break; 00324 00325 // Shrink just one live interval. Then delete new dead defs. 00326 LiveInterval *LI = ToShrink.back(); 00327 ToShrink.pop_back(); 00328 if (foldAsLoad(LI, Dead)) 00329 continue; 00330 if (TheDelegate) 00331 TheDelegate->LRE_WillShrinkVirtReg(LI->reg); 00332 if (!LIS.shrinkToUses(LI, &Dead)) 00333 continue; 00334 00335 // Don't create new intervals for a register being spilled. 00336 // The new intervals would have to be spilled anyway so its not worth it. 00337 // Also they currently aren't spilled so creating them and not spilling 00338 // them results in incorrect code. 00339 bool BeingSpilled = false; 00340 for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) { 00341 if (LI->reg == RegsBeingSpilled[i]) { 00342 BeingSpilled = true; 00343 break; 00344 } 00345 } 00346 00347 if (BeingSpilled) continue; 00348 00349 // LI may have been separated, create new intervals. 00350 LI->RenumberValues(LIS); 00351 ConnectedVNInfoEqClasses ConEQ(LIS); 00352 unsigned NumComp = ConEQ.Classify(LI); 00353 if (NumComp <= 1) 00354 continue; 00355 ++NumFracRanges; 00356 bool IsOriginal = VRM && VRM->getOriginal(LI->reg) == LI->reg; 00357 DEBUG(dbgs() << NumComp << " components: " << *LI << '\n'); 00358 SmallVector<LiveInterval*, 8> Dups(1, LI); 00359 for (unsigned i = 1; i != NumComp; ++i) { 00360 Dups.push_back(&createFrom(LI->reg)); 00361 // If LI is an original interval that hasn't been split yet, make the new 00362 // intervals their own originals instead of referring to LI. The original 00363 // interval must contain all the split products, and LI doesn't. 00364 if (IsOriginal) 00365 VRM->setIsSplitFromReg(Dups.back()->reg, 0); 00366 if (TheDelegate) 00367 TheDelegate->LRE_DidCloneVirtReg(Dups.back()->reg, LI->reg); 00368 } 00369 ConEQ.Distribute(&Dups[0], MRI); 00370 DEBUG({ 00371 for (unsigned i = 0; i != NumComp; ++i) 00372 dbgs() << '\t' << *Dups[i] << '\n'; 00373 }); 00374 } 00375 } 00376 00377 void LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF, 00378 const MachineLoopInfo &Loops) { 00379 VirtRegAuxInfo VRAI(MF, LIS, Loops); 00380 for (iterator I = begin(), E = end(); I != E; ++I) { 00381 LiveInterval &LI = **I; 00382 if (MRI.recomputeRegClass(LI.reg, MF.getTarget())) 00383 DEBUG(dbgs() << "Inflated " << PrintReg(LI.reg) << " to " 00384 << MRI.getRegClass(LI.reg)->getName() << '\n'); 00385 VRAI.CalculateWeightAndHint(LI); 00386 } 00387 }