| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- using ARMeilleure.IntermediateRepresentation;
- using System;
- using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
- namespace ARMeilleure.CodeGen.Optimizations
- {
- static class ConstantFolding
- {
- public static void RunPass(Operation operation)
- {
- if (operation.Destination == default || operation.SourcesCount == 0)
- {
- return;
- }
- if (!AreAllSourcesConstant(operation))
- {
- return;
- }
- OperandType type = operation.Destination.Type;
- switch (operation.Instruction)
- {
- case Instruction.Add:
- if (operation.GetSource(0).Relocatable ||
- operation.GetSource(1).Relocatable)
- {
- break;
- }
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => x + y);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => x + y);
- }
- break;
- case Instruction.BitwiseAnd:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => x & y);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => x & y);
- }
- break;
- case Instruction.BitwiseExclusiveOr:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => x ^ y);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => x ^ y);
- }
- break;
- case Instruction.BitwiseNot:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => ~x);
- }
- else if (type == OperandType.I64)
- {
- EvaluateUnaryI64(operation, (x) => ~x);
- }
- break;
- case Instruction.BitwiseOr:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => x | y);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => x | y);
- }
- break;
- case Instruction.ConvertI64ToI32:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => x);
- }
- break;
- case Instruction.Compare:
- if (type == OperandType.I32 &&
- operation.GetSource(0).Type == type &&
- operation.GetSource(1).Type == type)
- {
- switch ((Comparison)operation.GetSource(2).Value)
- {
- case Comparison.Equal:
- EvaluateBinaryI32(operation, (x, y) => x == y ? 1 : 0);
- break;
- case Comparison.NotEqual:
- EvaluateBinaryI32(operation, (x, y) => x != y ? 1 : 0);
- break;
- case Comparison.Greater:
- EvaluateBinaryI32(operation, (x, y) => x > y ? 1 : 0);
- break;
- case Comparison.LessOrEqual:
- EvaluateBinaryI32(operation, (x, y) => x <= y ? 1 : 0);
- break;
- case Comparison.GreaterUI:
- EvaluateBinaryI32(operation, (x, y) => (uint)x > (uint)y ? 1 : 0);
- break;
- case Comparison.LessOrEqualUI:
- EvaluateBinaryI32(operation, (x, y) => (uint)x <= (uint)y ? 1 : 0);
- break;
- case Comparison.GreaterOrEqual:
- EvaluateBinaryI32(operation, (x, y) => x >= y ? 1 : 0);
- break;
- case Comparison.Less:
- EvaluateBinaryI32(operation, (x, y) => x < y ? 1 : 0);
- break;
- case Comparison.GreaterOrEqualUI:
- EvaluateBinaryI32(operation, (x, y) => (uint)x >= (uint)y ? 1 : 0);
- break;
- case Comparison.LessUI:
- EvaluateBinaryI32(operation, (x, y) => (uint)x < (uint)y ? 1 : 0);
- break;
- }
- }
- break;
- case Instruction.Copy:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => x);
- }
- else if (type == OperandType.I64)
- {
- EvaluateUnaryI64(operation, (x) => x);
- }
- break;
- case Instruction.Divide:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => y != 0 ? x / y : 0);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => y != 0 ? x / y : 0);
- }
- break;
- case Instruction.DivideUI:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => y != 0 ? (int)((uint)x / (uint)y) : 0);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => y != 0 ? (long)((ulong)x / (ulong)y) : 0);
- }
- break;
- case Instruction.Multiply:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => x * y);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => x * y);
- }
- break;
- case Instruction.Negate:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => -x);
- }
- else if (type == OperandType.I64)
- {
- EvaluateUnaryI64(operation, (x) => -x);
- }
- break;
- case Instruction.ShiftLeft:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => x << y);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => x << (int)y);
- }
- break;
- case Instruction.ShiftRightSI:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => x >> y);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => x >> (int)y);
- }
- break;
- case Instruction.ShiftRightUI:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => (int)((uint)x >> y));
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => (long)((ulong)x >> (int)y));
- }
- break;
- case Instruction.SignExtend16:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => (short)x);
- }
- else if (type == OperandType.I64)
- {
- EvaluateUnaryI64(operation, (x) => (short)x);
- }
- break;
- case Instruction.SignExtend32:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => x);
- }
- else if (type == OperandType.I64)
- {
- EvaluateUnaryI64(operation, (x) => (int)x);
- }
- break;
- case Instruction.SignExtend8:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => (sbyte)x);
- }
- else if (type == OperandType.I64)
- {
- EvaluateUnaryI64(operation, (x) => (sbyte)x);
- }
- break;
- case Instruction.ZeroExtend16:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => (ushort)x);
- }
- else if (type == OperandType.I64)
- {
- EvaluateUnaryI64(operation, (x) => (ushort)x);
- }
- break;
- case Instruction.ZeroExtend32:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => x);
- }
- else if (type == OperandType.I64)
- {
- EvaluateUnaryI64(operation, (x) => (uint)x);
- }
- break;
- case Instruction.ZeroExtend8:
- if (type == OperandType.I32)
- {
- EvaluateUnaryI32(operation, (x) => (byte)x);
- }
- else if (type == OperandType.I64)
- {
- EvaluateUnaryI64(operation, (x) => (byte)x);
- }
- break;
- case Instruction.Subtract:
- if (type == OperandType.I32)
- {
- EvaluateBinaryI32(operation, (x, y) => x - y);
- }
- else if (type == OperandType.I64)
- {
- EvaluateBinaryI64(operation, (x, y) => x - y);
- }
- break;
- }
- }
- private static bool AreAllSourcesConstant(Operation operation)
- {
- for (int index = 0; index < operation.SourcesCount; index++)
- {
- Operand srcOp = operation.GetSource(index);
- if (srcOp.Kind != OperandKind.Constant)
- {
- return false;
- }
- }
- return true;
- }
- private static void EvaluateUnaryI32(Operation operation, Func<int, int> op)
- {
- int x = operation.GetSource(0).AsInt32();
- operation.TurnIntoCopy(Const(op(x)));
- }
- private static void EvaluateUnaryI64(Operation operation, Func<long, long> op)
- {
- long x = operation.GetSource(0).AsInt64();
- operation.TurnIntoCopy(Const(op(x)));
- }
- private static void EvaluateBinaryI32(Operation operation, Func<int, int, int> op)
- {
- int x = operation.GetSource(0).AsInt32();
- int y = operation.GetSource(1).AsInt32();
- operation.TurnIntoCopy(Const(op(x, y)));
- }
- private static void EvaluateBinaryI64(Operation operation, Func<long, long, long> op)
- {
- long x = operation.GetSource(0).AsInt64();
- long y = operation.GetSource(1).AsInt64();
- operation.TurnIntoCopy(Const(op(x, y)));
- }
- }
- }
|