Small refactoring on StructuredProgram, move RemovePhis method to a separate class
This commit is contained in:
parent
711093b553
commit
29df9376b0
2 changed files with 137 additions and 119 deletions
74
Ryujinx.Graphics/Shader/StructuredIr/PhiFunction.cs
Normal file
74
Ryujinx.Graphics/Shader/StructuredIr/PhiFunction.cs
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||||
|
{
|
||||||
|
static class PhiFunction
|
||||||
|
{
|
||||||
|
public static void Remove(BasicBlock[] blocks)
|
||||||
|
{
|
||||||
|
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
|
||||||
|
{
|
||||||
|
BasicBlock block = blocks[blkIndex];
|
||||||
|
|
||||||
|
LinkedListNode<INode> node = block.Operations.First;
|
||||||
|
|
||||||
|
while (node != null)
|
||||||
|
{
|
||||||
|
LinkedListNode<INode> nextNode = node.Next;
|
||||||
|
|
||||||
|
if (!(node.Value is PhiNode phi))
|
||||||
|
{
|
||||||
|
node = nextNode;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int index = 0; index < phi.SourcesCount; index++)
|
||||||
|
{
|
||||||
|
Operand src = phi.GetSource(index);
|
||||||
|
|
||||||
|
BasicBlock srcBlock = phi.GetBlock(index);
|
||||||
|
|
||||||
|
Operation copyOp = new Operation(Instruction.Copy, phi.Dest, src);
|
||||||
|
|
||||||
|
AddBeforeBranch(srcBlock, copyOp);
|
||||||
|
}
|
||||||
|
|
||||||
|
block.Operations.Remove(node);
|
||||||
|
|
||||||
|
node = nextNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AddBeforeBranch(BasicBlock block, INode node)
|
||||||
|
{
|
||||||
|
INode lastOp = block.GetLastOp();
|
||||||
|
|
||||||
|
if (lastOp is Operation operation && IsControlFlowInst(operation.Inst))
|
||||||
|
{
|
||||||
|
block.Operations.AddBefore(block.Operations.Last, node);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
block.Operations.AddLast(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsControlFlowInst(Instruction inst)
|
||||||
|
{
|
||||||
|
switch (inst)
|
||||||
|
{
|
||||||
|
case Instruction.Branch:
|
||||||
|
case Instruction.BranchIfFalse:
|
||||||
|
case Instruction.BranchIfTrue:
|
||||||
|
case Instruction.Discard:
|
||||||
|
case Instruction.Return:
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||||
{
|
{
|
||||||
|
@ -7,7 +6,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||||
{
|
{
|
||||||
public static StructuredProgramInfo MakeStructuredProgram(BasicBlock[] blocks)
|
public static StructuredProgramInfo MakeStructuredProgram(BasicBlock[] blocks)
|
||||||
{
|
{
|
||||||
RemovePhis(blocks);
|
PhiFunction.Remove(blocks);
|
||||||
|
|
||||||
StructuredProgramContext context = new StructuredProgramContext(blocks);
|
StructuredProgramContext context = new StructuredProgramContext(blocks);
|
||||||
|
|
||||||
|
@ -19,15 +18,22 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||||
|
|
||||||
foreach (INode node in block.Operations)
|
foreach (INode node in block.Operations)
|
||||||
{
|
{
|
||||||
if (node is Operation operation)
|
AddOperation(context, (Operation)node);
|
||||||
|
}
|
||||||
|
|
||||||
|
context.LeaveBlock(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
context.PrependLocalDeclarations();
|
||||||
|
|
||||||
|
return context.Info;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AddOperation(StructuredProgramContext context, Operation operation)
|
||||||
{
|
{
|
||||||
Instruction inst = operation.Inst;
|
Instruction inst = operation.Inst;
|
||||||
|
|
||||||
if (operation.Dest != null &&
|
if (operation.Dest != null && !IsBranchInst(inst))
|
||||||
operation.Inst != Instruction.MarkLabel &&
|
|
||||||
operation.Inst != Instruction.Branch &&
|
|
||||||
operation.Inst != Instruction.BranchIfTrue &&
|
|
||||||
operation.Inst != Instruction.BranchIfFalse)
|
|
||||||
{
|
{
|
||||||
AstOperand dest = context.GetOperandDef(operation.Dest);
|
AstOperand dest = context.GetOperandDef(operation.Dest);
|
||||||
|
|
||||||
|
@ -96,76 +102,14 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||||
context.AddNode(astOperation);
|
context.AddNode(astOperation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
context.LeaveBlock(block);
|
private static bool IsBranchInst(Instruction inst)
|
||||||
}
|
|
||||||
|
|
||||||
context.PrependLocalDeclarations();
|
|
||||||
|
|
||||||
return context.Info;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void RemovePhis(BasicBlock[] blocks)
|
|
||||||
{
|
|
||||||
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
|
|
||||||
{
|
|
||||||
BasicBlock block = blocks[blkIndex];
|
|
||||||
|
|
||||||
LinkedListNode<INode> node = block.Operations.First;
|
|
||||||
|
|
||||||
while (node != null)
|
|
||||||
{
|
|
||||||
LinkedListNode<INode> nextNode = node.Next;
|
|
||||||
|
|
||||||
if (!(node.Value is PhiNode phi))
|
|
||||||
{
|
|
||||||
node = nextNode;
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int index = 0; index < phi.SourcesCount; index++)
|
|
||||||
{
|
|
||||||
Operand src = phi.GetSource(index);
|
|
||||||
|
|
||||||
BasicBlock srcBlock = phi.GetBlock(index);
|
|
||||||
|
|
||||||
Operation copyOp = new Operation(Instruction.Copy, phi.Dest, src);
|
|
||||||
|
|
||||||
AddBeforeBranch(srcBlock, copyOp);
|
|
||||||
}
|
|
||||||
|
|
||||||
block.Operations.Remove(node);
|
|
||||||
|
|
||||||
node = nextNode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void AddBeforeBranch(BasicBlock block, INode node)
|
|
||||||
{
|
|
||||||
INode lastOp = block.GetLastOp();
|
|
||||||
|
|
||||||
if (lastOp is Operation operation && IsControlFlowInst(operation.Inst))
|
|
||||||
{
|
|
||||||
block.Operations.AddBefore(block.Operations.Last, node);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
block.Operations.AddLast(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsControlFlowInst(Instruction inst)
|
|
||||||
{
|
{
|
||||||
switch (inst)
|
switch (inst)
|
||||||
{
|
{
|
||||||
case Instruction.Branch:
|
case Instruction.Branch:
|
||||||
case Instruction.BranchIfFalse:
|
case Instruction.BranchIfFalse:
|
||||||
case Instruction.BranchIfTrue:
|
case Instruction.BranchIfTrue:
|
||||||
case Instruction.Discard:
|
|
||||||
case Instruction.Return:
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue