ryujinx-mirror/ARMeilleure/Translation/ControlFlowGraph.cs
riperiperi 8226997bc7
CodeGen Optimisations (LSRA and Translator) (#978)
* Start of JIT garbage collection improvements

- thread static pool for Operand, MemoryOperand, Operation
- Operands and Operations are always to be constructed via their static
helper classes, so they can be pooled.
- removing LinkedList from Node for sources/destinations (replaced with
List<>s for now, but probably could do arrays since size is bounded)
- removing params constructors from Node
- LinkedList<> to List<> with Clear() for Operand assignments/uses
- ThreadStaticPool is very simple and basically just exists for the
purpose of our specific translation allocation problem. Right now it
will stay at the worst case allocation count for that thread (so far) -
the pool can never shrink.

- Still some cases of Operand[] that haven't been removed yet. Will need
to evaluate them (eg. is there a reasonable max number of params for
Calls?)

* ConcurrentStack instead of ConcurrentQueue for Rejit

* Optimize some parts of LSRA

- BitMap now operates on 64-bit int rather than 32-bit
- BitMap is now pooled in a ThreadStatic pool (within lrsa)
- BitMap now is now its own iterator. Marginally speeds up iterating
through the bits.
- A few cases where enumerators were generated have been converted to
forms that generate less garbage.
- New data structure for sorting _usePositions in LiveIntervals. Much
faster split, NextUseAfter, initial insertion. Random insertion is
slightly slower.
- That last one is WIP since you need to insert the values backwards. It
would be ideal if it just flipped it for you, uncomplicating things on
the caller side.

* Use a static pool of thread static pools. (yes.)

Prevents each execution thread creating its own lowCq pool and making me cry.

* Move constant value to top, change naming convention.

* Fix iteration of memory operands.

* Increase max thread count.

* Address Feedback
2020-03-18 22:44:32 +11:00

156 lines
No EOL
4.6 KiB
C#

using ARMeilleure.IntermediateRepresentation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace ARMeilleure.Translation
{
class ControlFlowGraph
{
public BasicBlock Entry { get; }
public IntrusiveList<BasicBlock> Blocks { get; }
public BasicBlock[] PostOrderBlocks { get; }
public int[] PostOrderMap { get; }
public ControlFlowGraph(BasicBlock entry, IntrusiveList<BasicBlock> blocks)
{
Entry = entry;
Blocks = blocks;
RemoveUnreachableBlocks(blocks);
HashSet<BasicBlock> visited = new HashSet<BasicBlock>();
Stack<BasicBlock> blockStack = new Stack<BasicBlock>();
PostOrderBlocks = new BasicBlock[blocks.Count];
PostOrderMap = new int[blocks.Count];
visited.Add(entry);
blockStack.Push(entry);
int index = 0;
while (blockStack.TryPop(out BasicBlock block))
{
if (block.Next != null && visited.Add(block.Next))
{
blockStack.Push(block);
blockStack.Push(block.Next);
}
else if (block.Branch != null && visited.Add(block.Branch))
{
blockStack.Push(block);
blockStack.Push(block.Branch);
}
else
{
PostOrderMap[block.Index] = index;
PostOrderBlocks[index++] = block;
}
}
}
private void RemoveUnreachableBlocks(IntrusiveList<BasicBlock> blocks)
{
HashSet<BasicBlock> visited = new HashSet<BasicBlock>();
Queue<BasicBlock> workQueue = new Queue<BasicBlock>();
visited.Add(Entry);
workQueue.Enqueue(Entry);
while (workQueue.TryDequeue(out BasicBlock block))
{
Debug.Assert(block.Index != -1, "Invalid block index.");
if (block.Next != null && visited.Add(block.Next))
{
workQueue.Enqueue(block.Next);
}
if (block.Branch != null && visited.Add(block.Branch))
{
workQueue.Enqueue(block.Branch);
}
}
if (visited.Count < blocks.Count)
{
// Remove unreachable blocks and renumber.
int index = 0;
for (BasicBlock block = blocks.First; block != null;)
{
BasicBlock nextBlock = block.ListNext;
if (!visited.Contains(block))
{
block.Next = null;
block.Branch = null;
blocks.Remove(block);
}
else
{
block.Index = index++;
}
block = nextBlock;
}
}
}
public BasicBlock SplitEdge(BasicBlock predecessor, BasicBlock successor)
{
BasicBlock splitBlock = new BasicBlock(Blocks.Count);
if (predecessor.Next == successor)
{
predecessor.Next = splitBlock;
}
if (predecessor.Branch == successor)
{
predecessor.Branch = splitBlock;
}
if (splitBlock.Predecessors.Count == 0)
{
throw new ArgumentException("Predecessor and successor are not connected.");
}
// Insert the new block on the list of blocks.
BasicBlock succPrev = successor.ListPrevious;
if (succPrev != null && succPrev != predecessor && succPrev.Next == successor)
{
// Can't insert after the predecessor or before the successor.
// Here, we insert it before the successor by also spliting another
// edge (the one between the block before "successor" and "successor").
BasicBlock splitBlock2 = new BasicBlock(splitBlock.Index + 1);
succPrev.Next = splitBlock2;
splitBlock2.Branch = successor;
splitBlock2.Operations.AddLast(OperationHelper.Operation(Instruction.Branch, null));
Blocks.AddBefore(successor, splitBlock2);
}
splitBlock.Next = successor;
Blocks.AddBefore(successor, splitBlock);
return splitBlock;
}
}
}