Esempio n. 1
0
    /*
     * Test the dominance set
     */
    private static void TestDomaintes(int index, SortedSet <int> expected)
    {
        IRGraph cfg = BuildSampleCFG();

        SortedSet <IRBlock> V      = cfg.GetSetOfAllBlocks();
        SortedSet <IRBlock> VSansR = new SortedSet <IRBlock>();

        VSansR.UnionWith(V);
        VSansR.Remove(cfg.GetGraphHead());

        SortedSet <IRBlock> result    = DominatorTree.CalculateDominatesSet(cfg, V, VSansR, cfg.GetBlock(index));
        SortedSet <int>     intResult = ConvertToIndexSet(result);

        if (!intResult.SetEquals(expected))
        {
            throw new Exception("Dominates blocks for block " + index + " is " + ConvertSetToString(intResult) + " should be " + ConvertSetToString(expected));
        }
    }
Esempio n. 2
0
    private static Dictionary <Ident, IRIdent> SimpleVariableAnalysis(IRGraph graph)
    {
        Dictionary <Ident, IRIdent> identAnalysis = new Dictionary <Ident, IRIdent>();

        foreach (IRBlock block in graph.GetSetOfAllBlocks())
        {
            foreach (IRTuple stmt in block.GetStatements())
            {
                HashSet <Ident> usedVars = stmt.GetUsedVars();
                foreach (Ident ident in usedVars)
                {
                    IRIdent identObj;
                    if (identAnalysis.ContainsKey(ident))
                    {
                        identObj = identAnalysis[ident];
                    }
                    else
                    {
                        identObj             = new IRIdent(ident, stmt);
                        identAnalysis[ident] = identObj;
                    }

                    identObj.AddUsesite(stmt);
                }

                HashSet <Ident> definedVars = stmt.GetDefinedVars();
                foreach (Ident ident in definedVars)
                {
                    if (!identAnalysis.ContainsKey(ident))
                    {
                        IRIdent identObj = new IRIdent(ident, stmt);
                        identAnalysis[ident] = identObj;
                    }
                }
            }
        }

        return(identAnalysis);
    }
Esempio n. 3
0
    private static void InsertPhiFunctions(IRGraph graph, DominatorTree dominatorTree)
    {
        foreach (IRBlock block in graph.GetSetOfAllBlocks())
        {
            Console.WriteLine();
            Console.WriteLine("***");
            Console.WriteLine("Block: " + block.GetIndex());
            block.PrintStatements();

            Console.Write("Predecessors: ");
            foreach (IRBlock pred in graph.GetPredecessors(block))
            {
                Console.Write(pred.GetIndex() + ", ");
            }
            Console.WriteLine();

            Console.Write("Live in : ");
            foreach (Ident ident in block.GetLiveIn())
            {
                Console.Write(ident + ", ");
            }
            Console.WriteLine();

            Console.Write("Defined vars: ");
            foreach (Ident ident in block.GetDefinedVars())
            {
                Console.Write(ident + ", ");
            }
            Console.WriteLine();
        }

        foreach (Ident v in graph.GetDefinedVars())
        {
            // A(v) = blocks containing an assignment to v
            HashSet <IRBlock> Av = new HashSet <IRBlock>();
            foreach (IRBlock block in graph.GetSetOfAllBlocks())
            {
                if (block.GetDefinedVars().Contains(v) && block.GetLiveIn().Contains(v))
                {
                    Av.Add(block);
                }
            }

            // place Phi tuple for each v in the iterated dominance frontier of A(v)
            HashSet <IRBlock> needsPhiFunction = new HashSet <IRBlock>();
            foreach (IRBlock Avblock in Av)
            {
                // create set of blocks that need phi functions
                foreach (IRBlock block in dominatorTree.GetDominanceFrontier(Avblock))
                {
                    needsPhiFunction.Add(block);
                }
            }

            // only want one phi function per block for each variable where appropiate
            foreach (IRBlock block in needsPhiFunction)
            {
                // Phi function should have as many arguments as it does predecessors
                List <Ident> sources = new List <Ident>();
                foreach (IRBlock b in graph.GetPredecessors(block))
                {
                    sources.Add(v);
                }

                IRTupleManyOp phi = new IRTupleManyOp(IrOp.PHI, v, sources);

                block.InsertStatement(phi, 0);
                Console.WriteLine("** SSA: Inserting phi function: " + phi.toString() + " into block " + block.GetIndex());
            }
        }
    }
