SsaDeconstruction.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using ARMeilleure.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  4. namespace ARMeilleure.Translation
  5. {
  6. static partial class Ssa
  7. {
  8. public static void Deconstruct(ControlFlowGraph cfg)
  9. {
  10. foreach (BasicBlock block in cfg.Blocks)
  11. {
  12. LinkedListNode<Node> node = block.Operations.First;
  13. while (node?.Value is PhiNode phi)
  14. {
  15. LinkedListNode<Node> nextNode = node.Next;
  16. Operand local = Local(phi.Destination.Type);
  17. for (int index = 0; index < phi.SourcesCount; index++)
  18. {
  19. BasicBlock predecessor = phi.GetBlock(index);
  20. Operand source = phi.GetSource(index);
  21. predecessor.Append(new Operation(Instruction.Copy, local, source));
  22. phi.SetSource(index, null);
  23. }
  24. Operation copyOp = new Operation(Instruction.Copy, phi.Destination, local);
  25. block.Operations.AddBefore(node, copyOp);
  26. phi.Destination = null;
  27. block.Operations.Remove(node);
  28. node = nextNode;
  29. }
  30. }
  31. }
  32. }
  33. }