/// <summary> /// Gets the dominated nodes of the condition successor, only if they can form a legal block for the if construct. /// </summary> /// <param name="dominatorTree"></param> /// <param name="conditionSuccessor"></param> /// <returns>On success - a set of the nodes fo the block. Otherwise it returns null.</returns> private HashSet<ILogicalConstruct> GetBlockBody(DominatorTree dominatorTree, ILogicalConstruct conditionSuccessor, ConditionLogicalConstruct theCondition) { if(conditionSuccessor == dominatorTree.RootConstruct) //Corner case - the successor cannot be the entry of the construct. { return null; } HashSet<ILogicalConstruct> body = null; if(conditionSuccessor.AllPredecessors.Count == 1) //The condition successor must have only one predecessor - the condition. { body = new HashSet<ILogicalConstruct>(); foreach (ILogicalConstruct node in dominatorTree.GetDominatedNodes(conditionSuccessor)) { if (node == theCondition) { return null; } body.Add(node); } } return body; }
private Dictionary<ILogicalConstruct, HashSet<ISingleEntrySubGraph>> GetValidCases(DominatorTree dominatorTree, ILogicalConstruct switchCFGBlock) { Dictionary<ILogicalConstruct, HashSet<ISingleEntrySubGraph>> caseEntriesToDominatedNodesMap = new Dictionary<ILogicalConstruct, HashSet<ISingleEntrySubGraph>>(); HashSet<ISingleEntrySubGraph> legalPredecessors = new HashSet<ISingleEntrySubGraph>(); legalPredecessors.Add(switchCFGBlock); foreach (ILogicalConstruct successor in switchCFGBlock.SameParentSuccessors) { if (successor != switchCFGBlock && dominatorTree.GetImmediateDominator(successor) == switchCFGBlock) { HashSet<ISingleEntrySubGraph> dominatedNodes = dominatorTree.GetDominatedNodes(successor); caseEntriesToDominatedNodesMap.Add(successor, dominatedNodes); legalPredecessors.UnionWith(dominatedNodes); } } DFSTree dfsTree = DFSTBuilder.BuildTree(switchCFGBlock.Parent, switchCFGBlock); List<ILogicalConstruct> orderedCaseEntries = new List<ILogicalConstruct>(dfsTree.ReversePostOrder.Select(node => node.Construct as ILogicalConstruct).Where(construct => caseEntriesToDominatedNodesMap.ContainsKey(construct))); bool changed; do { changed = false; foreach (ILogicalConstruct caseEntry in orderedCaseEntries) { HashSet<ISingleEntrySubGraph> dominatedNodes; if (caseEntriesToDominatedNodesMap.TryGetValue(caseEntry, out dominatedNodes) && !IsCaseValid(caseEntry, legalPredecessors)) { legalPredecessors.ExceptWith(dominatedNodes); caseEntriesToDominatedNodesMap.Remove(caseEntry); changed = true; } } } while (changed); return caseEntriesToDominatedNodesMap; }