Esempio n. 4
0
    private static void InsertPhiFunctions(IRGraph graph, DominatorTree dominatorTree)
    {
        foreach (IRBlock block in graph.GetSetOfAllBlocks())
        {
          Console.WriteLine();
          Console.WriteLine("***");
          Console.WriteLine("Block: " + block.GetIndex());
          block.PrintStatements();

          Console.Write("Predecessors: ");
          foreach (IRBlock pred in graph.GetPredecessors(block))
        Console.Write(pred.GetIndex() + ", ");
          Console.WriteLine();

          Console.Write("Live in : ");
          foreach (Ident ident in block.GetLiveIn())
        Console.Write(ident + ", ");
          Console.WriteLine();

          Console.Write("Defined vars: ");
          foreach (Ident ident in block.GetDefinedVars())
        Console.Write(ident + ", ");
          Console.WriteLine();
        }

        foreach (Ident v in graph.GetDefinedVars())
        {
          // A(v) = blocks containing an assignment to v
          HashSet<IRBlock> Av = new HashSet<IRBlock>();
          foreach (IRBlock block in graph.GetSetOfAllBlocks())
          {
        if (block.GetDefinedVars().Contains(v) && block.GetLiveIn().Contains(v))
          Av.Add(block);
          }

          // place Phi tuple for each v in the iterated dominance frontier of A(v)
          HashSet<IRBlock> needsPhiFunction = new HashSet<IRBlock>();
          foreach (IRBlock Avblock in Av)
          {
        // create set of blocks that need phi functions
        foreach (IRBlock block in dominatorTree.GetDominanceFrontier(Avblock))
        {
          needsPhiFunction.Add(block);
        }
          }

          // only want one phi function per block for each variable where appropiate
          foreach (IRBlock block in needsPhiFunction)
          {
        // Phi function should have as many arguments as it does predecessors
        List<Ident> sources = new List<Ident>();
        foreach (IRBlock b in graph.GetPredecessors(block))
          sources.Add(v);

        IRTupleManyOp phi = new IRTupleManyOp(IrOp.PHI, v, sources);

        block.InsertStatement(phi, 0);
        Console.WriteLine("** SSA: Inserting phi function: " + phi.toString() + " into block " + block.GetIndex());
          }
        }
    }
Esempio n. 5
0
    private static Dictionary<Ident, IRIdent> SimpleVariableAnalysis(IRGraph graph)
    {
        Dictionary<Ident, IRIdent> identAnalysis = new Dictionary<Ident, IRIdent>();

        foreach (IRBlock block in graph.GetSetOfAllBlocks())
        {
          foreach (IRTuple stmt in block.GetStatements())
          {
        HashSet<Ident> usedVars = stmt.GetUsedVars();
        foreach (Ident ident in usedVars)
        {
          IRIdent identObj;
          if (identAnalysis.ContainsKey(ident)) {
            identObj = identAnalysis[ident];
          } else {
            identObj = new IRIdent(ident, stmt);
            identAnalysis[ident] = identObj;
          }

          identObj.AddUsesite(stmt);
        }

        HashSet<Ident> definedVars = stmt.GetDefinedVars();
        foreach (Ident ident in definedVars)
        {
          if (!identAnalysis.ContainsKey(ident)) {
            IRIdent identObj = new IRIdent(ident, stmt);
            identAnalysis[ident] = identObj;
          }
        }
          }
        }

        return identAnalysis;
    }