1 //===-- LiveRangeShrink.cpp - Move instructions to shrink 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 /// \file 11 /// This pass moves instructions close to the definition of its operands to 12 /// shrink live range of the def instruction. The code motion is limited within 13 /// the basic block. The moved instruction should have 1 def, and more than one 14 /// uses, all of which are the only use of the def. 15 /// 16 ///===---------------------------------------------------------------------===// 17 #include "llvm/CodeGen/MachineFunctionPass.h" 18 #include "llvm/CodeGen/MachineRegisterInfo.h" 19 #include "llvm/CodeGen/Passes.h" 20 #include "llvm/ADT/Statistic.h" 21 #include "llvm/Support/Debug.h" 22 23 #define DEBUG_TYPE "lrshrink" 24 25 STATISTIC(NumInstrsHoistedToShrinkLiveRange, 26 "Number of insructions hoisted to shrink live range."); 27 28 using namespace llvm; 29 30 namespace { 31 class LiveRangeShrink : public MachineFunctionPass { 32 public: 33 static char ID; 34 35 LiveRangeShrink() : MachineFunctionPass(ID) { 36 initializeLiveRangeShrinkPass(*PassRegistry::getPassRegistry()); 37 } 38 39 void getAnalysisUsage(AnalysisUsage &AU) const override { 40 AU.setPreservesCFG(); 41 MachineFunctionPass::getAnalysisUsage(AU); 42 } 43 44 StringRef getPassName() const override { return "Live Range Shrink"; } 45 46 bool runOnMachineFunction(MachineFunction &MF) override; 47 }; 48 } // End anonymous namespace. 49 50 char LiveRangeShrink::ID = 0; 51 char &llvm::LiveRangeShrinkID = LiveRangeShrink::ID; 52 53 INITIALIZE_PASS(LiveRangeShrink, "lrshrink", "Live Range Shrink Pass", false, 54 false) 55 namespace { 56 typedef DenseMap<MachineInstr *, unsigned> InstOrderMap; 57 58 /// Returns \p New if it's dominated by \p Old, otherwise return \p Old. 59 /// \p M maintains a map from instruction to its dominating order that satisfies 60 /// M[A] > M[B] guarantees that A is dominated by B. 61 /// If \p New is not in \p M, return \p Old. Otherwise if \p Old is null, return 62 /// \p New. 63 MachineInstr *FindDominatedInstruction(MachineInstr &New, MachineInstr *Old, 64 const InstOrderMap &M) { 65 auto NewIter = M.find(&New); 66 if (NewIter == M.end()) 67 return Old; 68 if (Old == nullptr) 69 return &New; 70 unsigned OrderOld = M.find(Old)->second; 71 unsigned OrderNew = NewIter->second; 72 if (OrderOld != OrderNew) 73 return OrderOld < OrderNew ? &New : Old; 74 // OrderOld == OrderNew, we need to iterate down from Old to see if it 75 // can reach New, if yes, New is dominated by Old. 76 for (MachineInstr *I = Old->getNextNode(); M.find(I)->second == OrderNew; 77 I = I->getNextNode()) 78 if (I == &New) 79 return &New; 80 return Old; 81 } 82 83 /// Builds Instruction to its dominating order number map \p M by traversing 84 /// from instruction \p Start. 85 void BuildInstOrderMap(MachineBasicBlock::iterator Start, InstOrderMap &M) { 86 M.clear(); 87 unsigned i = 0; 88 for (MachineInstr &I : make_range(Start, Start->getParent()->end())) 89 M[&I] = i++; 90 } 91 } // end anonymous namespace 92 93 bool LiveRangeShrink::runOnMachineFunction(MachineFunction &MF) { 94 if (skipFunction(*MF.getFunction())) 95 return false; 96 97 MachineRegisterInfo &MRI = MF.getRegInfo(); 98 99 DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n'); 100 101 InstOrderMap IOM; 102 // Map from register to instruction order (value of IOM) where the 103 // register is used last. When moving instructions up, we need to 104 // make sure all its defs (including dead def) will not cross its 105 // last use when moving up. 106 DenseMap<unsigned, unsigned> UseMap; 107 108 for (MachineBasicBlock &MBB : MF) { 109 if (MBB.empty()) 110 continue; 111 bool SawStore = false; 112 BuildInstOrderMap(MBB.begin(), IOM); 113 UseMap.clear(); 114 115 for (MachineBasicBlock::iterator Next = MBB.begin(); Next != MBB.end();) { 116 MachineInstr &MI = *Next; 117 ++Next; 118 if (MI.isPHI() || MI.isDebugValue()) 119 continue; 120 if (MI.mayStore()) 121 SawStore = true; 122 123 unsigned CurrentOrder = IOM[&MI]; 124 unsigned Barrier = 0; 125 for (const MachineOperand &MO : MI.operands()) { 126 if (!MO.isReg() || MO.isDebug()) 127 continue; 128 if (MO.isUse()) 129 UseMap[MO.getReg()] = CurrentOrder; 130 else if (MO.isDead() && UseMap.count(MO.getReg())) 131 // Barrier is the last instruction where MO get used. MI should not 132 // be moved above Barrier. 133 Barrier = std::max(Barrier, UseMap[MO.getReg()]); 134 } 135 136 if (!MI.isSafeToMove(nullptr, SawStore)) { 137 // If MI has side effects, it should become a barrier for code motion. 138 // IOM is rebuild from the next instruction to prevent later 139 // instructions from being moved before this MI. 140 if (MI.hasUnmodeledSideEffects() && Next != MBB.end()) { 141 BuildInstOrderMap(Next, IOM); 142 SawStore = false; 143 } 144 continue; 145 } 146 147 const MachineOperand *DefMO = nullptr; 148 MachineInstr *Insert = nullptr; 149 150 // Number of live-ranges that will be shortened. We do not count 151 // live-ranges that are defined by a COPY as it could be coalesced later. 152 unsigned NumEligibleUse = 0; 153 154 for (const MachineOperand &MO : MI.operands()) { 155 if (!MO.isReg() || MO.isDead() || MO.isDebug()) 156 continue; 157 unsigned Reg = MO.getReg(); 158 // Do not move the instruction if it def/uses a physical register, 159 // unless it is a constant physical register. 160 if (TargetRegisterInfo::isPhysicalRegister(Reg) && 161 !MRI.isConstantPhysReg(Reg)) { 162 Insert = nullptr; 163 break; 164 } 165 if (MO.isDef()) { 166 // Do not move if there is more than one def. 167 if (DefMO) { 168 Insert = nullptr; 169 break; 170 } 171 DefMO = &MO; 172 } else if (MRI.hasOneNonDBGUse(Reg) && MRI.hasOneDef(Reg)) { 173 MachineInstr &DefInstr = *MRI.def_instr_begin(Reg); 174 if (!DefInstr.isCopy()) 175 NumEligibleUse++; 176 Insert = FindDominatedInstruction(DefInstr, Insert, IOM); 177 } else { 178 Insert = nullptr; 179 break; 180 } 181 } 182 // Move the instruction when # of shrunk live range > 1. 183 if (DefMO && Insert && NumEligibleUse > 1 && Barrier <= IOM[Insert]) { 184 MachineBasicBlock::iterator I = std::next(Insert->getIterator()); 185 // Skip all the PHI and debug instructions. 186 while (I != MBB.end() && (I->isPHI() || I->isDebugValue())) 187 I = std::next(I); 188 if (I == MI.getIterator()) 189 continue; 190 191 // Update the dominator order to be the same as the insertion point. 192 // We do this to maintain a non-decreasing order without need to update 193 // all instruction orders after the insertion point. 194 unsigned NewOrder = IOM[&*I]; 195 IOM[&MI] = NewOrder; 196 NumInstrsHoistedToShrinkLiveRange++; 197 198 // Find MI's debug value following MI. 199 MachineBasicBlock::iterator EndIter = std::next(MI.getIterator()); 200 if (MI.getOperand(0).isReg()) 201 for (; EndIter != MBB.end() && EndIter->isDebugValue() && 202 EndIter->getOperand(0).isReg() && 203 EndIter->getOperand(0).getReg() == MI.getOperand(0).getReg(); 204 ++EndIter, ++Next) 205 IOM[&*EndIter] = NewOrder; 206 MBB.splice(I, &MBB, MI.getIterator(), EndIter); 207 } 208 } 209 } 210 return false; 211 } 212