SsaDeconstruction.cs 1.4 KB

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