2018-12-11 01:58:52 +01:00
|
|
|
using System;
|
2018-10-31 02:43:02 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace ChocolArm64.Translation
|
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
class RegisterUsage
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
public const long CallerSavedIntRegistersMask = 0x7fL << 9;
|
|
|
|
public const long PStateNzcvFlagsMask = 0xfL << 60;
|
|
|
|
|
|
|
|
public const long CallerSavedVecRegistersMask = 0xffffL << 16;
|
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
private class PathIo
|
|
|
|
{
|
|
|
|
private Dictionary<ILBlock, long> _allInputs;
|
|
|
|
private Dictionary<ILBlock, long> _cmnOutputs;
|
|
|
|
|
|
|
|
private long _allOutputs;
|
|
|
|
|
|
|
|
public PathIo()
|
|
|
|
{
|
|
|
|
_allInputs = new Dictionary<ILBlock, long>();
|
|
|
|
_cmnOutputs = new Dictionary<ILBlock, long>();
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
public void Set(ILBlock entry, long inputs, long outputs)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
if (!_allInputs.TryAdd(entry, inputs))
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
_allInputs[entry] |= inputs;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
if (!_cmnOutputs.TryAdd(entry, outputs))
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
_cmnOutputs[entry] &= outputs;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_allOutputs |= outputs;
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
public long GetInputs(ILBlock entry)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
if (_allInputs.TryGetValue(entry, out long inputs))
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
//We also need to read the registers that may not be written
|
|
|
|
//by all paths that can reach a exit point, to ensure that
|
|
|
|
//the local variable will not remain uninitialized depending
|
|
|
|
//on the flow path taken.
|
|
|
|
return inputs | (_allOutputs & ~_cmnOutputs[entry]);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long GetOutputs()
|
|
|
|
{
|
|
|
|
return _allOutputs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Dictionary<ILBlock, PathIo> _intPaths;
|
|
|
|
private Dictionary<ILBlock, PathIo> _vecPaths;
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
private struct BlockIo : IEquatable<BlockIo>
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
public ILBlock Block { get; }
|
|
|
|
public ILBlock Entry { get; }
|
|
|
|
|
|
|
|
public long IntInputs { get; set; }
|
|
|
|
public long VecInputs { get; set; }
|
|
|
|
public long IntOutputs { get; set; }
|
|
|
|
public long VecOutputs { get; set; }
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
public BlockIo(ILBlock block, ILBlock entry)
|
|
|
|
{
|
|
|
|
Block = block;
|
|
|
|
Entry = entry;
|
|
|
|
|
|
|
|
IntInputs = IntOutputs = 0;
|
|
|
|
VecInputs = VecOutputs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockIo(
|
|
|
|
ILBlock block,
|
|
|
|
ILBlock entry,
|
|
|
|
long intInputs,
|
|
|
|
long vecInputs,
|
|
|
|
long intOutputs,
|
|
|
|
long vecOutputs) : this(block, entry)
|
|
|
|
{
|
|
|
|
IntInputs = intInputs;
|
|
|
|
VecInputs = vecInputs;
|
|
|
|
IntOutputs = intOutputs;
|
|
|
|
VecOutputs = vecOutputs;
|
|
|
|
}
|
2018-12-11 01:58:52 +01:00
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (!(obj is BlockIo other))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
return Equals(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(BlockIo other)
|
|
|
|
{
|
2018-12-11 01:58:52 +01:00
|
|
|
return other.Block == Block &&
|
|
|
|
other.Entry == Entry &&
|
|
|
|
other.IntInputs == IntInputs &&
|
|
|
|
other.VecInputs == VecInputs &&
|
|
|
|
other.IntOutputs == IntOutputs &&
|
|
|
|
other.VecOutputs == VecOutputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return HashCode.Combine(Block, Entry, IntInputs, VecInputs, IntOutputs, VecOutputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator ==(BlockIo lhs, BlockIo rhs)
|
|
|
|
{
|
|
|
|
return lhs.Equals(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(BlockIo lhs, BlockIo rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
public RegisterUsage()
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
|
|
|
_intPaths = new Dictionary<ILBlock, PathIo>();
|
|
|
|
_vecPaths = new Dictionary<ILBlock, PathIo>();
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
public void BuildUses(ILBlock entry)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
|
|
|
//This will go through all possible paths on the graph,
|
|
|
|
//and store all inputs/outputs for each block. A register
|
|
|
|
//that was previously written to already is not considered an input.
|
|
|
|
//When a block can be reached by more than one path, then the
|
|
|
|
//output from all paths needs to be set for this block, and
|
|
|
|
//only outputs present in all of the parent blocks can be considered
|
2019-02-28 03:03:31 +01:00
|
|
|
//when doing input elimination. Each block chain has a entry, that's where
|
2018-10-31 02:43:02 +01:00
|
|
|
//the code starts executing. They are present on the subroutine start point,
|
|
|
|
//and on call return points too (address written to X30 by BL).
|
|
|
|
HashSet<BlockIo> visited = new HashSet<BlockIo>();
|
|
|
|
|
|
|
|
Queue<BlockIo> unvisited = new Queue<BlockIo>();
|
|
|
|
|
|
|
|
void Enqueue(BlockIo block)
|
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
if (visited.Add(block))
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
|
|
|
unvisited.Enqueue(block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
Enqueue(new BlockIo(entry, entry));
|
2018-10-31 02:43:02 +01:00
|
|
|
|
|
|
|
while (unvisited.Count > 0)
|
|
|
|
{
|
|
|
|
BlockIo current = unvisited.Dequeue();
|
|
|
|
|
|
|
|
current.IntInputs |= current.Block.IntInputs & ~current.IntOutputs;
|
|
|
|
current.VecInputs |= current.Block.VecInputs & ~current.VecOutputs;
|
|
|
|
current.IntOutputs |= current.Block.IntOutputs;
|
|
|
|
current.VecOutputs |= current.Block.VecOutputs;
|
|
|
|
|
|
|
|
//Check if this is a exit block
|
|
|
|
//(a block that returns or calls another sub).
|
|
|
|
if ((current.Block.Next == null &&
|
|
|
|
current.Block.Branch == null) || current.Block.HasStateStore)
|
|
|
|
{
|
|
|
|
if (!_intPaths.TryGetValue(current.Block, out PathIo intPath))
|
|
|
|
{
|
|
|
|
_intPaths.Add(current.Block, intPath = new PathIo());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_vecPaths.TryGetValue(current.Block, out PathIo vecPath))
|
|
|
|
{
|
|
|
|
_vecPaths.Add(current.Block, vecPath = new PathIo());
|
|
|
|
}
|
|
|
|
|
|
|
|
intPath.Set(current.Entry, current.IntInputs, current.IntOutputs);
|
|
|
|
vecPath.Set(current.Entry, current.VecInputs, current.VecOutputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnqueueFromCurrent(ILBlock block, bool retTarget)
|
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
BlockIo blockIo;
|
2018-10-31 02:43:02 +01:00
|
|
|
|
|
|
|
if (retTarget)
|
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
blockIo = new BlockIo(block, block);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
blockIo = new BlockIo(
|
|
|
|
block,
|
|
|
|
current.Entry,
|
|
|
|
current.IntInputs,
|
|
|
|
current.VecInputs,
|
|
|
|
current.IntOutputs,
|
|
|
|
current.VecOutputs);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
Enqueue(blockIo);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (current.Block.Next != null)
|
|
|
|
{
|
|
|
|
EnqueueFromCurrent(current.Block.Next, current.Block.HasStateStore);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.Block.Branch != null)
|
|
|
|
{
|
|
|
|
EnqueueFromCurrent(current.Block.Branch, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
public long GetIntInputs(ILBlock entry) => GetInputsImpl(entry, _intPaths.Values);
|
|
|
|
public long GetVecInputs(ILBlock entry) => GetInputsImpl(entry, _vecPaths.Values);
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
private long GetInputsImpl(ILBlock entry, IEnumerable<PathIo> values)
|
|
|
|
{
|
|
|
|
long inputs = 0;
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
foreach (PathIo path in values)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
inputs |= path.GetInputs(entry);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
return inputs;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
public long GetIntNotInputs(ILBlock entry) => GetNotInputsImpl(entry, _intPaths.Values);
|
|
|
|
public long GetVecNotInputs(ILBlock entry) => GetNotInputsImpl(entry, _vecPaths.Values);
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
private long GetNotInputsImpl(ILBlock entry, IEnumerable<PathIo> values)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
//Returns a mask with registers that are written to
|
|
|
|
//before being read. Only those registers that are
|
|
|
|
//written in all paths, and is not read before being
|
|
|
|
//written to on those paths, should be set on the mask.
|
|
|
|
long mask = -1L;
|
2018-10-31 02:43:02 +01:00
|
|
|
|
|
|
|
foreach (PathIo path in values)
|
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
mask &= path.GetOutputs() & ~path.GetInputs(entry);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 03:03:31 +01:00
|
|
|
return mask;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public long GetIntOutputs(ILBlock block) => _intPaths[block].GetOutputs();
|
|
|
|
public long GetVecOutputs(ILBlock block) => _vecPaths[block].GetOutputs();
|
2019-02-28 03:03:31 +01:00
|
|
|
|
|
|
|
public static long ClearCallerSavedIntRegs(long mask, bool isAarch64)
|
|
|
|
{
|
|
|
|
//TODO: ARM32 support.
|
|
|
|
if (isAarch64)
|
|
|
|
{
|
|
|
|
mask &= ~(CallerSavedIntRegistersMask | PStateNzcvFlagsMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long ClearCallerSavedVecRegs(long mask, bool isAarch64)
|
|
|
|
{
|
|
|
|
//TODO: ARM32 support.
|
|
|
|
if (isAarch64)
|
|
|
|
{
|
|
|
|
mask &= ~CallerSavedVecRegistersMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
}
|