| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- using ARMeilleure.Common;
- using ARMeilleure.IntermediateRepresentation;
- using ARMeilleure.State;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using static ARMeilleure.IntermediateRepresentation.OperandHelper;
- namespace ARMeilleure.Translation
- {
- static partial class Ssa
- {
- private class DefMap
- {
- private readonly Dictionary<int, Operand> _map;
- private readonly BitMap _phiMasks;
- public DefMap()
- {
- _map = new Dictionary<int, Operand>();
- _phiMasks = new BitMap(RegisterConsts.TotalCount);
- }
- public bool TryAddOperand(int key, Operand operand)
- {
- return _map.TryAdd(key, operand);
- }
- public bool TryGetOperand(int key, out Operand operand)
- {
- return _map.TryGetValue(key, out operand);
- }
- public bool AddPhi(int key)
- {
- return _phiMasks.Set(key);
- }
- public bool HasPhi(int key)
- {
- return _phiMasks.IsSet(key);
- }
- }
- public static void Construct(ControlFlowGraph cfg)
- {
- var globalDefs = new DefMap[cfg.Blocks.Count];
- var localDefs = new Operand[cfg.LocalsCount + RegisterConsts.TotalCount];
- var dfPhiBlocks = new Queue<BasicBlock>();
- for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
- {
- globalDefs[block.Index] = new DefMap();
- }
- // First pass, get all defs and locals uses.
- for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
- {
- for (Node node = block.Operations.First; node != null; node = node.ListNext)
- {
- if (node is not Operation operation)
- {
- continue;
- }
- for (int index = 0; index < operation.SourcesCount; index++)
- {
- Operand src = operation.GetSource(index);
- if (TryGetId(src, out int srcKey))
- {
- Operand local = localDefs[srcKey] ?? src;
- operation.SetSource(index, local);
- }
- }
- Operand dest = operation.Destination;
- if (TryGetId(dest, out int destKey))
- {
- Operand local = Local(dest.Type);
- localDefs[destKey] = local;
- operation.Destination = local;
- }
- }
- for (int key = 0; key < localDefs.Length; key++)
- {
- Operand local = localDefs[key];
- if (local is null)
- {
- continue;
- }
- globalDefs[block.Index].TryAddOperand(key, local);
- dfPhiBlocks.Enqueue(block);
- while (dfPhiBlocks.TryDequeue(out BasicBlock dfPhiBlock))
- {
- foreach (BasicBlock domFrontier in dfPhiBlock.DominanceFrontiers)
- {
- if (globalDefs[domFrontier.Index].AddPhi(key))
- {
- dfPhiBlocks.Enqueue(domFrontier);
- }
- }
- }
- }
- Array.Clear(localDefs, 0, localDefs.Length);
- }
- // Second pass, rename variables with definitions on different blocks.
- for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
- {
- for (Node node = block.Operations.First; node != null; node = node.ListNext)
- {
- if (node is not Operation operation)
- {
- continue;
- }
- for (int index = 0; index < operation.SourcesCount; index++)
- {
- Operand src = operation.GetSource(index);
- if (TryGetId(src, out int key))
- {
- Operand local = localDefs[key];
- if (local is null)
- {
- local = FindDef(globalDefs, block, src);
- localDefs[key] = local;
- }
- operation.SetSource(index, local);
- }
- }
- }
- Array.Clear(localDefs, 0, localDefs.Length);
- }
- }
- private static Operand FindDef(DefMap[] globalDefs, BasicBlock current, Operand operand)
- {
- if (globalDefs[current.Index].HasPhi(GetId(operand)))
- {
- return InsertPhi(globalDefs, current, operand);
- }
- if (current != current.ImmediateDominator)
- {
- return FindDefOnPred(globalDefs, current.ImmediateDominator, operand);
- }
- return Undef();
- }
- private static Operand FindDefOnPred(DefMap[] globalDefs, BasicBlock current, Operand operand)
- {
- BasicBlock previous;
- do
- {
- DefMap defMap = globalDefs[current.Index];
- int key = GetId(operand);
- if (defMap.TryGetOperand(key, out Operand lastDef))
- {
- return lastDef;
- }
- if (defMap.HasPhi(key))
- {
- return InsertPhi(globalDefs, current, operand);
- }
- previous = current;
- current = current.ImmediateDominator;
- }
- while (previous != current);
- return Undef();
- }
- private static Operand InsertPhi(DefMap[] globalDefs, BasicBlock block, Operand operand)
- {
- // This block has a Phi that has not been materialized yet, but that
- // would define a new version of the variable we're looking for. We need
- // to materialize the Phi, add all the block/operand pairs into the Phi, and
- // then use the definition from that Phi.
- Operand local = Local(operand.Type);
- PhiNode phi = new PhiNode(local, block.Predecessors.Count);
- AddPhi(block, phi);
- globalDefs[block.Index].TryAddOperand(GetId(operand), local);
- for (int index = 0; index < block.Predecessors.Count; index++)
- {
- BasicBlock predecessor = block.Predecessors[index];
- phi.SetBlock(index, predecessor);
- phi.SetSource(index, FindDefOnPred(globalDefs, predecessor, operand));
- }
- return local;
- }
- private static void AddPhi(BasicBlock block, PhiNode phi)
- {
- Node node = block.Operations.First;
- if (node != null)
- {
- while (node.ListNext is PhiNode)
- {
- node = node.ListNext;
- }
- }
- if (node is PhiNode)
- {
- block.Operations.AddAfter(node, phi);
- }
- else
- {
- block.Operations.AddFirst(phi);
- }
- }
- private static bool TryGetId(Operand operand, out int result)
- {
- if (operand is { Kind: OperandKind.Register })
- {
- Register reg = operand.GetRegister();
- if (reg.Type == RegisterType.Integer)
- {
- result = reg.Index;
- }
- else if (reg.Type == RegisterType.Vector)
- {
- result = RegisterConsts.IntRegsCount + reg.Index;
- }
- else if (reg.Type == RegisterType.Flag)
- {
- result = RegisterConsts.IntAndVecRegsCount + reg.Index;
- }
- else /* if (reg.Type == RegisterType.FpFlag) */
- {
- result = RegisterConsts.FpFlagsOffset + reg.Index;
- }
- return true;
- }
- else if (operand is { Kind: OperandKind.LocalVariable } && operand.GetLocalNumber() > 0)
- {
- result = RegisterConsts.TotalCount + operand.GetLocalNumber() - 1;
- return true;
- }
- result = -1;
- return false;
- }
- private static int GetId(Operand operand)
- {
- if (!TryGetId(operand, out int key))
- {
- Debug.Fail("OperandKind must be Register or a numbered LocalVariable.");
- }
- return key;
- }
- }
- }
|