SsaDeconstruction.cs 1.5 KB

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