public void RemoveRedundancies(I386Prg prg) { foreach (var n in prg.Functions) { List <IMachineInstruction> redundantInstrs = new List <IMachineInstruction>(); foreach (var inst in n.body) { if (inst is InstrBinary i) { if (i.kind == InstrBinary.Kind.MOV) { if (i.src is Operand.Reg reg1 && i.dst is Operand.Reg reg2) { if (reg1.reg.Equals(reg2.reg)) { redundantInstrs.Add(i); } } } } } foreach (var red in redundantInstrs) { n.body.Remove(red); } } }
public void AllocateRegisters(I386Prg prg, List <Temp> registers) { GraphGenerator graphGenerator = new GraphGenerator(); LivenessMachine livenessMachine = new LivenessMachine(); foreach (var n in prg.Functions) { Console.WriteLine("Registerallocation for " + n.Name); while (true) { registerDict.Clear(); var cfg = graphGenerator.GenGraphs(n); //Console.WriteLine("Generated Graph."); var interferenceGraph = livenessMachine.CalcInterferenceGraph(cfg, registers); //Console.WriteLine("Generated Interference Graph"); var nodesToSpill = ColorizeGraph(interferenceGraph, registers, registers.Count, n); //Console.WriteLine("Allocated Registers"); if (nodesToSpill.Count > 0) { n.Spill(nodesToSpill); //Console.WriteLine("Spilled."); } else { RenameTemps(n, registerDict); break; } } } }