private static DominatorTreeNode BuildExpectedDominatorTree() { // this is silly replication but hey it makes life easy IRBlock block1 = new IRBlock(1); IRBlock block2 = new IRBlock(2); IRBlock block3 = new IRBlock(3); IRBlock block4 = new IRBlock(4); IRBlock block5 = new IRBlock(5); IRBlock block6 = new IRBlock(6); IRBlock block7 = new IRBlock(7); IRBlock block8 = new IRBlock(8); IRBlock block9 = new IRBlock(9); DominatorTreeNode node1 = new DominatorTreeNode(block1); DominatorTreeNode node2 = new DominatorTreeNode(block2); DominatorTreeNode node3 = new DominatorTreeNode(block3); DominatorTreeNode node4 = new DominatorTreeNode(block4); DominatorTreeNode node5 = new DominatorTreeNode(block5); DominatorTreeNode node6 = new DominatorTreeNode(block6); DominatorTreeNode node7 = new DominatorTreeNode(block7); DominatorTreeNode node8 = new DominatorTreeNode(block8); DominatorTreeNode node9 = new DominatorTreeNode(block9); node1.AddDescendant(node2); node2.AddDescendant(node3); node3.AddDescendant(node4); node3.AddDescendant(node5); node4.AddDescendant(node6); node4.AddDescendant(node7); node4.AddDescendant(node9); node5.AddDescendant(node8); // node4.AddDescendant(node8); return(node1); }
private void BuildDominatorTree() { // Using Aho & Ullman - The thoery of parsing and compilation Vol II ... Algorithm 11.5 IRBlock head = this.cfg.GetGraphHead(); SortedSet <IRBlock> V = this.cfg.GetSetOfAllBlocks(); SortedSet <IRBlock> VSansR = new SortedSet <IRBlock>(); VSansR.UnionWith(V); VSansR.Remove(head); // remove head from the set of all blocks // calculate which blocks dominates what list of blocks Dictionary <IRBlock, SortedSet <IRBlock> > dominatesMapping = new Dictionary <IRBlock, SortedSet <IRBlock> >(); dominatesMapping[head] = VSansR; foreach (IRBlock v in VSansR) { dominatesMapping[v] = CalculateDominatesSet(this.cfg, V, VSansR, v); } // use dominates sets to build the dominator tree SortedSet <IRBlock> placed = new SortedSet <IRBlock>(); this.imediateDominator = new Dictionary <IRBlock, IRBlock>(); CalculateImediateDominators(head, dominatesMapping, placed); this.rootNode = BuildTreeFromImediateDominators(); }
private SortedSet <IRBlock> ConstructDominanceFrontierMapping(IRBlock x) { // Using Ron Cytron et al Algorithm SortedSet <IRBlock> dfx = new SortedSet <IRBlock>(); // DF(X)local foreach (IRBlock y in x.GetSuccessors()) { if (!this.imediateDominator[y].Equals(x)) { dfx.Add(y); } } // DF(X)up DominatorTreeNode xnode = this.blockToNodeMappings[x]; foreach (DominatorTreeNode z in xnode.GetDescendants()) { foreach (IRBlock y in this.dominanceFrontiers[z.GetBlock()]) { if (!this.imediateDominator[y].Equals(x)) { dfx.Add(y); } } } return(dfx); }
private void RecurseDownTree(List <IRBlock> list, DominatorTreeNode node) { foreach (DominatorTreeNode descendant in node.GetDescendants()) { RecurseDownTree(list, descendant); } list.Add(node.GetBlock()); }
private static void printTree(DominatorTreeNode node) { Console.WriteLine("\n** Node " + node.GetBlock().GetIndex()); foreach (DominatorTreeNode desc in node.GetDescendants()) { Console.Write(desc.GetBlock().GetIndex() + ", "); } Console.WriteLine(); foreach (DominatorTreeNode desc in node.GetDescendants()) { printTree(desc); } }
private DominatorTreeNode BuildDominatorTree(IList <BasicBlock> blocks) { _logger.LogDebug("[SsaBuilder:BuildDominatorTree])"); var nodeMap = new Dictionary <BasicBlock, DominatorTreeNode>(); DominatorTreeNode?rootNode = null; foreach (var block in blocks) { var immediateDominator = block.ImmediateDominator; if (immediateDominator != null) { DominatorTreeNode?node; if (!nodeMap.TryGetValue(immediateDominator, out node)) { node = new DominatorTreeNode(immediateDominator); nodeMap[immediateDominator] = node; } var childNode = new DominatorTreeNode(block); nodeMap[block] = childNode; node.Children.Add(childNode); } else { if (!nodeMap.TryGetValue(block, out rootNode)) { rootNode = new DominatorTreeNode(block); nodeMap.Add(block, rootNode); } } } foreach (var node in nodeMap) { var sb = new StringBuilder($"{node.Key.Label} : "); foreach (var childNode in node.Value.Children) { sb.Append($"{childNode.Block.Label} "); } _logger.LogDebug(sb.ToString()); } return(rootNode !); }
private DominatorTreeNode BuildTreeFromImediateDominators() { // create nodes foreach (IRBlock block in this.cfg.GetSetOfAllBlocks()) { this.blockToNodeMappings[block] = new DominatorTreeNode(block); } // link nodes foreach (KeyValuePair <IRBlock, IRBlock> pair in this.imediateDominator) { DominatorTreeNode ancestor = this.blockToNodeMappings[pair.Value]; DominatorTreeNode descendant = this.blockToNodeMappings[pair.Key]; ancestor.AddDescendant(descendant); } return(this.blockToNodeMappings[this.cfg.GetGraphHead()]); }
/* * Test Building Full Dominator Tree */ public static void TestBuildFullTree() { // Build Dominator Tree IRGraph cfg = BuildSampleCFG(); DominatorTree dominatorTree = new DominatorTree(cfg); // Expected DominatorTreeNode expected = BuildExpectedDominatorTree(); // Compare Result to Expected if (!dominatorTree.GetRoot().Equals(expected)) { Console.WriteLine("\n\n *** RESULT ***"); printTree(dominatorTree.GetRoot()); Console.WriteLine("\n\n *** EXPECTED ***"); printTree(expected); throw new Exception("Dominator Tree built doesn't match expected dominator tree"); } }
private static void Search(IRBlock block, DominatorTree dominatorTree, Dictionary <Ident, int> count, Dictionary <Ident, Stack <int> > stack) { // Algorithm from Ron Cytron et al to rename variables into SSA form foreach (IRTuple irt in block.GetStatements()) { Console.Write("Type: " + irt.GetType() + " "); irt.Print(); Console.WriteLine(); if (irt.getOp() != IrOp.PHI) { foreach (Ident v in irt.GetUsedVars()) { Console.WriteLine("Renaming variable: " + v); int i = stack[v].Peek(); RenameTupleSourcesHelper(irt, v, i); // replace uses of v with vi in statement } } foreach (Ident a in irt.GetDefinedVars()) { count[a] = count[a] + 1; int i = count[a]; stack[a].Push(i); irt.setDest(RenameVar(a, i)); // replace definitions of a with ai in statement } } // rename Phi function arguments in the successors List <IRBlock> successors = block.GetSuccessors(); for (int i = 0; i < successors.Count; i++) { foreach (IRTuple stmt in block.GetStatements()) { if (stmt.getOp() == IrOp.PHI) { IRTupleManyOp phi = (IRTupleManyOp)stmt; Ident v = phi.GetSources()[i]; int numbering = stack[v].Peek(); phi.SetSource(i, RenameVar(v, numbering)); } } } // for each descendant do the Search(X) renaming process DominatorTreeNode node = dominatorTree.GetNode(block); foreach (DominatorTreeNode descendant in node.GetDescendants()) { Search(descendant.GetBlock(), dominatorTree, count, stack); } // pop stack phase foreach (IRTuple irt in block.GetStatements()) { foreach (Ident v in irt.GetDefinedVars()) { stack[v].Pop(); } } }
private void BuildDominatorTree() { // Using Aho & Ullman - The thoery of parsing and compilation Vol II ... Algorithm 11.5 IRBlock head = this.cfg.GetGraphHead(); SortedSet<IRBlock> V = this.cfg.GetSetOfAllBlocks(); SortedSet<IRBlock> VSansR = new SortedSet<IRBlock>(); VSansR.UnionWith(V); VSansR.Remove(head); // remove head from the set of all blocks // calculate which blocks dominates what list of blocks Dictionary<IRBlock, SortedSet<IRBlock>> dominatesMapping = new Dictionary<IRBlock, SortedSet<IRBlock>>(); dominatesMapping[head] = VSansR; foreach (IRBlock v in VSansR) { dominatesMapping[v] = CalculateDominatesSet(this.cfg, V, VSansR, v); } // use dominates sets to build the dominator tree SortedSet<IRBlock> placed = new SortedSet<IRBlock>(); this.imediateDominator = new Dictionary<IRBlock, IRBlock>(); CalculateImediateDominators(head, dominatesMapping, placed); this.rootNode = BuildTreeFromImediateDominators(); }
private void RecurseDownTree(List<IRBlock> list, DominatorTreeNode node) { foreach(DominatorTreeNode descendant in node.GetDescendants()) { RecurseDownTree(list, descendant); } list.Add(node.GetBlock()); }
private static DominatorTreeNode BuildExpectedDominatorTree() { // this is silly replication but hey it makes life easy IRBlock block1 = new IRBlock(1); IRBlock block2 = new IRBlock(2); IRBlock block3 = new IRBlock(3); IRBlock block4 = new IRBlock(4); IRBlock block5 = new IRBlock(5); IRBlock block6 = new IRBlock(6); IRBlock block7 = new IRBlock(7); IRBlock block8 = new IRBlock(8); IRBlock block9 = new IRBlock(9); DominatorTreeNode node1 = new DominatorTreeNode(block1); DominatorTreeNode node2 = new DominatorTreeNode(block2); DominatorTreeNode node3 = new DominatorTreeNode(block3); DominatorTreeNode node4 = new DominatorTreeNode(block4); DominatorTreeNode node5 = new DominatorTreeNode(block5); DominatorTreeNode node6 = new DominatorTreeNode(block6); DominatorTreeNode node7 = new DominatorTreeNode(block7); DominatorTreeNode node8 = new DominatorTreeNode(block8); DominatorTreeNode node9 = new DominatorTreeNode(block9); node1.AddDescendant(node2); node2.AddDescendant(node3); node3.AddDescendant(node4); node3.AddDescendant(node5); node4.AddDescendant(node6); node4.AddDescendant(node7); node4.AddDescendant(node9); node5.AddDescendant(node8); // node4.AddDescendant(node8); return node1; }