LiveRangeEdit.cpp revision 834a9cdbb78c1a16c8ceecb018a324a3ba31c365
1//===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// The LiveRangeEdit class represents changes done to a virtual register when it 11// is spilled or split. 12//===----------------------------------------------------------------------===// 13 14#define DEBUG_TYPE "regalloc" 15#include "VirtRegMap.h" 16#include "llvm/ADT/SetVector.h" 17#include "llvm/ADT/Statistic.h" 18#include "llvm/CodeGen/CalcSpillWeights.h" 19#include "llvm/CodeGen/LiveIntervalAnalysis.h" 20#include "llvm/CodeGen/LiveRangeEdit.h" 21#include "llvm/CodeGen/MachineRegisterInfo.h" 22#include "llvm/Target/TargetInstrInfo.h" 23#include "llvm/Support/Debug.h" 24#include "llvm/Support/raw_ostream.h" 25 26using namespace llvm; 27 28STATISTIC(NumDCEDeleted, "Number of instructions deleted by DCE"); 29STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE"); 30STATISTIC(NumFracRanges, "Number of live ranges fractured by DCE"); 31 32void LiveRangeEdit::Delegate::anchor() { } 33 34LiveInterval &LiveRangeEdit::createFrom(unsigned OldReg) { 35 unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg)); 36 if (VRM) { 37 VRM->grow(); 38 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg)); 39 } 40 LiveInterval &LI = LIS.getOrCreateInterval(VReg); 41 NewRegs.push_back(&LI); 42 return LI; 43} 44 45bool LiveRangeEdit::checkRematerializable(VNInfo *VNI, 46 const MachineInstr *DefMI, 47 AliasAnalysis *aa) { 48 assert(DefMI && "Missing instruction"); 49 ScannedRemattable = true; 50 if (!TII.isTriviallyReMaterializable(DefMI, aa)) 51 return false; 52 Remattable.insert(VNI); 53 return true; 54} 55 56void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) { 57 for (LiveInterval::vni_iterator I = getParent().vni_begin(), 58 E = getParent().vni_end(); I != E; ++I) { 59 VNInfo *VNI = *I; 60 if (VNI->isUnused()) 61 continue; 62 MachineInstr *DefMI = LIS.getInstructionFromIndex(VNI->def); 63 if (!DefMI) 64 continue; 65 checkRematerializable(VNI, DefMI, aa); 66 } 67 ScannedRemattable = true; 68} 69 70bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) { 71 if (!ScannedRemattable) 72 scanRemattable(aa); 73 return !Remattable.empty(); 74} 75 76/// allUsesAvailableAt - Return true if all registers used by OrigMI at 77/// OrigIdx are also available with the same value at UseIdx. 78bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI, 79 SlotIndex OrigIdx, 80 SlotIndex UseIdx) { 81 OrigIdx = OrigIdx.getRegSlot(true); 82 UseIdx = UseIdx.getRegSlot(true); 83 for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) { 84 const MachineOperand &MO = OrigMI->getOperand(i); 85 if (!MO.isReg() || !MO.getReg() || !MO.readsReg()) 86 continue; 87 88 // We can't remat physreg uses, unless it is a constant. 89 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) { 90 if (MRI.isConstantPhysReg(MO.getReg(), VRM->getMachineFunction())) 91 continue; 92 return false; 93 } 94 95 LiveInterval &li = LIS.getInterval(MO.getReg()); 96 const VNInfo *OVNI = li.getVNInfoAt(OrigIdx); 97 if (!OVNI) 98 continue; 99 if (OVNI != li.getVNInfoAt(UseIdx)) 100 return false; 101 } 102 return true; 103} 104 105bool LiveRangeEdit::canRematerializeAt(Remat &RM, 106 SlotIndex UseIdx, 107 bool cheapAsAMove) { 108 assert(ScannedRemattable && "Call anyRematerializable first"); 109 110 // Use scanRemattable info. 111 if (!Remattable.count(RM.ParentVNI)) 112 return false; 113 114 // No defining instruction provided. 115 SlotIndex DefIdx; 116 if (RM.OrigMI) 117 DefIdx = LIS.getInstructionIndex(RM.OrigMI); 118 else { 119 DefIdx = RM.ParentVNI->def; 120 RM.OrigMI = LIS.getInstructionFromIndex(DefIdx); 121 assert(RM.OrigMI && "No defining instruction for remattable value"); 122 } 123 124 // If only cheap remats were requested, bail out early. 125 if (cheapAsAMove && !RM.OrigMI->isAsCheapAsAMove()) 126 return false; 127 128 // Verify that all used registers are available with the same values. 129 if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx)) 130 return false; 131 132 return true; 133} 134 135SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB, 136 MachineBasicBlock::iterator MI, 137 unsigned DestReg, 138 const Remat &RM, 139 const TargetRegisterInfo &tri, 140 bool Late) { 141 assert(RM.OrigMI && "Invalid remat"); 142 TII.reMaterialize(MBB, MI, DestReg, 0, RM.OrigMI, tri); 143 Rematted.insert(RM.ParentVNI); 144 return LIS.getSlotIndexes()->insertMachineInstrInMaps(--MI, Late) 145 .getRegSlot(); 146} 147 148void LiveRangeEdit::eraseVirtReg(unsigned Reg) { 149 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg)) 150 LIS.removeInterval(Reg); 151} 152 153bool LiveRangeEdit::foldAsLoad(LiveInterval *LI, 154 SmallVectorImpl<MachineInstr*> &Dead) { 155 MachineInstr *DefMI = 0, *UseMI = 0; 156 157 // Check that there is a single def and a single use. 158 for (MachineRegisterInfo::reg_nodbg_iterator I = MRI.reg_nodbg_begin(LI->reg), 159 E = MRI.reg_nodbg_end(); I != E; ++I) { 160 MachineOperand &MO = I.getOperand(); 161 MachineInstr *MI = MO.getParent(); 162 if (MO.isDef()) { 163 if (DefMI && DefMI != MI) 164 return false; 165 if (!MI->canFoldAsLoad()) 166 return false; 167 DefMI = MI; 168 } else if (!MO.isUndef()) { 169 if (UseMI && UseMI != MI) 170 return false; 171 // FIXME: Targets don't know how to fold subreg uses. 172 if (MO.getSubReg()) 173 return false; 174 UseMI = MI; 175 } 176 } 177 if (!DefMI || !UseMI) 178 return false; 179 180 DEBUG(dbgs() << "Try to fold single def: " << *DefMI 181 << " into single use: " << *UseMI); 182 183 SmallVector<unsigned, 8> Ops; 184 if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second) 185 return false; 186 187 MachineInstr *FoldMI = TII.foldMemoryOperand(UseMI, Ops, DefMI); 188 if (!FoldMI) 189 return false; 190 DEBUG(dbgs() << " folded: " << *FoldMI); 191 LIS.ReplaceMachineInstrInMaps(UseMI, FoldMI); 192 UseMI->eraseFromParent(); 193 DefMI->addRegisterDead(LI->reg, 0); 194 Dead.push_back(DefMI); 195 ++NumDCEFoldedLoads; 196 return true; 197} 198 199void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead, 200 ArrayRef<unsigned> RegsBeingSpilled) { 201 SetVector<LiveInterval*, 202 SmallVector<LiveInterval*, 8>, 203 SmallPtrSet<LiveInterval*, 8> > ToShrink; 204 205 for (;;) { 206 // Erase all dead defs. 207 while (!Dead.empty()) { 208 MachineInstr *MI = Dead.pop_back_val(); 209 assert(MI->allDefsAreDead() && "Def isn't really dead"); 210 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot(); 211 212 // Never delete inline asm. 213 if (MI->isInlineAsm()) { 214 DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI); 215 continue; 216 } 217 218 // Use the same criteria as DeadMachineInstructionElim. 219 bool SawStore = false; 220 if (!MI->isSafeToMove(&TII, 0, SawStore)) { 221 DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI); 222 continue; 223 } 224 225 DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI); 226 227 // Collect virtual registers to be erased after MI is gone. 228 SmallVector<unsigned, 8> RegsToErase; 229 230 // Check for live intervals that may shrink 231 for (MachineInstr::mop_iterator MOI = MI->operands_begin(), 232 MOE = MI->operands_end(); MOI != MOE; ++MOI) { 233 if (!MOI->isReg()) 234 continue; 235 unsigned Reg = MOI->getReg(); 236 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 237 continue; 238 LiveInterval &LI = LIS.getInterval(Reg); 239 240 // Shrink read registers, unless it is likely to be expensive and 241 // unlikely to change anything. We typically don't want to shrink the 242 // PIC base register that has lots of uses everywhere. 243 // Always shrink COPY uses that probably come from live range splitting. 244 if (MI->readsVirtualRegister(Reg) && 245 (MI->isCopy() || MOI->isDef() || MRI.hasOneNonDBGUse(Reg) || 246 LI.killedAt(Idx))) 247 ToShrink.insert(&LI); 248 249 // Remove defined value. 250 if (MOI->isDef()) { 251 if (VNInfo *VNI = LI.getVNInfoAt(Idx)) { 252 if (TheDelegate) 253 TheDelegate->LRE_WillShrinkVirtReg(LI.reg); 254 LI.removeValNo(VNI); 255 if (LI.empty()) 256 RegsToErase.push_back(Reg); 257 } 258 } 259 } 260 261 if (TheDelegate) 262 TheDelegate->LRE_WillEraseInstruction(MI); 263 LIS.RemoveMachineInstrFromMaps(MI); 264 MI->eraseFromParent(); 265 ++NumDCEDeleted; 266 267 // Erase any virtregs that are now empty and unused. There may be <undef> 268 // uses around. Keep the empty live range in that case. 269 for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) { 270 unsigned Reg = RegsToErase[i]; 271 if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) { 272 ToShrink.remove(&LIS.getInterval(Reg)); 273 eraseVirtReg(Reg); 274 } 275 } 276 } 277 278 if (ToShrink.empty()) 279 break; 280 281 // Shrink just one live interval. Then delete new dead defs. 282 LiveInterval *LI = ToShrink.back(); 283 ToShrink.pop_back(); 284 if (foldAsLoad(LI, Dead)) 285 continue; 286 if (TheDelegate) 287 TheDelegate->LRE_WillShrinkVirtReg(LI->reg); 288 if (!LIS.shrinkToUses(LI, &Dead)) 289 continue; 290 291 // Don't create new intervals for a register being spilled. 292 // The new intervals would have to be spilled anyway so its not worth it. 293 // Also they currently aren't spilled so creating them and not spilling 294 // them results in incorrect code. 295 bool BeingSpilled = false; 296 for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) { 297 if (LI->reg == RegsBeingSpilled[i]) { 298 BeingSpilled = true; 299 break; 300 } 301 } 302 303 if (BeingSpilled) continue; 304 305 // LI may have been separated, create new intervals. 306 LI->RenumberValues(LIS); 307 ConnectedVNInfoEqClasses ConEQ(LIS); 308 unsigned NumComp = ConEQ.Classify(LI); 309 if (NumComp <= 1) 310 continue; 311 ++NumFracRanges; 312 bool IsOriginal = VRM && VRM->getOriginal(LI->reg) == LI->reg; 313 DEBUG(dbgs() << NumComp << " components: " << *LI << '\n'); 314 SmallVector<LiveInterval*, 8> Dups(1, LI); 315 for (unsigned i = 1; i != NumComp; ++i) { 316 Dups.push_back(&createFrom(LI->reg)); 317 // If LI is an original interval that hasn't been split yet, make the new 318 // intervals their own originals instead of referring to LI. The original 319 // interval must contain all the split products, and LI doesn't. 320 if (IsOriginal) 321 VRM->setIsSplitFromReg(Dups.back()->reg, 0); 322 if (TheDelegate) 323 TheDelegate->LRE_DidCloneVirtReg(Dups.back()->reg, LI->reg); 324 } 325 ConEQ.Distribute(&Dups[0], MRI); 326 DEBUG({ 327 for (unsigned i = 0; i != NumComp; ++i) 328 dbgs() << '\t' << *Dups[i] << '\n'; 329 }); 330 } 331} 332 333void LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF, 334 const MachineLoopInfo &Loops) { 335 VirtRegAuxInfo VRAI(MF, LIS, Loops); 336 for (iterator I = begin(), E = end(); I != E; ++I) { 337 LiveInterval &LI = **I; 338 if (MRI.recomputeRegClass(LI.reg, MF.getTarget())) 339 DEBUG(dbgs() << "Inflated " << PrintReg(LI.reg) << " to " 340 << MRI.getRegClass(LI.reg)->getName() << '\n'); 341 VRAI.CalculateWeightAndHint(LI); 342 } 343} 344