diff options
| author | TSR Berry <20988865+TSRBerry@users.noreply.github.com> | 2023-04-08 01:22:00 +0200 |
|---|---|---|
| committer | Mary <thog@protonmail.com> | 2023-04-27 23:51:14 +0200 |
| commit | cee712105850ac3385cd0091a923438167433f9f (patch) | |
| tree | 4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /ARMeilleure/CodeGen/Optimizations/TailMerge.cs | |
| parent | cd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff) | |
Move solution and projects to src
Diffstat (limited to 'ARMeilleure/CodeGen/Optimizations/TailMerge.cs')
| -rw-r--r-- | ARMeilleure/CodeGen/Optimizations/TailMerge.cs | 83 |
1 files changed, 0 insertions, 83 deletions
diff --git a/ARMeilleure/CodeGen/Optimizations/TailMerge.cs b/ARMeilleure/CodeGen/Optimizations/TailMerge.cs deleted file mode 100644 index e94df159..00000000 --- a/ARMeilleure/CodeGen/Optimizations/TailMerge.cs +++ /dev/null @@ -1,83 +0,0 @@ -using ARMeilleure.IntermediateRepresentation; -using ARMeilleure.Translation; -using static ARMeilleure.IntermediateRepresentation.Operation.Factory; - -namespace ARMeilleure.CodeGen.Optimizations -{ - static class TailMerge - { - public static void RunPass(in CompilerContext cctx) - { - ControlFlowGraph cfg = cctx.Cfg; - - BasicBlock mergedReturn = new(cfg.Blocks.Count); - - Operand returnValue; - Operation returnOp; - - if (cctx.FuncReturnType == OperandType.None) - { - returnValue = default; - returnOp = Operation(Instruction.Return, default); - } - else - { - returnValue = cfg.AllocateLocal(cctx.FuncReturnType); - returnOp = Operation(Instruction.Return, default, returnValue); - } - - mergedReturn.Frequency = BasicBlockFrequency.Cold; - mergedReturn.Operations.AddLast(returnOp); - - for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext) - { - Operation op = block.Operations.Last; - - if (op != default && op.Instruction == Instruction.Return) - { - block.Operations.Remove(op); - - if (cctx.FuncReturnType == OperandType.None) - { - PrepareMerge(block, mergedReturn); - } - else - { - Operation copyOp = Operation(Instruction.Copy, returnValue, op.GetSource(0)); - - PrepareMerge(block, mergedReturn).Append(copyOp); - } - } - } - - cfg.Blocks.AddLast(mergedReturn); - cfg.Update(); - } - - private static BasicBlock PrepareMerge(BasicBlock from, BasicBlock to) - { - BasicBlock fromPred = from.Predecessors.Count == 1 ? from.Predecessors[0] : null; - - // If the block is empty, we can try to append to the predecessor and avoid unnecessary jumps. - if (from.Operations.Count == 0 && fromPred != null && fromPred.SuccessorsCount == 1) - { - for (int i = 0; i < fromPred.SuccessorsCount; i++) - { - if (fromPred.GetSuccessor(i) == from) - { - fromPred.SetSuccessor(i, to); - } - } - - // NOTE: `from` becomes unreachable and the call to `cfg.Update()` will remove it. - return fromPred; - } - else - { - from.AddSuccessor(to); - - return from; - } - } - } -} |
