RDFLiveness.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. //===- RDFLiveness.cpp ----------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Computation of the liveness information from the data-flow graph.
  10. //
  11. // The main functionality of this code is to compute block live-in
  12. // information. With the live-in information in place, the placement
  13. // of kill flags can also be recalculated.
  14. //
  15. // The block live-in calculation is based on the ideas from the following
  16. // publication:
  17. //
  18. // Dibyendu Das, Ramakrishna Upadrasta, Benoit Dupont de Dinechin.
  19. // "Efficient Liveness Computation Using Merge Sets and DJ-Graphs."
  20. // ACM Transactions on Architecture and Code Optimization, Association for
  21. // Computing Machinery, 2012, ACM TACO Special Issue on "High-Performance
  22. // and Embedded Architectures and Compilers", 8 (4),
  23. // <10.1145/2086696.2086706>. <hal-00647369>
  24. //
  25. #include "llvm/ADT/BitVector.h"
  26. #include "llvm/ADT/DenseMap.h"
  27. #include "llvm/ADT/STLExtras.h"
  28. #include "llvm/ADT/SetVector.h"
  29. #include "llvm/ADT/SmallSet.h"
  30. #include "llvm/CodeGen/MachineBasicBlock.h"
  31. #include "llvm/CodeGen/MachineDominanceFrontier.h"
  32. #include "llvm/CodeGen/MachineDominators.h"
  33. #include "llvm/CodeGen/MachineFunction.h"
  34. #include "llvm/CodeGen/MachineInstr.h"
  35. #include "llvm/CodeGen/RDFLiveness.h"
  36. #include "llvm/CodeGen/RDFGraph.h"
  37. #include "llvm/CodeGen/RDFRegisters.h"
  38. #include "llvm/CodeGen/TargetRegisterInfo.h"
  39. #include "llvm/MC/LaneBitmask.h"
  40. #include "llvm/MC/MCRegisterInfo.h"
  41. #include "llvm/Support/CommandLine.h"
  42. #include "llvm/Support/Debug.h"
  43. #include "llvm/Support/ErrorHandling.h"
  44. #include "llvm/Support/raw_ostream.h"
  45. #include <algorithm>
  46. #include <cassert>
  47. #include <cstdint>
  48. #include <iterator>
  49. #include <map>
  50. #include <unordered_map>
  51. #include <utility>
  52. #include <vector>
  53. using namespace llvm;
  54. using namespace rdf;
  55. static cl::opt<unsigned> MaxRecNest("rdf-liveness-max-rec", cl::init(25),
  56. cl::Hidden, cl::desc("Maximum recursion level"));
  57. namespace llvm {
  58. namespace rdf {
  59. raw_ostream &operator<< (raw_ostream &OS, const Print<Liveness::RefMap> &P) {
  60. OS << '{';
  61. for (auto &I : P.Obj) {
  62. OS << ' ' << printReg(I.first, &P.G.getTRI()) << '{';
  63. for (auto J = I.second.begin(), E = I.second.end(); J != E; ) {
  64. OS << Print<NodeId>(J->first, P.G) << PrintLaneMaskOpt(J->second);
  65. if (++J != E)
  66. OS << ',';
  67. }
  68. OS << '}';
  69. }
  70. OS << " }";
  71. return OS;
  72. }
  73. } // end namespace rdf
  74. } // end namespace llvm
  75. // The order in the returned sequence is the order of reaching defs in the
  76. // upward traversal: the first def is the closest to the given reference RefA,
  77. // the next one is further up, and so on.
  78. // The list ends at a reaching phi def, or when the reference from RefA is
  79. // covered by the defs in the list (see FullChain).
  80. // This function provides two modes of operation:
  81. // (1) Returning the sequence of reaching defs for a particular reference
  82. // node. This sequence will terminate at the first phi node [1].
  83. // (2) Returning a partial sequence of reaching defs, where the final goal
  84. // is to traverse past phi nodes to the actual defs arising from the code
  85. // itself.
  86. // In mode (2), the register reference for which the search was started
  87. // may be different from the reference node RefA, for which this call was
  88. // made, hence the argument RefRR, which holds the original register.
  89. // Also, some definitions may have already been encountered in a previous
  90. // call that will influence register covering. The register references
  91. // already defined are passed in through DefRRs.
  92. // In mode (1), the "continuation" considerations do not apply, and the
  93. // RefRR is the same as the register in RefA, and the set DefRRs is empty.
  94. //
  95. // [1] It is possible for multiple phi nodes to be included in the returned
  96. // sequence:
  97. // SubA = phi ...
  98. // SubB = phi ...
  99. // ... = SuperAB(rdef:SubA), SuperAB"(rdef:SubB)
  100. // However, these phi nodes are independent from one another in terms of
  101. // the data-flow.
  102. NodeList Liveness::getAllReachingDefs(RegisterRef RefRR,
  103. NodeAddr<RefNode*> RefA, bool TopShadows, bool FullChain,
  104. const RegisterAggr &DefRRs) {
  105. NodeList RDefs; // Return value.
  106. SetVector<NodeId> DefQ;
  107. DenseMap<MachineInstr*, uint32_t> OrdMap;
  108. // Dead defs will be treated as if they were live, since they are actually
  109. // on the data-flow path. They cannot be ignored because even though they
  110. // do not generate meaningful values, they still modify registers.
  111. // If the reference is undefined, there is nothing to do.
  112. if (RefA.Addr->getFlags() & NodeAttrs::Undef)
  113. return RDefs;
  114. // The initial queue should not have reaching defs for shadows. The
  115. // whole point of a shadow is that it will have a reaching def that
  116. // is not aliased to the reaching defs of the related shadows.
  117. NodeId Start = RefA.Id;
  118. auto SNA = DFG.addr<RefNode*>(Start);
  119. if (NodeId RD = SNA.Addr->getReachingDef())
  120. DefQ.insert(RD);
  121. if (TopShadows) {
  122. for (auto S : DFG.getRelatedRefs(RefA.Addr->getOwner(DFG), RefA))
  123. if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
  124. DefQ.insert(RD);
  125. }
  126. // Collect all the reaching defs, going up until a phi node is encountered,
  127. // or there are no more reaching defs. From this set, the actual set of
  128. // reaching defs will be selected.
  129. // The traversal upwards must go on until a covering def is encountered.
  130. // It is possible that a collection of non-covering (individually) defs
  131. // will be sufficient, but keep going until a covering one is found.
  132. for (unsigned i = 0; i < DefQ.size(); ++i) {
  133. auto TA = DFG.addr<DefNode*>(DefQ[i]);
  134. if (TA.Addr->getFlags() & NodeAttrs::PhiRef)
  135. continue;
  136. // Stop at the covering/overwriting def of the initial register reference.
  137. RegisterRef RR = TA.Addr->getRegRef(DFG);
  138. if (!DFG.IsPreservingDef(TA))
  139. if (RegisterAggr::isCoverOf(RR, RefRR, PRI))
  140. continue;
  141. // Get the next level of reaching defs. This will include multiple
  142. // reaching defs for shadows.
  143. for (auto S : DFG.getRelatedRefs(TA.Addr->getOwner(DFG), TA))
  144. if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
  145. DefQ.insert(RD);
  146. // Don't visit sibling defs. They share the same reaching def (which
  147. // will be visited anyway), but they define something not aliased to
  148. // this ref.
  149. }
  150. // Return the MachineBasicBlock containing a given instruction.
  151. auto Block = [this] (NodeAddr<InstrNode*> IA) -> MachineBasicBlock* {
  152. if (IA.Addr->getKind() == NodeAttrs::Stmt)
  153. return NodeAddr<StmtNode*>(IA).Addr->getCode()->getParent();
  154. assert(IA.Addr->getKind() == NodeAttrs::Phi);
  155. NodeAddr<PhiNode*> PA = IA;
  156. NodeAddr<BlockNode*> BA = PA.Addr->getOwner(DFG);
  157. return BA.Addr->getCode();
  158. };
  159. SmallSet<NodeId,32> Defs;
  160. // Remove all non-phi defs that are not aliased to RefRR, and separate
  161. // the the remaining defs into buckets for containing blocks.
  162. std::map<NodeId, NodeAddr<InstrNode*>> Owners;
  163. std::map<MachineBasicBlock*, SmallVector<NodeId,32>> Blocks;
  164. for (NodeId N : DefQ) {
  165. auto TA = DFG.addr<DefNode*>(N);
  166. bool IsPhi = TA.Addr->getFlags() & NodeAttrs::PhiRef;
  167. if (!IsPhi && !PRI.alias(RefRR, TA.Addr->getRegRef(DFG)))
  168. continue;
  169. Defs.insert(TA.Id);
  170. NodeAddr<InstrNode*> IA = TA.Addr->getOwner(DFG);
  171. Owners[TA.Id] = IA;
  172. Blocks[Block(IA)].push_back(IA.Id);
  173. }
  174. auto Precedes = [this,&OrdMap] (NodeId A, NodeId B) {
  175. if (A == B)
  176. return false;
  177. NodeAddr<InstrNode*> OA = DFG.addr<InstrNode*>(A);
  178. NodeAddr<InstrNode*> OB = DFG.addr<InstrNode*>(B);
  179. bool StmtA = OA.Addr->getKind() == NodeAttrs::Stmt;
  180. bool StmtB = OB.Addr->getKind() == NodeAttrs::Stmt;
  181. if (StmtA && StmtB) {
  182. const MachineInstr *InA = NodeAddr<StmtNode*>(OA).Addr->getCode();
  183. const MachineInstr *InB = NodeAddr<StmtNode*>(OB).Addr->getCode();
  184. assert(InA->getParent() == InB->getParent());
  185. auto FA = OrdMap.find(InA);
  186. if (FA != OrdMap.end())
  187. return FA->second < OrdMap.find(InB)->second;
  188. const MachineBasicBlock *BB = InA->getParent();
  189. for (auto It = BB->begin(), E = BB->end(); It != E; ++It) {
  190. if (It == InA->getIterator())
  191. return true;
  192. if (It == InB->getIterator())
  193. return false;
  194. }
  195. llvm_unreachable("InA and InB should be in the same block");
  196. }
  197. // One of them is a phi node.
  198. if (!StmtA && !StmtB) {
  199. // Both are phis, which are unordered. Break the tie by id numbers.
  200. return A < B;
  201. }
  202. // Only one of them is a phi. Phis always precede statements.
  203. return !StmtA;
  204. };
  205. auto GetOrder = [&OrdMap] (MachineBasicBlock &B) {
  206. uint32_t Pos = 0;
  207. for (MachineInstr &In : B)
  208. OrdMap.insert({&In, ++Pos});
  209. };
  210. // For each block, sort the nodes in it.
  211. std::vector<MachineBasicBlock*> TmpBB;
  212. for (auto &Bucket : Blocks) {
  213. TmpBB.push_back(Bucket.first);
  214. if (Bucket.second.size() > 2)
  215. GetOrder(*Bucket.first);
  216. llvm::sort(Bucket.second, Precedes);
  217. }
  218. // Sort the blocks with respect to dominance.
  219. llvm::sort(TmpBB,
  220. [this](auto A, auto B) { return MDT.properlyDominates(A, B); });
  221. std::vector<NodeId> TmpInst;
  222. for (MachineBasicBlock *MBB : llvm::reverse(TmpBB)) {
  223. auto &Bucket = Blocks[MBB];
  224. TmpInst.insert(TmpInst.end(), Bucket.rbegin(), Bucket.rend());
  225. }
  226. // The vector is a list of instructions, so that defs coming from
  227. // the same instruction don't need to be artificially ordered.
  228. // Then, when computing the initial segment, and iterating over an
  229. // instruction, pick the defs that contribute to the covering (i.e. is
  230. // not covered by previously added defs). Check the defs individually,
  231. // i.e. first check each def if is covered or not (without adding them
  232. // to the tracking set), and then add all the selected ones.
  233. // The reason for this is this example:
  234. // *d1<A>, *d2<B>, ... Assume A and B are aliased (can happen in phi nodes).
  235. // *d3<C> If A \incl BuC, and B \incl AuC, then *d2 would be
  236. // covered if we added A first, and A would be covered
  237. // if we added B first.
  238. // In this example we want both A and B, because we don't want to give
  239. // either one priority over the other, since they belong to the same
  240. // statement.
  241. RegisterAggr RRs(DefRRs);
  242. auto DefInSet = [&Defs] (NodeAddr<RefNode*> TA) -> bool {
  243. return TA.Addr->getKind() == NodeAttrs::Def &&
  244. Defs.count(TA.Id);
  245. };
  246. for (NodeId T : TmpInst) {
  247. if (!FullChain && RRs.hasCoverOf(RefRR))
  248. break;
  249. auto TA = DFG.addr<InstrNode*>(T);
  250. bool IsPhi = DFG.IsCode<NodeAttrs::Phi>(TA);
  251. NodeList Ds;
  252. for (NodeAddr<DefNode*> DA : TA.Addr->members_if(DefInSet, DFG)) {
  253. RegisterRef QR = DA.Addr->getRegRef(DFG);
  254. // Add phi defs even if they are covered by subsequent defs. This is
  255. // for cases where the reached use is not covered by any of the defs
  256. // encountered so far: the phi def is needed to expose the liveness
  257. // of that use to the entry of the block.
  258. // Example:
  259. // phi d1<R3>(,d2,), ... Phi def d1 is covered by d2.
  260. // d2<R3>(d1,,u3), ...
  261. // ..., u3<D1>(d2) This use needs to be live on entry.
  262. if (FullChain || IsPhi || !RRs.hasCoverOf(QR))
  263. Ds.push_back(DA);
  264. }
  265. llvm::append_range(RDefs, Ds);
  266. for (NodeAddr<DefNode*> DA : Ds) {
  267. // When collecting a full chain of definitions, do not consider phi
  268. // defs to actually define a register.
  269. uint16_t Flags = DA.Addr->getFlags();
  270. if (!FullChain || !(Flags & NodeAttrs::PhiRef))
  271. if (!(Flags & NodeAttrs::Preserving)) // Don't care about Undef here.
  272. RRs.insert(DA.Addr->getRegRef(DFG));
  273. }
  274. }
  275. auto DeadP = [](const NodeAddr<DefNode*> DA) -> bool {
  276. return DA.Addr->getFlags() & NodeAttrs::Dead;
  277. };
  278. llvm::erase_if(RDefs, DeadP);
  279. return RDefs;
  280. }
  281. std::pair<NodeSet,bool>
  282. Liveness::getAllReachingDefsRec(RegisterRef RefRR, NodeAddr<RefNode*> RefA,
  283. NodeSet &Visited, const NodeSet &Defs) {
  284. return getAllReachingDefsRecImpl(RefRR, RefA, Visited, Defs, 0, MaxRecNest);
  285. }
  286. std::pair<NodeSet,bool>
  287. Liveness::getAllReachingDefsRecImpl(RegisterRef RefRR, NodeAddr<RefNode*> RefA,
  288. NodeSet &Visited, const NodeSet &Defs, unsigned Nest, unsigned MaxNest) {
  289. if (Nest > MaxNest)
  290. return { NodeSet(), false };
  291. // Collect all defined registers. Do not consider phis to be defining
  292. // anything, only collect "real" definitions.
  293. RegisterAggr DefRRs(PRI);
  294. for (NodeId D : Defs) {
  295. const auto DA = DFG.addr<const DefNode*>(D);
  296. if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
  297. DefRRs.insert(DA.Addr->getRegRef(DFG));
  298. }
  299. NodeList RDs = getAllReachingDefs(RefRR, RefA, false, true, DefRRs);
  300. if (RDs.empty())
  301. return { Defs, true };
  302. // Make a copy of the preexisting definitions and add the newly found ones.
  303. NodeSet TmpDefs = Defs;
  304. for (NodeAddr<NodeBase*> R : RDs)
  305. TmpDefs.insert(R.Id);
  306. NodeSet Result = Defs;
  307. for (NodeAddr<DefNode*> DA : RDs) {
  308. Result.insert(DA.Id);
  309. if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
  310. continue;
  311. NodeAddr<PhiNode*> PA = DA.Addr->getOwner(DFG);
  312. if (Visited.count(PA.Id))
  313. continue;
  314. Visited.insert(PA.Id);
  315. // Go over all phi uses and get the reaching defs for each use.
  316. for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
  317. const auto &T = getAllReachingDefsRecImpl(RefRR, U, Visited, TmpDefs,
  318. Nest+1, MaxNest);
  319. if (!T.second)
  320. return { T.first, false };
  321. Result.insert(T.first.begin(), T.first.end());
  322. }
  323. }
  324. return { Result, true };
  325. }
  326. /// Find the nearest ref node aliased to RefRR, going upwards in the data
  327. /// flow, starting from the instruction immediately preceding Inst.
  328. NodeAddr<RefNode*> Liveness::getNearestAliasedRef(RegisterRef RefRR,
  329. NodeAddr<InstrNode*> IA) {
  330. NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
  331. NodeList Ins = BA.Addr->members(DFG);
  332. NodeId FindId = IA.Id;
  333. auto E = Ins.rend();
  334. auto B = std::find_if(Ins.rbegin(), E,
  335. [FindId] (const NodeAddr<InstrNode*> T) {
  336. return T.Id == FindId;
  337. });
  338. // Do not scan IA (which is what B would point to).
  339. if (B != E)
  340. ++B;
  341. do {
  342. // Process the range of instructions from B to E.
  343. for (NodeAddr<InstrNode*> I : make_range(B, E)) {
  344. NodeList Refs = I.Addr->members(DFG);
  345. NodeAddr<RefNode*> Clob, Use;
  346. // Scan all the refs in I aliased to RefRR, and return the one that
  347. // is the closest to the output of I, i.e. def > clobber > use.
  348. for (NodeAddr<RefNode*> R : Refs) {
  349. if (!PRI.alias(R.Addr->getRegRef(DFG), RefRR))
  350. continue;
  351. if (DFG.IsDef(R)) {
  352. // If it's a non-clobbering def, just return it.
  353. if (!(R.Addr->getFlags() & NodeAttrs::Clobbering))
  354. return R;
  355. Clob = R;
  356. } else {
  357. Use = R;
  358. }
  359. }
  360. if (Clob.Id != 0)
  361. return Clob;
  362. if (Use.Id != 0)
  363. return Use;
  364. }
  365. // Go up to the immediate dominator, if any.
  366. MachineBasicBlock *BB = BA.Addr->getCode();
  367. BA = NodeAddr<BlockNode*>();
  368. if (MachineDomTreeNode *N = MDT.getNode(BB)) {
  369. if ((N = N->getIDom()))
  370. BA = DFG.findBlock(N->getBlock());
  371. }
  372. if (!BA.Id)
  373. break;
  374. Ins = BA.Addr->members(DFG);
  375. B = Ins.rbegin();
  376. E = Ins.rend();
  377. } while (true);
  378. return NodeAddr<RefNode*>();
  379. }
  380. NodeSet Liveness::getAllReachedUses(RegisterRef RefRR,
  381. NodeAddr<DefNode*> DefA, const RegisterAggr &DefRRs) {
  382. NodeSet Uses;
  383. // If the original register is already covered by all the intervening
  384. // defs, no more uses can be reached.
  385. if (DefRRs.hasCoverOf(RefRR))
  386. return Uses;
  387. // Add all directly reached uses.
  388. // If the def is dead, it does not provide a value for any use.
  389. bool IsDead = DefA.Addr->getFlags() & NodeAttrs::Dead;
  390. NodeId U = !IsDead ? DefA.Addr->getReachedUse() : 0;
  391. while (U != 0) {
  392. auto UA = DFG.addr<UseNode*>(U);
  393. if (!(UA.Addr->getFlags() & NodeAttrs::Undef)) {
  394. RegisterRef UR = UA.Addr->getRegRef(DFG);
  395. if (PRI.alias(RefRR, UR) && !DefRRs.hasCoverOf(UR))
  396. Uses.insert(U);
  397. }
  398. U = UA.Addr->getSibling();
  399. }
  400. // Traverse all reached defs. This time dead defs cannot be ignored.
  401. for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) {
  402. auto DA = DFG.addr<DefNode*>(D);
  403. NextD = DA.Addr->getSibling();
  404. RegisterRef DR = DA.Addr->getRegRef(DFG);
  405. // If this def is already covered, it cannot reach anything new.
  406. // Similarly, skip it if it is not aliased to the interesting register.
  407. if (DefRRs.hasCoverOf(DR) || !PRI.alias(RefRR, DR))
  408. continue;
  409. NodeSet T;
  410. if (DFG.IsPreservingDef(DA)) {
  411. // If it is a preserving def, do not update the set of intervening defs.
  412. T = getAllReachedUses(RefRR, DA, DefRRs);
  413. } else {
  414. RegisterAggr NewDefRRs = DefRRs;
  415. NewDefRRs.insert(DR);
  416. T = getAllReachedUses(RefRR, DA, NewDefRRs);
  417. }
  418. Uses.insert(T.begin(), T.end());
  419. }
  420. return Uses;
  421. }
  422. void Liveness::computePhiInfo() {
  423. RealUseMap.clear();
  424. NodeList Phis;
  425. NodeAddr<FuncNode*> FA = DFG.getFunc();
  426. NodeList Blocks = FA.Addr->members(DFG);
  427. for (NodeAddr<BlockNode*> BA : Blocks) {
  428. auto Ps = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
  429. llvm::append_range(Phis, Ps);
  430. }
  431. // phi use -> (map: reaching phi -> set of registers defined in between)
  432. std::map<NodeId,std::map<NodeId,RegisterAggr>> PhiUp;
  433. std::vector<NodeId> PhiUQ; // Work list of phis for upward propagation.
  434. std::unordered_map<NodeId,RegisterAggr> PhiDRs; // Phi -> registers defined by it.
  435. // Go over all phis.
  436. for (NodeAddr<PhiNode*> PhiA : Phis) {
  437. // Go over all defs and collect the reached uses that are non-phi uses
  438. // (i.e. the "real uses").
  439. RefMap &RealUses = RealUseMap[PhiA.Id];
  440. NodeList PhiRefs = PhiA.Addr->members(DFG);
  441. // Have a work queue of defs whose reached uses need to be found.
  442. // For each def, add to the queue all reached (non-phi) defs.
  443. SetVector<NodeId> DefQ;
  444. NodeSet PhiDefs;
  445. RegisterAggr DRs(PRI);
  446. for (NodeAddr<RefNode*> R : PhiRefs) {
  447. if (!DFG.IsRef<NodeAttrs::Def>(R))
  448. continue;
  449. DRs.insert(R.Addr->getRegRef(DFG));
  450. DefQ.insert(R.Id);
  451. PhiDefs.insert(R.Id);
  452. }
  453. PhiDRs.insert(std::make_pair(PhiA.Id, DRs));
  454. // Collect the super-set of all possible reached uses. This set will
  455. // contain all uses reached from this phi, either directly from the
  456. // phi defs, or (recursively) via non-phi defs reached by the phi defs.
  457. // This set of uses will later be trimmed to only contain these uses that
  458. // are actually reached by the phi defs.
  459. for (unsigned i = 0; i < DefQ.size(); ++i) {
  460. NodeAddr<DefNode*> DA = DFG.addr<DefNode*>(DefQ[i]);
  461. // Visit all reached uses. Phi defs should not really have the "dead"
  462. // flag set, but check it anyway for consistency.
  463. bool IsDead = DA.Addr->getFlags() & NodeAttrs::Dead;
  464. NodeId UN = !IsDead ? DA.Addr->getReachedUse() : 0;
  465. while (UN != 0) {
  466. NodeAddr<UseNode*> A = DFG.addr<UseNode*>(UN);
  467. uint16_t F = A.Addr->getFlags();
  468. if ((F & (NodeAttrs::Undef | NodeAttrs::PhiRef)) == 0) {
  469. RegisterRef R = A.Addr->getRegRef(DFG);
  470. RealUses[R.Reg].insert({A.Id,R.Mask});
  471. }
  472. UN = A.Addr->getSibling();
  473. }
  474. // Visit all reached defs, and add them to the queue. These defs may
  475. // override some of the uses collected here, but that will be handled
  476. // later.
  477. NodeId DN = DA.Addr->getReachedDef();
  478. while (DN != 0) {
  479. NodeAddr<DefNode*> A = DFG.addr<DefNode*>(DN);
  480. for (auto T : DFG.getRelatedRefs(A.Addr->getOwner(DFG), A)) {
  481. uint16_t Flags = NodeAddr<DefNode*>(T).Addr->getFlags();
  482. // Must traverse the reached-def chain. Consider:
  483. // def(D0) -> def(R0) -> def(R0) -> use(D0)
  484. // The reachable use of D0 passes through a def of R0.
  485. if (!(Flags & NodeAttrs::PhiRef))
  486. DefQ.insert(T.Id);
  487. }
  488. DN = A.Addr->getSibling();
  489. }
  490. }
  491. // Filter out these uses that appear to be reachable, but really
  492. // are not. For example:
  493. //
  494. // R1:0 = d1
  495. // = R1:0 u2 Reached by d1.
  496. // R0 = d3
  497. // = R1:0 u4 Still reached by d1: indirectly through
  498. // the def d3.
  499. // R1 = d5
  500. // = R1:0 u6 Not reached by d1 (covered collectively
  501. // by d3 and d5), but following reached
  502. // defs and uses from d1 will lead here.
  503. for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE; ) {
  504. // For each reached register UI->first, there is a set UI->second, of
  505. // uses of it. For each such use, check if it is reached by this phi,
  506. // i.e. check if the set of its reaching uses intersects the set of
  507. // this phi's defs.
  508. NodeRefSet Uses = UI->second;
  509. UI->second.clear();
  510. for (std::pair<NodeId,LaneBitmask> I : Uses) {
  511. auto UA = DFG.addr<UseNode*>(I.first);
  512. // Undef flag is checked above.
  513. assert((UA.Addr->getFlags() & NodeAttrs::Undef) == 0);
  514. RegisterRef R(UI->first, I.second);
  515. // Calculate the exposed part of the reached use.
  516. RegisterAggr Covered(PRI);
  517. for (NodeAddr<DefNode*> DA : getAllReachingDefs(R, UA)) {
  518. if (PhiDefs.count(DA.Id))
  519. break;
  520. Covered.insert(DA.Addr->getRegRef(DFG));
  521. }
  522. if (RegisterRef RC = Covered.clearIn(R)) {
  523. // We are updating the map for register UI->first, so we need
  524. // to map RC to be expressed in terms of that register.
  525. RegisterRef S = PRI.mapTo(RC, UI->first);
  526. UI->second.insert({I.first, S.Mask});
  527. }
  528. }
  529. UI = UI->second.empty() ? RealUses.erase(UI) : std::next(UI);
  530. }
  531. // If this phi reaches some "real" uses, add it to the queue for upward
  532. // propagation.
  533. if (!RealUses.empty())
  534. PhiUQ.push_back(PhiA.Id);
  535. // Go over all phi uses and check if the reaching def is another phi.
  536. // Collect the phis that are among the reaching defs of these uses.
  537. // While traversing the list of reaching defs for each phi use, accumulate
  538. // the set of registers defined between this phi (PhiA) and the owner phi
  539. // of the reaching def.
  540. NodeSet SeenUses;
  541. for (auto I : PhiRefs) {
  542. if (!DFG.IsRef<NodeAttrs::Use>(I) || SeenUses.count(I.Id))
  543. continue;
  544. NodeAddr<PhiUseNode*> PUA = I;
  545. if (PUA.Addr->getReachingDef() == 0)
  546. continue;
  547. RegisterRef UR = PUA.Addr->getRegRef(DFG);
  548. NodeList Ds = getAllReachingDefs(UR, PUA, true, false, NoRegs);
  549. RegisterAggr DefRRs(PRI);
  550. for (NodeAddr<DefNode*> D : Ds) {
  551. if (D.Addr->getFlags() & NodeAttrs::PhiRef) {
  552. NodeId RP = D.Addr->getOwner(DFG).Id;
  553. std::map<NodeId,RegisterAggr> &M = PhiUp[PUA.Id];
  554. auto F = M.find(RP);
  555. if (F == M.end())
  556. M.insert(std::make_pair(RP, DefRRs));
  557. else
  558. F->second.insert(DefRRs);
  559. }
  560. DefRRs.insert(D.Addr->getRegRef(DFG));
  561. }
  562. for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PhiA, PUA))
  563. SeenUses.insert(T.Id);
  564. }
  565. }
  566. if (Trace) {
  567. dbgs() << "Phi-up-to-phi map with intervening defs:\n";
  568. for (auto I : PhiUp) {
  569. dbgs() << "phi " << Print<NodeId>(I.first, DFG) << " -> {";
  570. for (auto R : I.second)
  571. dbgs() << ' ' << Print<NodeId>(R.first, DFG)
  572. << Print<RegisterAggr>(R.second, DFG);
  573. dbgs() << " }\n";
  574. }
  575. }
  576. // Propagate the reached registers up in the phi chain.
  577. //
  578. // The following type of situation needs careful handling:
  579. //
  580. // phi d1<R1:0> (1)
  581. // |
  582. // ... d2<R1>
  583. // |
  584. // phi u3<R1:0> (2)
  585. // |
  586. // ... u4<R1>
  587. //
  588. // The phi node (2) defines a register pair R1:0, and reaches a "real"
  589. // use u4 of just R1. The same phi node is also known to reach (upwards)
  590. // the phi node (1). However, the use u4 is not reached by phi (1),
  591. // because of the intervening definition d2 of R1. The data flow between
  592. // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
  593. //
  594. // When propagating uses up the phi chains, get the all reaching defs
  595. // for a given phi use, and traverse the list until the propagated ref
  596. // is covered, or until reaching the final phi. Only assume that the
  597. // reference reaches the phi in the latter case.
  598. // The operation "clearIn" can be expensive. For a given set of intervening
  599. // defs, cache the result of subtracting these defs from a given register
  600. // ref.
  601. using SubMap = std::unordered_map<RegisterRef, RegisterRef>;
  602. std::unordered_map<RegisterAggr, SubMap> Subs;
  603. auto ClearIn = [] (RegisterRef RR, const RegisterAggr &Mid, SubMap &SM) {
  604. if (Mid.empty())
  605. return RR;
  606. auto F = SM.find(RR);
  607. if (F != SM.end())
  608. return F->second;
  609. RegisterRef S = Mid.clearIn(RR);
  610. SM.insert({RR, S});
  611. return S;
  612. };
  613. // Go over all phis.
  614. for (unsigned i = 0; i < PhiUQ.size(); ++i) {
  615. auto PA = DFG.addr<PhiNode*>(PhiUQ[i]);
  616. NodeList PUs = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG);
  617. RefMap &RUM = RealUseMap[PA.Id];
  618. for (NodeAddr<UseNode*> UA : PUs) {
  619. std::map<NodeId,RegisterAggr> &PUM = PhiUp[UA.Id];
  620. RegisterRef UR = UA.Addr->getRegRef(DFG);
  621. for (const std::pair<const NodeId, RegisterAggr> &P : PUM) {
  622. bool Changed = false;
  623. const RegisterAggr &MidDefs = P.second;
  624. // Collect the set PropUp of uses that are reached by the current
  625. // phi PA, and are not covered by any intervening def between the
  626. // currently visited use UA and the upward phi P.
  627. if (MidDefs.hasCoverOf(UR))
  628. continue;
  629. SubMap &SM = Subs[MidDefs];
  630. // General algorithm:
  631. // for each (R,U) : U is use node of R, U is reached by PA
  632. // if MidDefs does not cover (R,U)
  633. // then add (R-MidDefs,U) to RealUseMap[P]
  634. //
  635. for (const std::pair<const RegisterId, NodeRefSet> &T : RUM) {
  636. RegisterRef R(T.first);
  637. // The current phi (PA) could be a phi for a regmask. It could
  638. // reach a whole variety of uses that are not related to the
  639. // specific upward phi (P.first).
  640. const RegisterAggr &DRs = PhiDRs.at(P.first);
  641. if (!DRs.hasAliasOf(R))
  642. continue;
  643. R = PRI.mapTo(DRs.intersectWith(R), T.first);
  644. for (std::pair<NodeId,LaneBitmask> V : T.second) {
  645. LaneBitmask M = R.Mask & V.second;
  646. if (M.none())
  647. continue;
  648. if (RegisterRef SS = ClearIn(RegisterRef(R.Reg, M), MidDefs, SM)) {
  649. NodeRefSet &RS = RealUseMap[P.first][SS.Reg];
  650. Changed |= RS.insert({V.first,SS.Mask}).second;
  651. }
  652. }
  653. }
  654. if (Changed)
  655. PhiUQ.push_back(P.first);
  656. }
  657. }
  658. }
  659. if (Trace) {
  660. dbgs() << "Real use map:\n";
  661. for (auto I : RealUseMap) {
  662. dbgs() << "phi " << Print<NodeId>(I.first, DFG);
  663. NodeAddr<PhiNode*> PA = DFG.addr<PhiNode*>(I.first);
  664. NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
  665. if (!Ds.empty()) {
  666. RegisterRef RR = NodeAddr<DefNode*>(Ds[0]).Addr->getRegRef(DFG);
  667. dbgs() << '<' << Print<RegisterRef>(RR, DFG) << '>';
  668. } else {
  669. dbgs() << "<noreg>";
  670. }
  671. dbgs() << " -> " << Print<RefMap>(I.second, DFG) << '\n';
  672. }
  673. }
  674. }
  675. void Liveness::computeLiveIns() {
  676. // Populate the node-to-block map. This speeds up the calculations
  677. // significantly.
  678. NBMap.clear();
  679. for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
  680. MachineBasicBlock *BB = BA.Addr->getCode();
  681. for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
  682. for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
  683. NBMap.insert(std::make_pair(RA.Id, BB));
  684. NBMap.insert(std::make_pair(IA.Id, BB));
  685. }
  686. }
  687. MachineFunction &MF = DFG.getMF();
  688. // Compute IDF first, then the inverse.
  689. decltype(IIDF) IDF;
  690. for (MachineBasicBlock &B : MF) {
  691. auto F1 = MDF.find(&B);
  692. if (F1 == MDF.end())
  693. continue;
  694. SetVector<MachineBasicBlock*> IDFB(F1->second.begin(), F1->second.end());
  695. for (unsigned i = 0; i < IDFB.size(); ++i) {
  696. auto F2 = MDF.find(IDFB[i]);
  697. if (F2 != MDF.end())
  698. IDFB.insert(F2->second.begin(), F2->second.end());
  699. }
  700. // Add B to the IDF(B). This will put B in the IIDF(B).
  701. IDFB.insert(&B);
  702. IDF[&B].insert(IDFB.begin(), IDFB.end());
  703. }
  704. for (auto I : IDF)
  705. for (auto S : I.second)
  706. IIDF[S].insert(I.first);
  707. computePhiInfo();
  708. NodeAddr<FuncNode*> FA = DFG.getFunc();
  709. NodeList Blocks = FA.Addr->members(DFG);
  710. // Build the phi live-on-entry map.
  711. for (NodeAddr<BlockNode*> BA : Blocks) {
  712. MachineBasicBlock *MB = BA.Addr->getCode();
  713. RefMap &LON = PhiLON[MB];
  714. for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG))
  715. for (const RefMap::value_type &S : RealUseMap[P.Id])
  716. LON[S.first].insert(S.second.begin(), S.second.end());
  717. }
  718. if (Trace) {
  719. dbgs() << "Phi live-on-entry map:\n";
  720. for (auto &I : PhiLON)
  721. dbgs() << "block #" << I.first->getNumber() << " -> "
  722. << Print<RefMap>(I.second, DFG) << '\n';
  723. }
  724. // Build the phi live-on-exit map. Each phi node has some set of reached
  725. // "real" uses. Propagate this set backwards into the block predecessors
  726. // through the reaching defs of the corresponding phi uses.
  727. for (NodeAddr<BlockNode*> BA : Blocks) {
  728. NodeList Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
  729. for (NodeAddr<PhiNode*> PA : Phis) {
  730. RefMap &RUs = RealUseMap[PA.Id];
  731. if (RUs.empty())
  732. continue;
  733. NodeSet SeenUses;
  734. for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
  735. if (!SeenUses.insert(U.Id).second)
  736. continue;
  737. NodeAddr<PhiUseNode*> PUA = U;
  738. if (PUA.Addr->getReachingDef() == 0)
  739. continue;
  740. // Each phi has some set (possibly empty) of reached "real" uses,
  741. // that is, uses that are part of the compiled program. Such a use
  742. // may be located in some farther block, but following a chain of
  743. // reaching defs will eventually lead to this phi.
  744. // Any chain of reaching defs may fork at a phi node, but there
  745. // will be a path upwards that will lead to this phi. Now, this
  746. // chain will need to fork at this phi, since some of the reached
  747. // uses may have definitions joining in from multiple predecessors.
  748. // For each reached "real" use, identify the set of reaching defs
  749. // coming from each predecessor P, and add them to PhiLOX[P].
  750. //
  751. auto PrA = DFG.addr<BlockNode*>(PUA.Addr->getPredecessor());
  752. RefMap &LOX = PhiLOX[PrA.Addr->getCode()];
  753. for (const std::pair<const RegisterId, NodeRefSet> &RS : RUs) {
  754. // We need to visit each individual use.
  755. for (std::pair<NodeId,LaneBitmask> P : RS.second) {
  756. // Create a register ref corresponding to the use, and find
  757. // all reaching defs starting from the phi use, and treating
  758. // all related shadows as a single use cluster.
  759. RegisterRef S(RS.first, P.second);
  760. NodeList Ds = getAllReachingDefs(S, PUA, true, false, NoRegs);
  761. for (NodeAddr<DefNode*> D : Ds) {
  762. // Calculate the mask corresponding to the visited def.
  763. RegisterAggr TA(PRI);
  764. TA.insert(D.Addr->getRegRef(DFG)).intersect(S);
  765. LaneBitmask TM = TA.makeRegRef().Mask;
  766. LOX[S.Reg].insert({D.Id, TM});
  767. }
  768. }
  769. }
  770. for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PA, PUA))
  771. SeenUses.insert(T.Id);
  772. } // for U : phi uses
  773. } // for P : Phis
  774. } // for B : Blocks
  775. if (Trace) {
  776. dbgs() << "Phi live-on-exit map:\n";
  777. for (auto &I : PhiLOX)
  778. dbgs() << "block #" << I.first->getNumber() << " -> "
  779. << Print<RefMap>(I.second, DFG) << '\n';
  780. }
  781. RefMap LiveIn;
  782. traverse(&MF.front(), LiveIn);
  783. // Add function live-ins to the live-in set of the function entry block.
  784. LiveMap[&MF.front()].insert(DFG.getLiveIns());
  785. if (Trace) {
  786. // Dump the liveness map
  787. for (MachineBasicBlock &B : MF) {
  788. std::vector<RegisterRef> LV;
  789. for (const MachineBasicBlock::RegisterMaskPair &LI : B.liveins())
  790. LV.push_back(RegisterRef(LI.PhysReg, LI.LaneMask));
  791. llvm::sort(LV);
  792. dbgs() << printMBBReference(B) << "\t rec = {";
  793. for (auto I : LV)
  794. dbgs() << ' ' << Print<RegisterRef>(I, DFG);
  795. dbgs() << " }\n";
  796. //dbgs() << "\tcomp = " << Print<RegisterAggr>(LiveMap[&B], DFG) << '\n';
  797. LV.clear();
  798. const RegisterAggr &LG = LiveMap[&B];
  799. for (auto I = LG.rr_begin(), E = LG.rr_end(); I != E; ++I)
  800. LV.push_back(*I);
  801. llvm::sort(LV);
  802. dbgs() << "\tcomp = {";
  803. for (auto I : LV)
  804. dbgs() << ' ' << Print<RegisterRef>(I, DFG);
  805. dbgs() << " }\n";
  806. }
  807. }
  808. }
  809. void Liveness::resetLiveIns() {
  810. for (auto &B : DFG.getMF()) {
  811. // Remove all live-ins.
  812. std::vector<unsigned> T;
  813. for (const MachineBasicBlock::RegisterMaskPair &LI : B.liveins())
  814. T.push_back(LI.PhysReg);
  815. for (auto I : T)
  816. B.removeLiveIn(I);
  817. // Add the newly computed live-ins.
  818. const RegisterAggr &LiveIns = LiveMap[&B];
  819. for (const RegisterRef R : make_range(LiveIns.rr_begin(), LiveIns.rr_end()))
  820. B.addLiveIn({MCPhysReg(R.Reg), R.Mask});
  821. }
  822. }
  823. void Liveness::resetKills() {
  824. for (auto &B : DFG.getMF())
  825. resetKills(&B);
  826. }
  827. void Liveness::resetKills(MachineBasicBlock *B) {
  828. auto CopyLiveIns = [this] (MachineBasicBlock *B, BitVector &LV) -> void {
  829. for (auto I : B->liveins()) {
  830. MCSubRegIndexIterator S(I.PhysReg, &TRI);
  831. if (!S.isValid()) {
  832. LV.set(I.PhysReg);
  833. continue;
  834. }
  835. do {
  836. LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
  837. if ((M & I.LaneMask).any())
  838. LV.set(S.getSubReg());
  839. ++S;
  840. } while (S.isValid());
  841. }
  842. };
  843. BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
  844. CopyLiveIns(B, LiveIn);
  845. for (auto SI : B->successors())
  846. CopyLiveIns(SI, Live);
  847. for (MachineInstr &MI : llvm::reverse(*B)) {
  848. if (MI.isDebugInstr())
  849. continue;
  850. MI.clearKillInfo();
  851. for (auto &Op : MI.operands()) {
  852. // An implicit def of a super-register may not necessarily start a
  853. // live range of it, since an implicit use could be used to keep parts
  854. // of it live. Instead of analyzing the implicit operands, ignore
  855. // implicit defs.
  856. if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
  857. continue;
  858. Register R = Op.getReg();
  859. if (!Register::isPhysicalRegister(R))
  860. continue;
  861. for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
  862. Live.reset(*SR);
  863. }
  864. for (auto &Op : MI.operands()) {
  865. if (!Op.isReg() || !Op.isUse() || Op.isUndef())
  866. continue;
  867. Register R = Op.getReg();
  868. if (!Register::isPhysicalRegister(R))
  869. continue;
  870. bool IsLive = false;
  871. for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
  872. if (!Live[*AR])
  873. continue;
  874. IsLive = true;
  875. break;
  876. }
  877. if (!IsLive)
  878. Op.setIsKill(true);
  879. for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
  880. Live.set(*SR);
  881. }
  882. }
  883. }
  884. // Helper function to obtain the basic block containing the reaching def
  885. // of the given use.
  886. MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
  887. auto F = NBMap.find(RN);
  888. if (F != NBMap.end())
  889. return F->second;
  890. llvm_unreachable("Node id not in map");
  891. }
  892. void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
  893. // The LiveIn map, for each (physical) register, contains the set of live
  894. // reaching defs of that register that are live on entry to the associated
  895. // block.
  896. // The summary of the traversal algorithm:
  897. //
  898. // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
  899. // and (U \in IDF(B) or B dom U).
  900. //
  901. // for (C : children) {
  902. // LU = {}
  903. // traverse(C, LU)
  904. // LiveUses += LU
  905. // }
  906. //
  907. // LiveUses -= Defs(B);
  908. // LiveUses += UpwardExposedUses(B);
  909. // for (C : IIDF[B])
  910. // for (U : LiveUses)
  911. // if (Rdef(U) dom C)
  912. // C.addLiveIn(U)
  913. //
  914. // Go up the dominator tree (depth-first).
  915. MachineDomTreeNode *N = MDT.getNode(B);
  916. for (auto I : *N) {
  917. RefMap L;
  918. MachineBasicBlock *SB = I->getBlock();
  919. traverse(SB, L);
  920. for (auto S : L)
  921. LiveIn[S.first].insert(S.second.begin(), S.second.end());
  922. }
  923. if (Trace) {
  924. dbgs() << "\n-- " << printMBBReference(*B) << ": " << __func__
  925. << " after recursion into: {";
  926. for (auto I : *N)
  927. dbgs() << ' ' << I->getBlock()->getNumber();
  928. dbgs() << " }\n";
  929. dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
  930. dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
  931. }
  932. // Add reaching defs of phi uses that are live on exit from this block.
  933. RefMap &PUs = PhiLOX[B];
  934. for (auto &S : PUs)
  935. LiveIn[S.first].insert(S.second.begin(), S.second.end());
  936. if (Trace) {
  937. dbgs() << "after LOX\n";
  938. dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
  939. dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
  940. }
  941. // The LiveIn map at this point has all defs that are live-on-exit from B,
  942. // as if they were live-on-entry to B. First, we need to filter out all
  943. // defs that are present in this block. Then we will add reaching defs of
  944. // all upward-exposed uses.
  945. // To filter out the defs, first make a copy of LiveIn, and then re-populate
  946. // LiveIn with the defs that should remain.
  947. RefMap LiveInCopy = LiveIn;
  948. LiveIn.clear();
  949. for (const std::pair<const RegisterId, NodeRefSet> &LE : LiveInCopy) {
  950. RegisterRef LRef(LE.first);
  951. NodeRefSet &NewDefs = LiveIn[LRef.Reg]; // To be filled.
  952. const NodeRefSet &OldDefs = LE.second;
  953. for (NodeRef OR : OldDefs) {
  954. // R is a def node that was live-on-exit
  955. auto DA = DFG.addr<DefNode*>(OR.first);
  956. NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
  957. NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
  958. if (B != BA.Addr->getCode()) {
  959. // Defs from a different block need to be preserved. Defs from this
  960. // block will need to be processed further, except for phi defs, the
  961. // liveness of which is handled through the PhiLON/PhiLOX maps.
  962. NewDefs.insert(OR);
  963. continue;
  964. }
  965. // Defs from this block need to stop the liveness from being
  966. // propagated upwards. This only applies to non-preserving defs,
  967. // and to the parts of the register actually covered by those defs.
  968. // (Note that phi defs should always be preserving.)
  969. RegisterAggr RRs(PRI);
  970. LRef.Mask = OR.second;
  971. if (!DFG.IsPreservingDef(DA)) {
  972. assert(!(IA.Addr->getFlags() & NodeAttrs::Phi));
  973. // DA is a non-phi def that is live-on-exit from this block, and
  974. // that is also located in this block. LRef is a register ref
  975. // whose use this def reaches. If DA covers LRef, then no part
  976. // of LRef is exposed upwards.A
  977. if (RRs.insert(DA.Addr->getRegRef(DFG)).hasCoverOf(LRef))
  978. continue;
  979. }
  980. // DA itself was not sufficient to cover LRef. In general, it is
  981. // the last in a chain of aliased defs before the exit from this block.
  982. // There could be other defs in this block that are a part of that
  983. // chain. Check that now: accumulate the registers from these defs,
  984. // and if they all together cover LRef, it is not live-on-entry.
  985. for (NodeAddr<DefNode*> TA : getAllReachingDefs(DA)) {
  986. // DefNode -> InstrNode -> BlockNode.
  987. NodeAddr<InstrNode*> ITA = TA.Addr->getOwner(DFG);
  988. NodeAddr<BlockNode*> BTA = ITA.Addr->getOwner(DFG);
  989. // Reaching defs are ordered in the upward direction.
  990. if (BTA.Addr->getCode() != B) {
  991. // We have reached past the beginning of B, and the accumulated
  992. // registers are not covering LRef. The first def from the
  993. // upward chain will be live.
  994. // Subtract all accumulated defs (RRs) from LRef.
  995. RegisterRef T = RRs.clearIn(LRef);
  996. assert(T);
  997. NewDefs.insert({TA.Id,T.Mask});
  998. break;
  999. }
  1000. // TA is in B. Only add this def to the accumulated cover if it is
  1001. // not preserving.
  1002. if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
  1003. RRs.insert(TA.Addr->getRegRef(DFG));
  1004. // If this is enough to cover LRef, then stop.
  1005. if (RRs.hasCoverOf(LRef))
  1006. break;
  1007. }
  1008. }
  1009. }
  1010. emptify(LiveIn);
  1011. if (Trace) {
  1012. dbgs() << "after defs in block\n";
  1013. dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
  1014. dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
  1015. }
  1016. // Scan the block for upward-exposed uses and add them to the tracking set.
  1017. for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
  1018. NodeAddr<InstrNode*> IA = I;
  1019. if (IA.Addr->getKind() != NodeAttrs::Stmt)
  1020. continue;
  1021. for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
  1022. if (UA.Addr->getFlags() & NodeAttrs::Undef)
  1023. continue;
  1024. RegisterRef RR = UA.Addr->getRegRef(DFG);
  1025. for (NodeAddr<DefNode*> D : getAllReachingDefs(UA))
  1026. if (getBlockWithRef(D.Id) != B)
  1027. LiveIn[RR.Reg].insert({D.Id,RR.Mask});
  1028. }
  1029. }
  1030. if (Trace) {
  1031. dbgs() << "after uses in block\n";
  1032. dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
  1033. dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
  1034. }
  1035. // Phi uses should not be propagated up the dominator tree, since they
  1036. // are not dominated by their corresponding reaching defs.
  1037. RegisterAggr &Local = LiveMap[B];
  1038. RefMap &LON = PhiLON[B];
  1039. for (auto &R : LON) {
  1040. LaneBitmask M;
  1041. for (auto P : R.second)
  1042. M |= P.second;
  1043. Local.insert(RegisterRef(R.first,M));
  1044. }
  1045. if (Trace) {
  1046. dbgs() << "after phi uses in block\n";
  1047. dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
  1048. dbgs() << " Local: " << Print<RegisterAggr>(Local, DFG) << '\n';
  1049. }
  1050. for (auto C : IIDF[B]) {
  1051. RegisterAggr &LiveC = LiveMap[C];
  1052. for (const std::pair<const RegisterId, NodeRefSet> &S : LiveIn)
  1053. for (auto R : S.second)
  1054. if (MDT.properlyDominates(getBlockWithRef(R.first), C))
  1055. LiveC.insert(RegisterRef(S.first, R.second));
  1056. }
  1057. }
  1058. void Liveness::emptify(RefMap &M) {
  1059. for (auto I = M.begin(), E = M.end(); I != E; )
  1060. I = I->second.empty() ? M.erase(I) : std::next(I);
  1061. }