private List <VariableDescriptor> ComputeDependencyChain(VariableDescriptor source, VariableDescriptor target, HashSet <VariableDescriptor> visited)
        {
            if (source.Equals(target))
            {
                return(new List <VariableDescriptor> {
                    target
                });
            }

            visited.Add(source);

            foreach (var w in dependsOnNonTransitive.Successors(source))
            {
                if (visited.Contains(w))
                {
                    continue;
                }
                var result = ComputeDependencyChain(w, target, visited);
                if (result != null)
                {
                    result.Insert(0, source);
                    return(result);
                }
            }

            return(null);
        }
        public void ShowDependencyChain(VariableDescriptor source, VariableDescriptor target)
        {
            var chain = ComputeDependencyChain(source, target, new HashSet <VariableDescriptor>());

            if (chain == null)
            {
                Console.WriteLine("No chain between " + source + " and " + target);
            }
            else
            {
                bool first = true;
                foreach (var v in chain)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        Console.Write(" -> ");
                    }
                    Console.Write(v);
                }
            }
            Console.WriteLine(); Console.WriteLine();
        }
 void AddDependences(VariableDescriptor v, IEnumerable <VariableDescriptor> vs, string reason, IToken tok)
 {
     foreach (var n in vs)
     {
         if (CommandLineOptions.Clo.DebugStagedHoudini)
         {
             Console.WriteLine("Adding dependence " + v + " -> " + n + ", reason: " + reason + "(" + tok.line + ":" + tok.col + ")");
         }
         dependsOnNonTransitive.AddEdge(v, n);
     }
 }
 private void AddControlDependences(Block b, VariableDescriptor v, string reason, IToken tok)
 {
     if (!BlockToControllingBlocks.ContainsKey(b))
     {
         return;
     }
     foreach (var controller in BlockToControllingBlocks[b])
     {
         AddDependences(v, ControllingBlockToVariables[controller], reason + " controlling block at (" + controller.tok.line + ":" + controller.tok.col + ")", tok);
     }
 }
 private void HandleAssign(string proc, Block b, AssignCmd assign)
 {
     foreach (var assignPair in assign.Lhss.Zip(assign.Rhss).Where
                  (Item => VariableRelevantToAnalysis(Item.Item1.DeepAssignedVariable, proc)))
     {
         VariableDescriptor assignedVariable = MakeDescriptor(proc, assignPair.Item1.DeepAssignedVariable);
         AddDependences(assignedVariable, GetReferencedVariables(assignPair.Item1, proc),
                        "LHS of assignment", assign.tok);
         AddDependences(assignedVariable, GetReferencedVariables(assignPair.Item2, proc),
                        "RHS of assignment", assign.tok);
         AddControlDependences(b, assignedVariable, "Variable assigned under control dependence", assign.tok);
     }
 }
        public HashSet <VariableDescriptor> DependsOn(VariableDescriptor v)
        {
            if (DependsOnSCCsDAG == null)
            {
                if (CommandLineOptions.Clo.Trace)
                {
                    Console.WriteLine("Variable dependence: computing SCCs");
                }

                Adjacency <VariableDescriptor> next = new Adjacency <VariableDescriptor>(dependsOnNonTransitive.Successors);
                Adjacency <VariableDescriptor> prev = new Adjacency <VariableDescriptor>(dependsOnNonTransitive.Predecessors);
                StronglyConnectedComponents <VariableDescriptor> DependsOnSCCs =
                    new StronglyConnectedComponents <VariableDescriptor>(
                        dependsOnNonTransitive.Nodes, next, prev);
                DependsOnSCCs.Compute();

                VariableDescriptorToSCC = new Dictionary <VariableDescriptor, SCC <VariableDescriptor> >();
                foreach (var scc in DependsOnSCCs)
                {
                    foreach (var s in scc)
                    {
                        VariableDescriptorToSCC[s] = scc;
                    }
                }

                DependsOnSCCsDAG = new Graph <SCC <VariableDescriptor> >();
                foreach (var edge in dependsOnNonTransitive.Edges)
                {
                    if (VariableDescriptorToSCC[edge.Item1] != VariableDescriptorToSCC[edge.Item2])
                    {
                        DependsOnSCCsDAG.AddEdge(VariableDescriptorToSCC[edge.Item1], VariableDescriptorToSCC[edge.Item2]);
                    }
                }

                SCC <VariableDescriptor> dummy = new SCC <VariableDescriptor>();
                foreach (var n in dependsOnNonTransitive.Nodes)
                {
                    DependsOnSCCsDAG.AddEdge(VariableDescriptorToSCC[n], dummy);
                }

                if (CommandLineOptions.Clo.Trace)
                {
                    Console.WriteLine("Variable dependence: SCCs computed!");
                }
            }

            return(DependsOn(VariableDescriptorToSCC[v]));
        }
        public override bool Equals(object that)
        {
            if (object.ReferenceEquals(this, that))
            {
                return(true);
            }

            VariableDescriptor thatDescriptor = that as VariableDescriptor;

            if (thatDescriptor == null)
            {
                return(false);
            }

            return(this.Name.Equals(thatDescriptor.Name));
        }
        private void dump(VariableDescriptor vd)
        {
            Console.Write(vd + " <- {");
            bool first = true;

            var SortedDependents = DependsOn(vd).ToList();

            SortedDependents.Sort();
            foreach (var Descriptor in SortedDependents)
            {
                Console.Write((first ? "" : ",") + "\n  " + Descriptor);
                if (first)
                {
                    first = false;
                }
            }
            Debug.Assert(!first);
            Console.WriteLine("\n}\n");
        }
        private void dump(VariableDescriptor vd)
        {
            Console.Write(vd + " <- {");
              bool first = true;

              var SortedDependents = DependsOn(vd).ToList();
              SortedDependents.Sort();
              foreach (var Descriptor in SortedDependents) {
            Console.Write((first ? "" : ",") + "\n  " + Descriptor);
            if (first) {
              first = false;
            }
              }
              Debug.Assert(!first);
              Console.WriteLine("\n}\n");
        }
        private List<VariableDescriptor> ComputeDependencyChain(VariableDescriptor source, VariableDescriptor target, HashSet<VariableDescriptor> visited)
        {
            if(source.Equals(target)) {
            return new List<VariableDescriptor> { target };
              }

              visited.Add(source);

              foreach(var w in dependsOnNonTransitive.Successors(source)) {
            if(visited.Contains(w)) {
              continue;
            }
            var result = ComputeDependencyChain(w, target, visited);
            if(result != null) {
              result.Insert(0, source);
              return result;
            }
              }

              return null;
        }
 void AddDependences(VariableDescriptor v, IEnumerable<VariableDescriptor> vs, string reason, IToken tok)
 {
     foreach (var n in vs) {
     if(CommandLineOptions.Clo.DebugStagedHoudini) {
       Console.WriteLine("Adding dependence " + v + " -> " + n + ", reason: " + reason + "(" + tok.line + ":" + tok.col + ")");
     }
     dependsOnNonTransitive.AddEdge(v, n);
       }
 }
 private void AddControlDependences(Block b, VariableDescriptor v, string reason, IToken tok)
 {
     if (!BlockToControllingBlocks.ContainsKey(b)) {
     return;
       }
       foreach (var controller in BlockToControllingBlocks[b]) {
     AddDependences(v, ControllingBlockToVariables[controller], reason + " controlling block at (" + controller.tok.line + ":" + controller.tok.col + ")", tok);
       }
 }
 public void ShowDependencyChain(VariableDescriptor source, VariableDescriptor target)
 {
     var chain = ComputeDependencyChain(source, target, new HashSet<VariableDescriptor>());
       if(chain == null) {
     Console.WriteLine("No chain between " + source + " and " + target);
       } else {
     bool first = true;
     foreach(var v in chain) {
       if(first) {
     first = false;
       } else {
     Console.Write(" -> ");
       }
       Console.Write(v);
     }
       }
       Console.WriteLine(); Console.WriteLine();
 }
        public HashSet<VariableDescriptor> DependsOn(VariableDescriptor v)
        {
            if (DependsOnSCCsDAG == null) {
            if (CommandLineOptions.Clo.Trace) {
              Console.WriteLine("Variable dependence: computing SCCs");
            }
            Adjacency<VariableDescriptor> next = new Adjacency<VariableDescriptor>(dependsOnNonTransitive.Successors);
            Adjacency<VariableDescriptor> prev = new Adjacency<VariableDescriptor>(dependsOnNonTransitive.Predecessors);
            StronglyConnectedComponents<VariableDescriptor> DependsOnSCCs = new StronglyConnectedComponents<VariableDescriptor>(
              dependsOnNonTransitive.Nodes, next, prev);
            DependsOnSCCs.Compute();

            VariableDescriptorToSCC = new Dictionary<VariableDescriptor, SCC<VariableDescriptor>>();
            foreach (var scc in DependsOnSCCs) {
              foreach (var s in scc) {
            VariableDescriptorToSCC[s] = scc;
              }
            }

            DependsOnSCCsDAG = new Graph<SCC<VariableDescriptor>>();
            foreach (var edge in dependsOnNonTransitive.Edges) {
              if (VariableDescriptorToSCC[edge.Item1] != VariableDescriptorToSCC[edge.Item2]) {
            DependsOnSCCsDAG.AddEdge(VariableDescriptorToSCC[edge.Item1], VariableDescriptorToSCC[edge.Item2]);
              }
            }

            SCC<VariableDescriptor> dummy = new SCC<VariableDescriptor>();
            foreach (var n in dependsOnNonTransitive.Nodes) {
              DependsOnSCCsDAG.AddEdge(VariableDescriptorToSCC[n], dummy);
            }

            if (CommandLineOptions.Clo.Trace) {
              Console.WriteLine("Variable dependence: SCCs computed!");
            }
              }
              return DependsOn(VariableDescriptorToSCC[v]);
        }