using Dna.DataStructures;
using Rivers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dna.ControlFlow.Analysis
{
///
/// Class for identifying back edges in a control flow graph.
///
public static class BackEdgeAnalysis
{
///
public static HashSet GetBackEdges(ControlFlowGraph cfg) => GetBackEdges(cfg, new ImmutableDomTree(cfg));
///
/// Gets all outgoing edges where the target dominates its source.
///
///
public static HashSet GetBackEdges(Graph cfg, ImmutableDomTree domTree)
{
// Collect all outgoing edges where node {S} is jumping to node {T} which dominates {S}.
var backEdges = cfg.Nodes
.SelectMany(node => node.OutgoingEdges)
.Where(outEdge => domTree.IsDominatedBy(outEdge.Source, outEdge.Target));
// Return a set of all back edges.
return new HashSet(backEdges);
}
}
}