/3rd_party/llvm/lib/CodeGen/LiveRangeCalc.cpp

https://code.google.com/p/softart/ · C++ · 376 lines · 239 code · 54 blank · 83 comment · 87 complexity · 6122c61d236408d7dd018cf21d0ab0e1 MD5 · raw file

  1. //===---- LiveRangeCalc.cpp - Calculate live ranges -----------------------===//
  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. // Implementation of the LiveRangeCalc class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "regalloc"
  14. #include "LiveRangeCalc.h"
  15. #include "llvm/CodeGen/MachineDominators.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. using namespace llvm;
  18. void LiveRangeCalc::reset(const MachineFunction *mf,
  19. SlotIndexes *SI,
  20. MachineDominatorTree *MDT,
  21. VNInfo::Allocator *VNIA) {
  22. MF = mf;
  23. MRI = &MF->getRegInfo();
  24. Indexes = SI;
  25. DomTree = MDT;
  26. Alloc = VNIA;
  27. unsigned N = MF->getNumBlockIDs();
  28. Seen.clear();
  29. Seen.resize(N);
  30. LiveOut.resize(N);
  31. LiveIn.clear();
  32. }
  33. void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
  34. assert(MRI && Indexes && "call reset() first");
  35. // Visit all def operands. If the same instruction has multiple defs of Reg,
  36. // LR.createDeadDef() will deduplicate.
  37. for (MachineRegisterInfo::def_iterator
  38. I = MRI->def_begin(Reg), E = MRI->def_end(); I != E; ++I) {
  39. const MachineInstr *MI = &*I;
  40. // Find the corresponding slot index.
  41. SlotIndex Idx;
  42. if (MI->isPHI())
  43. // PHI defs begin at the basic block start index.
  44. Idx = Indexes->getMBBStartIdx(MI->getParent());
  45. else
  46. // Instructions are either normal 'r', or early clobber 'e'.
  47. Idx = Indexes->getInstructionIndex(MI)
  48. .getRegSlot(I.getOperand().isEarlyClobber());
  49. // Create the def in LR. This may find an existing def.
  50. LR.createDeadDef(Idx, *Alloc);
  51. }
  52. }
  53. void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg) {
  54. assert(MRI && Indexes && "call reset() first");
  55. // Visit all operands that read Reg. This may include partial defs.
  56. for (MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(Reg),
  57. E = MRI->reg_nodbg_end(); I != E; ++I) {
  58. MachineOperand &MO = I.getOperand();
  59. // Clear all kill flags. They will be reinserted after register allocation
  60. // by LiveIntervalAnalysis::addKillFlags().
  61. if (MO.isUse())
  62. MO.setIsKill(false);
  63. if (!MO.readsReg())
  64. continue;
  65. // MI is reading Reg. We may have visited MI before if it happens to be
  66. // reading Reg multiple times. That is OK, extend() is idempotent.
  67. const MachineInstr *MI = &*I;
  68. // Find the SlotIndex being read.
  69. SlotIndex Idx;
  70. if (MI->isPHI()) {
  71. assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
  72. // PHI operands are paired: (Reg, PredMBB).
  73. // Extend the live range to be live-out from PredMBB.
  74. Idx = Indexes->getMBBEndIdx(MI->getOperand(I.getOperandNo()+1).getMBB());
  75. } else {
  76. // This is a normal instruction.
  77. Idx = Indexes->getInstructionIndex(MI).getRegSlot();
  78. // Check for early-clobber redefs.
  79. unsigned DefIdx;
  80. if (MO.isDef()) {
  81. if (MO.isEarlyClobber())
  82. Idx = Idx.getRegSlot(true);
  83. } else if (MI->isRegTiedToDefOperand(I.getOperandNo(), &DefIdx)) {
  84. // FIXME: This would be a lot easier if tied early-clobber uses also
  85. // had an early-clobber flag.
  86. if (MI->getOperand(DefIdx).isEarlyClobber())
  87. Idx = Idx.getRegSlot(true);
  88. }
  89. }
  90. extend(LR, Idx, Reg);
  91. }
  92. }
  93. // Transfer information from the LiveIn vector to the live ranges.
  94. void LiveRangeCalc::updateLiveIns() {
  95. LiveRangeUpdater Updater;
  96. for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
  97. E = LiveIn.end(); I != E; ++I) {
  98. if (!I->DomNode)
  99. continue;
  100. MachineBasicBlock *MBB = I->DomNode->getBlock();
  101. assert(I->Value && "No live-in value found");
  102. SlotIndex Start, End;
  103. tie(Start, End) = Indexes->getMBBRange(MBB);
  104. if (I->Kill.isValid())
  105. // Value is killed inside this block.
  106. End = I->Kill;
  107. else {
  108. // The value is live-through, update LiveOut as well.
  109. // Defer the Domtree lookup until it is needed.
  110. assert(Seen.test(MBB->getNumber()));
  111. LiveOut[MBB] = LiveOutPair(I->Value, (MachineDomTreeNode *)0);
  112. }
  113. Updater.setDest(&I->LR);
  114. Updater.add(Start, End, I->Value);
  115. }
  116. LiveIn.clear();
  117. }
  118. void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg) {
  119. assert(Kill.isValid() && "Invalid SlotIndex");
  120. assert(Indexes && "Missing SlotIndexes");
  121. assert(DomTree && "Missing dominator tree");
  122. MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot());
  123. assert(KillMBB && "No MBB at Kill");
  124. // Is there a def in the same MBB we can extend?
  125. if (LR.extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill))
  126. return;
  127. // Find the single reaching def, or determine if Kill is jointly dominated by
  128. // multiple values, and we may need to create even more phi-defs to preserve
  129. // VNInfo SSA form. Perform a search for all predecessor blocks where we
  130. // know the dominating VNInfo.
  131. if (findReachingDefs(LR, *KillMBB, Kill, PhysReg))
  132. return;
  133. // When there were multiple different values, we may need new PHIs.
  134. calculateValues();
  135. }
  136. // This function is called by a client after using the low-level API to add
  137. // live-out and live-in blocks. The unique value optimization is not
  138. // available, SplitEditor::transferValues handles that case directly anyway.
  139. void LiveRangeCalc::calculateValues() {
  140. assert(Indexes && "Missing SlotIndexes");
  141. assert(DomTree && "Missing dominator tree");
  142. updateSSA();
  143. updateLiveIns();
  144. }
  145. bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &KillMBB,
  146. SlotIndex Kill, unsigned PhysReg) {
  147. unsigned KillMBBNum = KillMBB.getNumber();
  148. // Block numbers where LR should be live-in.
  149. SmallVector<unsigned, 16> WorkList(1, KillMBBNum);
  150. // Remember if we have seen more than one value.
  151. bool UniqueVNI = true;
  152. VNInfo *TheVNI = 0;
  153. // Using Seen as a visited set, perform a BFS for all reaching defs.
  154. for (unsigned i = 0; i != WorkList.size(); ++i) {
  155. MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
  156. #ifndef NDEBUG
  157. if (MBB->pred_empty()) {
  158. MBB->getParent()->verify();
  159. llvm_unreachable("Use not jointly dominated by defs.");
  160. }
  161. if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
  162. !MBB->isLiveIn(PhysReg)) {
  163. MBB->getParent()->verify();
  164. errs() << "The register needs to be live in to BB#" << MBB->getNumber()
  165. << ", but is missing from the live-in list.\n";
  166. llvm_unreachable("Invalid global physical register");
  167. }
  168. #endif
  169. for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
  170. PE = MBB->pred_end(); PI != PE; ++PI) {
  171. MachineBasicBlock *Pred = *PI;
  172. // Is this a known live-out block?
  173. if (Seen.test(Pred->getNumber())) {
  174. if (VNInfo *VNI = LiveOut[Pred].first) {
  175. if (TheVNI && TheVNI != VNI)
  176. UniqueVNI = false;
  177. TheVNI = VNI;
  178. }
  179. continue;
  180. }
  181. SlotIndex Start, End;
  182. tie(Start, End) = Indexes->getMBBRange(Pred);
  183. // First time we see Pred. Try to determine the live-out value, but set
  184. // it as null if Pred is live-through with an unknown value.
  185. VNInfo *VNI = LR.extendInBlock(Start, End);
  186. setLiveOutValue(Pred, VNI);
  187. if (VNI) {
  188. if (TheVNI && TheVNI != VNI)
  189. UniqueVNI = false;
  190. TheVNI = VNI;
  191. continue;
  192. }
  193. // No, we need a live-in value for Pred as well
  194. if (Pred != &KillMBB)
  195. WorkList.push_back(Pred->getNumber());
  196. else
  197. // Loopback to KillMBB, so value is really live through.
  198. Kill = SlotIndex();
  199. }
  200. }
  201. LiveIn.clear();
  202. // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
  203. // neither require it. Skip the sorting overhead for small updates.
  204. if (WorkList.size() > 4)
  205. array_pod_sort(WorkList.begin(), WorkList.end());
  206. // If a unique reaching def was found, blit in the live ranges immediately.
  207. if (UniqueVNI) {
  208. LiveRangeUpdater Updater(&LR);
  209. for (SmallVectorImpl<unsigned>::const_iterator I = WorkList.begin(),
  210. E = WorkList.end(); I != E; ++I) {
  211. SlotIndex Start, End;
  212. tie(Start, End) = Indexes->getMBBRange(*I);
  213. // Trim the live range in KillMBB.
  214. if (*I == KillMBBNum && Kill.isValid())
  215. End = Kill;
  216. else
  217. LiveOut[MF->getBlockNumbered(*I)] =
  218. LiveOutPair(TheVNI, (MachineDomTreeNode *)0);
  219. Updater.add(Start, End, TheVNI);
  220. }
  221. return true;
  222. }
  223. // Multiple values were found, so transfer the work list to the LiveIn array
  224. // where UpdateSSA will use it as a work list.
  225. LiveIn.reserve(WorkList.size());
  226. for (SmallVectorImpl<unsigned>::const_iterator
  227. I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
  228. MachineBasicBlock *MBB = MF->getBlockNumbered(*I);
  229. addLiveInBlock(LR, DomTree->getNode(MBB));
  230. if (MBB == &KillMBB)
  231. LiveIn.back().Kill = Kill;
  232. }
  233. return false;
  234. }
  235. // This is essentially the same iterative algorithm that SSAUpdater uses,
  236. // except we already have a dominator tree, so we don't have to recompute it.
  237. void LiveRangeCalc::updateSSA() {
  238. assert(Indexes && "Missing SlotIndexes");
  239. assert(DomTree && "Missing dominator tree");
  240. // Interate until convergence.
  241. unsigned Changes;
  242. do {
  243. Changes = 0;
  244. // Propagate live-out values down the dominator tree, inserting phi-defs
  245. // when necessary.
  246. for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
  247. E = LiveIn.end(); I != E; ++I) {
  248. MachineDomTreeNode *Node = I->DomNode;
  249. // Skip block if the live-in value has already been determined.
  250. if (!Node)
  251. continue;
  252. MachineBasicBlock *MBB = Node->getBlock();
  253. MachineDomTreeNode *IDom = Node->getIDom();
  254. LiveOutPair IDomValue;
  255. // We need a live-in value to a block with no immediate dominator?
  256. // This is probably an unreachable block that has survived somehow.
  257. bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
  258. // IDom dominates all of our predecessors, but it may not be their
  259. // immediate dominator. Check if any of them have live-out values that are
  260. // properly dominated by IDom. If so, we need a phi-def here.
  261. if (!needPHI) {
  262. IDomValue = LiveOut[IDom->getBlock()];
  263. // Cache the DomTree node that defined the value.
  264. if (IDomValue.first && !IDomValue.second)
  265. LiveOut[IDom->getBlock()].second = IDomValue.second =
  266. DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
  267. for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
  268. PE = MBB->pred_end(); PI != PE; ++PI) {
  269. LiveOutPair &Value = LiveOut[*PI];
  270. if (!Value.first || Value.first == IDomValue.first)
  271. continue;
  272. // Cache the DomTree node that defined the value.
  273. if (!Value.second)
  274. Value.second =
  275. DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
  276. // This predecessor is carrying something other than IDomValue.
  277. // It could be because IDomValue hasn't propagated yet, or it could be
  278. // because MBB is in the dominance frontier of that value.
  279. if (DomTree->dominates(IDom, Value.second)) {
  280. needPHI = true;
  281. break;
  282. }
  283. }
  284. }
  285. // The value may be live-through even if Kill is set, as can happen when
  286. // we are called from extendRange. In that case LiveOutSeen is true, and
  287. // LiveOut indicates a foreign or missing value.
  288. LiveOutPair &LOP = LiveOut[MBB];
  289. // Create a phi-def if required.
  290. if (needPHI) {
  291. ++Changes;
  292. assert(Alloc && "Need VNInfo allocator to create PHI-defs");
  293. SlotIndex Start, End;
  294. tie(Start, End) = Indexes->getMBBRange(MBB);
  295. LiveRange &LR = I->LR;
  296. VNInfo *VNI = LR.getNextValue(Start, *Alloc);
  297. I->Value = VNI;
  298. // This block is done, we know the final value.
  299. I->DomNode = 0;
  300. // Add liveness since updateLiveIns now skips this node.
  301. if (I->Kill.isValid())
  302. LR.addSegment(LiveInterval::Segment(Start, I->Kill, VNI));
  303. else {
  304. LR.addSegment(LiveInterval::Segment(Start, End, VNI));
  305. LOP = LiveOutPair(VNI, Node);
  306. }
  307. } else if (IDomValue.first) {
  308. // No phi-def here. Remember incoming value.
  309. I->Value = IDomValue.first;
  310. // If the IDomValue is killed in the block, don't propagate through.
  311. if (I->Kill.isValid())
  312. continue;
  313. // Propagate IDomValue if it isn't killed:
  314. // MBB is live-out and doesn't define its own value.
  315. if (LOP.first == IDomValue.first)
  316. continue;
  317. ++Changes;
  318. LOP = IDomValue;
  319. }
  320. }
  321. } while (Changes);
  322. }