Ejemplo n.º 1
0
 private void Build()
 {
     foreach (BasicBlock b in Blocks)
     {
         HashSet <Temp.Temp> live = new HashSet <Temp.Temp>(b.LiveOut);
         for (int i = b.List.Count - 1; i >= 0; i--)
         {
             TExp inst = b.List[i];
             if (inst is Move)
             {
                 live.ExceptWith(inst.LivenessNode.Use);
                 HashSet <Temp.Temp> nodes = new HashSet <Temp.Temp>(inst.LivenessNode.Def);
                 nodes.UnionWith(inst.LivenessNode.Use);
                 foreach (Temp.Temp n in nodes)
                 {
                     GetNodeByTemp(n).MoveList.Add((Move)inst);
                 }
                 WorklistMoves.Add((Move)inst);
             }
             live.UnionWith(inst.LivenessNode.Def);
             foreach (Temp.Temp d in inst.LivenessNode.Def)
             {
                 foreach (Temp.Temp l in live)
                 {
                     AddEdge(l, d);
                 }
             }
             live.ExceptWith(inst.LivenessNode.Def);
             live.UnionWith(inst.LivenessNode.Use);
         }
     }
 }
Ejemplo n.º 2
0
 void Write(TExp exp)
 {
     if (exp is BinOp)
     {
         Write((BinOp)exp);
     }
     else if (exp is BinOpInt)
     {
         Write((BinOpInt)exp);
     }
     else if (exp is Call)
     {
         Write((Call)exp);
     }
     else if (exp is CJump)
     {
         Write((CJump)exp);
     }
     else if (exp is CJumpInt)
     {
         Write((CJumpInt)exp);
     }
     else if (exp is Jump)
     {
         Write((Jump)exp);
     }
     else if (exp is Label)
     {
         Write((Label)exp);
     }
     else if (exp is Load)
     {
         Write((Load)exp);
     }
     else if (exp is Move)
     {
         Write((Move)exp);
     }
     else if (exp is MoveInt)
     {
         Write((MoveInt)exp);
     }
     else if (exp is MoveLabel)
     {
         Write((MoveLabel)exp);
     }
     else if (exp is ReturnSink)
     {
         Write((ReturnSink)exp);
     }
     else if (exp is Store)
     {
         Write((Store)exp);
     }
 }
Ejemplo n.º 3
0
 public Node(TExp e, int i, int j, Dictionary<Temp.Temp, HashSet<PairInt>> defs)
 {
     LivenessNode node = new LivenessNode(e);
     foreach (Temp.Temp t in node.Def)
     {
         Kill.UnionWith(defs[t]);
     }
     Kill.Remove(new PairInt(i, j));
     if (node.Def.Count != 0)
     {
         Gen.Add(new PairInt(i, j));
     }
 }
Ejemplo n.º 4
0
            public Node(TExp e, int i, int j, Dictionary <Temp.Temp, HashSet <PairInt> > defs)
            {
                LivenessNode node = new LivenessNode(e);

                foreach (Temp.Temp t in node.Def)
                {
                    Kill.UnionWith(defs[t]);
                }
                Kill.Remove(new PairInt(i, j));
                if (node.Def.Count != 0)
                {
                    Gen.Add(new PairInt(i, j));
                }
            }
Ejemplo n.º 5
0
            public void ReachingDefinitions()
            {
                Queue <BasicBlock> queue = new Queue <BasicBlock>(Blocks);

                do
                {
                    BasicBlock        b   = queue.Dequeue();
                    HashSet <PairInt> old = new HashSet <PairInt>(b.ReachOut);
                    b.ReachIn = new HashSet <PairInt>();
                    foreach (BasicBlock p in b.Prev)
                    {
                        b.ReachIn.UnionWith(p.ReachOut);
                    }
                    b.ReachOut = new HashSet <PairInt>(b.ReachIn);
                    b.ReachOut.ExceptWith(b.ReachKill);
                    b.ReachOut.UnionWith(b.ReachGen);
                    if (!b.ReachOut.SetEquals(old))
                    {
                        foreach (BasicBlock s in b.Next)
                        {
                            queue.Enqueue(s);
                        }
                    }
                }while (queue.Count != 0);

                for (int i = 0; i < Blocks.Count; ++i)
                {
                    BasicBlock        b       = Blocks[i];
                    HashSet <PairInt> reachin = new HashSet <PairInt>(b.ReachIn);
                    for (int j = 0; j < b.List.Count; ++j)
                    {
                        TExp         inst = b.List[j];
                        LivenessNode node = inst.LivenessNode;
                        PairInt      q    = new PairInt(i, j);
                        foreach (Temp.Temp t in node.Use)
                        {
                            foreach (PairInt p in reachin)
                            {
                                if (Blocks[p.i].List[p.j].LivenessNode.Def.Contains(t))
                                {
                                    SetOf(DefineUseChain, t, p).Add(q);
                                    SetOf(UseDefineChain, t, q).Add(p);
                                }
                            }
                        }
                        reachin.ExceptWith(inst.ReachingDefinitionNode.Kill);
                        reachin.UnionWith(inst.ReachingDefinitionNode.Gen);
                    }
                }
            }
