public void AllocateRegisters(InterferenceGraph graph) { SpilledRegisters.Clear(); RegistersColoring.Clear(); mapping = InterferenceGraph.copyGraph(graph); Initialize(graph); do { if (toSimplify.Count > 0) { Simplify(); } else if (toFreeze.Count > 0) { Freeze(); } else if (toSpill.Count > 0) { Spill(); } UpdateVertexPartition(graph); } while(toSimplify.Count > 0 || toSpill.Count > 0 || toFreeze.Count > 0); AssignColors(); }
private void AssignColors() { while (stack.Count > 0) { var vertex = stack.Pop(); var forbidden = new HashSet <HardwareRegisterNode> (); foreach (Vertex neigh in vertex.NonCopyNeighbors) { if (RegistersColoring.ContainsKey(neigh.Register)) { forbidden.Add(RegistersColoring [neigh.Register]); } } var occurs = new Dictionary <HardwareRegisterNode, int> (); foreach (Vertex neigh in vertex.CopyNeighbors) { if (RegistersColoring.ContainsKey(neigh.Register)) { if (!occurs.ContainsKey(RegistersColoring [neigh.Register])) { occurs [RegistersColoring [neigh.Register]] = 1; } else { occurs [RegistersColoring [neigh.Register]]++; } } } if (forbidden.Count >= registers.Count) { SpilledRegisters.Add(vertex.Register); } else { int mostOccurs = 0; HardwareRegisterNode choice = null; foreach (HardwareRegisterNode reg in registers) { if (!forbidden.Contains(reg)) { var tmp = occurs.ContainsKey(reg) ? occurs[reg] : 0; if (tmp >= mostOccurs) { mostOccurs = tmp; choice = reg; } } } RegistersColoring [vertex.Register] = choice; } } }