private List <Loop> FindLoops()
        {
            List <Loop> loops = null;

            foreach (var block in BasicBlocks)
            {
                if (block.PreviousBlocks.Count == 0)
                {
                    continue;
                }

                foreach (var previous in block.PreviousBlocks)
                {
                    // Is this a back-edge? Yes, if "block" dominates "previous"
                    if (AnalysisDominance.IsDominator(block, previous))
                    {
                        var loop = new Loop(block, previous);

                        (loops ?? (loops = new List <Loop>())).Add(loop);
                    }
                }
            }

            return(loops);
        }
        private List <Loop> FindLoops()
        {
            var loops  = new List <Loop>();
            var lookup = new Dictionary <BasicBlock, Loop>();

            foreach (var block in BasicBlocks)
            {
                if (block.PreviousBlocks.Count == 0)
                {
                    continue;
                }

                foreach (var previous in block.PreviousBlocks)
                {
                    // Is this a back-edge? Yes, if "block" dominates "previous"
                    if (AnalysisDominance.IsDominator(block, previous))
                    {
                        if (lookup.TryGetValue(block, out Loop loop))
                        {
                            loop.AddBackEdge(previous);
                        }
                        else
                        {
                            loop = new Loop(block, previous);
                            loops.Add(loop);
                            lookup.Add(block, loop);
                        }
                    }
                }
            }

            foreach (var loop in loops)
            {
                PopulateLoopNodes(loop);
            }

            return(loops);
        }