Ejemplo n.º 6
0
            private void RewriteProgram(HashSet <Node> spilledNodes)
            {
                HashSet <Node> newTemps = new HashSet <Node>();

                foreach (Node v in spilledNodes)
                {
                    InFrame a = (InFrame)Frame.AllocLocal(true);
                    foreach (BasicBlock b in Blocks)
                    {
                        for (int i = 0; i < b.List.Count; i++)
                        {
                            TExp inst = b.List[i];
                            if (inst.LivenessNode == null)
                            {
                                continue;
                            }
                            if (inst.LivenessNode.Use.Contains(v.Temp))
                            {
                                Temp.Temp p = new Temp.Temp();
                                newTemps.Add(GetNodeByTemp(p));
                                GetNodeByTemp(p).IsNew = true;
                                b.List.Insert(i, new Load(Frame.FP(), a.Offset, p));
                                b.List[++i].ReplaceUse(v.Temp, p);
                            }
                            if (inst.LivenessNode.Def.Contains(v.Temp))
                            {
                                Temp.Temp p = new Temp.Temp();
                                newTemps.Add(GetNodeByTemp(p));
                                GetNodeByTemp(p).IsNew = true;
                                b.List.Insert(i + 1, new Store(Frame.FP(), a.Offset, p));
                                b.List[i++].ReplaceDef(v.Temp, p);
                            }
                        }
                    }
                }
                spilledNodes.Clear();
                Initial = newTemps;
                Initial.UnionWith(ColoredNodes);
                Initial.UnionWith(CoalescedNodes);
                ColoredNodes.Clear();
                CoalescedNodes.Clear();
            }
Ejemplo n.º 7
0
 public ReachingDefinition(List <BasicBlock> blocks)
 {
     Blocks = blocks;
     for (int i = 0; i < Blocks.Count; ++i)
     {
         BasicBlock b = Blocks[i];
         for (int j = 0; j < b.List.Count; ++j)
         {
             TExp         inst = b.List[j];
             LivenessNode node = inst.LivenessNode = new LivenessNode(inst);
             foreach (Temp.Temp t in node.Def)
             {
                 if (Defs[t] == null)
                 {
                     Defs.Add(t, new HashSet <PairInt>());
                 }
                 Defs[t].Add(new PairInt(i, j));
             }
         }
     }
     for (int i = 0; i < Blocks.Count; ++i)
     {
         BasicBlock b = Blocks[i];
         b.ReachIn.Clear();
         b.ReachOut.Clear();
         b.ReachGen.Clear();
         b.ReachKill.Clear();
         for (int j = 0; j < b.List.Count; ++j)
         {
             TExp inst = b.List[j];
             Node node = inst.ReachingDefinitionNode = new Node(inst, i, j, Defs);
             b.ReachGen.ExceptWith(node.Kill);
             b.ReachGen.UnionWith(node.Gen);
             b.ReachKill.UnionWith(node.Kill);
         }
     }
 }
Ejemplo n.º 8
0
 public LivenessNode(TExp k)
 {
     if (k is BinOp)
     {
         Use.Add((k as BinOp).Left);
         Use.Add((k as BinOp).Right);
         Def.Add((k as BinOp).Dst);
     }
     else if (k is BinOpInt)
     {
         Use.Add(((BinOpInt)k).Left);
         Def.Add(((BinOpInt)k).Dst);
     }
     else if (k is Call)
     {
         //Use: a0-a3
         for (int i = 0; i < 4; i++)
         {
             Use.Add(MipsFrame.Reg[4 + i]);
         }
         //define: v0, v1
         Def.Add(MipsFrame.Reg[2]);
         Def.Add(MipsFrame.Reg[3]);
         //define: a0-a3
         for (int i = 0; i < 4; i++)
         {
             Def.Add(MipsFrame.Reg[4 + i]);
         }
         //define: t0-t9
         for (int i = 0; i < 8; i++)
         {
             Def.Add(MipsFrame.Reg[8 + i]);
         }
         Def.Add(MipsFrame.Reg[24]);
         Def.Add(MipsFrame.Reg[25]);
         //define: ra
         Def.Add(MipsFrame.Reg[31]);
         //define: gp
         Def.Add(MipsFrame.Reg[28]);
     }
     else if (k is CJump)
     {
         Use.Add(((CJump)k).Left);
         Use.Add(((CJump)k).Right);
     }
     else if (k is CJumpInt)
     {
         Use.Add(((CJumpInt)k).Left);
     }
     else if (k is Jump)
     {
     }
     else if (k is Label)
     {
     }
     else if (k is Load)
     {
         Use.Add(((Load)k).Mem);
         Def.Add(((Load)k).Dst);
     }
     else if (k is Move)
     {
         Use.Add(((Move)k).Src);
         Def.Add(((Move)k).Dst);
     }
     else if (k is MoveInt)
     {
         Def.Add(((MoveInt)k).Dst);
     }
     else if (k is MoveLabel)
     {
         Def.Add(((MoveLabel)k).Dst);
     }
     else if (k is ReturnSink)
     {
         //v0
         Use.Add(MipsFrame.Reg[2]);
         //sp
         Use.Add(MipsFrame.Reg[29]);
         //fp
         Use.Add(MipsFrame.Reg[30]);
         //ra
         Use.Add(MipsFrame.Reg[31]);
         //s0-s7
         for (int i = 0; i < 8; i++)
         {
             Use.Add(MipsFrame.Reg[16 + i]);
         }
     }
     else if (k is Store)
     {
         Use.Add(((Store)k).Mem);
         Use.Add(((Store)k).Src);
     }
 }