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 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 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); }
public static SortedSet<IRBlock> CalculateDominatesSet(IRGraph cfg, SortedSet<IRBlock> V, SortedSet<IRBlock> VSansR, IRBlock v) { SortedSet<IRBlock> VSansv = new SortedSet<IRBlock>(); VSansv.UnionWith(V); VSansv.Remove(v); // V - {v} SortedSet<IRBlock> S = FindReachableBlocks(cfg, v.GetIndex()); VSansv.ExceptWith(S); return VSansv; // V - {v} - S }
private static SortedSet<IRBlock> FindReachableBlocks(SortedSet<IRBlock> reachable, IRBlock block, int ignoreBlockIndex) { List<IRBlock> successors = block.GetSuccessors(); foreach (IRBlock successor in successors) { if ((!reachable.Contains(successor)) && (successor.GetIndex() != ignoreBlockIndex)) { reachable.Add(successor); FindReachableBlocks(reachable, successor, ignoreBlockIndex); } } return reachable; }
// TODO simplify this mess - blocks should know predcessors public SortedSet <IRBlock> GetPredecessors(IRBlock block) { SortedSet <IRBlock> predecessors = new SortedSet <IRBlock>(); foreach (KeyValuePair <int, IRBlock> pair in this.blocks) { if (pair.Value.GetSuccessors().Contains(block)) { predecessors.Add(pair.Value); } } return(predecessors); }
public void Print() { foreach (KeyValuePair <int, IRBlock> pair in this.blocks) { IRBlock block = pair.Value; Console.WriteLine("Block " + block.GetIndex() + ":"); block.PrintStatements(); block.PrintSuccessors(); block.PrintLiveuseDef(); block.PrintLiveInOut(); block.PrintLiveouts(); Console.WriteLine(); } }
private void CalculateImediateDominators(IRBlock block, Dictionary <IRBlock, SortedSet <IRBlock> > dominatesMapping, SortedSet <IRBlock> placed) { placed.Add(block); foreach (IRBlock blocki in dominatesMapping[block]) { if (!placed.Contains(blocki)) { this.imediateDominator[blocki] = block; CalculateImediateDominators(blocki, dominatesMapping, placed); } } }
public static SortedSet <IRBlock> FindReachableBlocks(IRGraph cfg, int ignoreBlockIndex) { IRBlock head = cfg.GetGraphHead(); SortedSet <IRBlock> reachable = new SortedSet <IRBlock>(); reachable.Add(head); // if you can't use head you should ge no where if (head.GetIndex() == ignoreBlockIndex) { return(reachable); } return(FindReachableBlocks(reachable, head, ignoreBlockIndex)); }
public static void Main(string [] args) { IRBlock block1 = new IRBlock(1); block1.AppendStatement(new IRTuple(IrOp.LABEL, "F$1")); block1.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "T", "R$2")); block1.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "A", "R$0")); block1.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "B", "R$1")); block1.AppendStatement(new IRTupleOneOpImm <int>(IrOp.STORE, "C", 0)); block1.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "D", "A")); IRBlock block2 = new IRBlock(2); block2.AppendStatement(new IRTuple(IrOp.LABEL, "L$1")); block2.AppendStatement(new IRTupleTwoOp(IrOp.ADD, "C", "C", "B")); block2.AppendStatement(new IRTupleOneOpImm <int>(IrOp.STORE, "T$1", 1)); block2.AppendStatement(new IRTupleTwoOp(IrOp.SUB, "D", "D", "T$1")); block2.AppendStatement(new IRTupleOneOpImm <int>(IrOp.STORE, "T$2", 0)); block2.AppendStatement(new IRTupleTwoOp(IrOp.LTE, "T$3", "D", "T$2")); block2.AppendStatement(new IRTupleOneOpIdent(IrOp.JMPF, "L$1", "T$3")); IRBlock block3 = new IRBlock(3); block3.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "R$0", "C")); block3.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "R$2", "T")); block1.AddSuccessor(block2); block2.AddSuccessor(block2); block2.AddSuccessor(block3); List <IRBlock> blocks = new List <IRBlock>(); blocks.Add(block1); blocks.Add(block2); blocks.Add(block3); foreach (IRBlock irb in blocks) { irb.ComputeLiveuseDef(); irb.ComputeLiveouts(); Console.WriteLine("B" + irb.GetIndex() + ":"); irb.PrintStatements(); irb.PrintSuccessors(); irb.PrintLiveuseDef(); irb.PrintLiveouts(); Console.WriteLine(); } }
// Establish pointers between each block in the graph and its successor blocks private void LinkSuccessors(SortedDictionary <int, int> firsts, SortedDictionary <int, int> lasts) { foreach (KeyValuePair <int, IRBlock> pair in this.blocks) { IRBlock block = pair.Value; IRTuple tup = block.GetLast(); // Get last statement in block if (tup.getOp() == IrOp.JMP) { foreach (KeyValuePair <int, IRBlock> pair0 in this.blocks) { IRBlock block0 = pair0.Value; IRTuple tup0 = block0.GetFirst(); if (tup0.getOp() == IrOp.LABEL && tup0.getDest() == tup.getDest()) { block.AddSuccessor(block0); } } } else if (tup.getOp() == IrOp.JMPF) { foreach (KeyValuePair <int, IRBlock> pair0 in this.blocks) { IRBlock block0 = pair0.Value; IRTuple tup0 = block0.GetFirst(); if (tup0.getOp() == IrOp.LABEL && tup0.getDest() == ((IRTupleOneOpIdent)tup).getDest()) { block.AddSuccessor(block0); } else if (firsts[block0.GetIndex()] == lasts[block.GetIndex()] + 1) { block.AddSuccessor(block0); } } } else { foreach (KeyValuePair <int, IRBlock> pair0 in this.blocks) { IRBlock block0 = pair0.Value; if (firsts[block0.GetIndex()] == lasts[block.GetIndex()] + 1) { block.AddSuccessor(block0); } } } } }
public static void Main(string [] args) { IRBlock block1 = new IRBlock(1); block1.AppendStatement(new IRTuple(IrOp.LABEL, "F$1")); block1.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "T", "R$2")); block1.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "A", "R$0")); block1.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "B", "R$1")); block1.AppendStatement(new IRTupleOneOpImm<int>(IrOp.STORE, "C", 0)); block1.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "D", "A")); IRBlock block2 = new IRBlock(2); block2.AppendStatement(new IRTuple(IrOp.LABEL, "L$1")); block2.AppendStatement(new IRTupleTwoOp(IrOp.ADD, "C", "C", "B")); block2.AppendStatement(new IRTupleOneOpImm<int>(IrOp.STORE, "T$1", 1)); block2.AppendStatement(new IRTupleTwoOp(IrOp.SUB, "D", "D", "T$1")); block2.AppendStatement(new IRTupleOneOpImm<int>(IrOp.STORE, "T$2", 0)); block2.AppendStatement(new IRTupleTwoOp(IrOp.LTE, "T$3", "D", "T$2")); block2.AppendStatement(new IRTupleOneOpIdent(IrOp.JMPF, "L$1", "T$3")); IRBlock block3 = new IRBlock(3); block3.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "R$0", "C")); block3.AppendStatement(new IRTupleOneOpIdent(IrOp.STORE, "R$2", "T")); block1.AddSuccessor(block2); block2.AddSuccessor(block2); block2.AddSuccessor(block3); List<IRBlock> blocks = new List<IRBlock>(); blocks.Add(block1); blocks.Add(block2); blocks.Add(block3); foreach (IRBlock irb in blocks) { irb.ComputeLiveuseDef(); irb.ComputeLiveouts(); Console.WriteLine("B" + irb.GetIndex() + ":"); irb.PrintStatements(); irb.PrintSuccessors(); irb.PrintLiveuseDef(); irb.PrintLiveouts(); Console.WriteLine(); } }
// Compute LiveIn and LiveOut for each block in this graph private void ComputeBlockLiveness() { bool converged = true; do { converged = true; foreach (KeyValuePair <int, IRBlock> pair in this.blocks) { IRBlock block = pair.Value; HashSet <Ident> oldlivein = new HashSet <Ident>(block.GetLiveIn()); HashSet <Ident> oldliveout = new HashSet <Ident>(block.GetLiveOut()); block.UpdateLiveness(); if (!block.GetLiveIn().SetEquals(oldlivein) || !block.GetLiveOut().SetEquals(oldliveout)) { converged = false; } } } while(!converged); }
/* * Helper Methods */ private static IRGraph BuildSampleCFG() { 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); block1.AddSuccessor(block2); block2.AddSuccessor(block3); block3.AddSuccessor(block4); block3.AddSuccessor(block5); block4.AddSuccessor(block6); block4.AddSuccessor(block7); block5.AddSuccessor(block8); block6.AddSuccessor(block9); block7.AddSuccessor(block9); block9.AddSuccessor(block3); List <IRBlock> blocks = new List <IRBlock>(); blocks.Add(block1); blocks.Add(block2); blocks.Add(block3); blocks.Add(block4); blocks.Add(block5); blocks.Add(block6); blocks.Add(block7); blocks.Add(block8); blocks.Add(block9); IRGraph cfg = new IRGraph(blocks); return(cfg); }
public DominatorTreeNode GetNode(IRBlock block) { return(this.blockToNodeMappings[block]); }
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(); } } }
/* * Helper Methods */ private static IRGraph BuildSampleCFG() { 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); block1.AddSuccessor(block2); block2.AddSuccessor(block3); block3.AddSuccessor(block4); block3.AddSuccessor(block5); block4.AddSuccessor(block6); block4.AddSuccessor(block7); block5.AddSuccessor(block8); block6.AddSuccessor(block9); block7.AddSuccessor(block9); block9.AddSuccessor(block3); List<IRBlock> blocks = new List<IRBlock>(); blocks.Add(block1); blocks.Add(block2); blocks.Add(block3); blocks.Add(block4); blocks.Add(block5); blocks.Add(block6); blocks.Add(block7); blocks.Add(block8); blocks.Add(block9); IRGraph cfg = new IRGraph(blocks); return cfg; }
public DominatorTreeNode GetNode(IRBlock block) { return this.blockToNodeMappings[block]; }
public SortedSet<IRBlock> GetDominanceFrontier(IRBlock block) { return this.dominanceFrontiers[block]; }
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 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 CalculateImediateDominators(IRBlock block, Dictionary<IRBlock, SortedSet<IRBlock>> dominatesMapping, SortedSet<IRBlock> placed) { placed.Add(block); foreach (IRBlock blocki in dominatesMapping[block]) { if(!placed.Contains(blocki)) { this.imediateDominator[blocki] = block; CalculateImediateDominators(blocki, dominatesMapping, placed); } } }
public static SortedSet <IRBlock> CalculateDominatesSet(IRGraph cfg, SortedSet <IRBlock> V, SortedSet <IRBlock> VSansR, IRBlock v) { SortedSet <IRBlock> VSansv = new SortedSet <IRBlock>(); VSansv.UnionWith(V); VSansv.Remove(v); // V - {v} SortedSet <IRBlock> S = FindReachableBlocks(cfg, v.GetIndex()); VSansv.ExceptWith(S); return(VSansv); // V - {v} - S }
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 static SortedSet <IRBlock> FindReachableBlocks(SortedSet <IRBlock> reachable, IRBlock block, int ignoreBlockIndex) { List <IRBlock> successors = block.GetSuccessors(); foreach (IRBlock successor in successors) { if ((!reachable.Contains(successor)) && (successor.GetIndex() != ignoreBlockIndex)) { reachable.Add(successor); FindReachableBlocks(reachable, successor, ignoreBlockIndex); } } return(reachable); }
public DominatorTreeNode(IRBlock block) { this.block = block; this.descendants = new SortedSet <DominatorTreeNode>(); }
public SortedSet <IRBlock> GetDominanceFrontier(IRBlock block) { return(this.dominanceFrontiers[block]); }