Files
Xenia-Canary/src/xenia/cpu/compiler/passes/simplification_pass.cc
chss95cs@gmail.com 33a6cfc0a7 Add special cases to DOT_PRODUCT_3/4 that detect whether they're calculating lengthsquared
Add alternate path to DOT_PRODUCT_3/4 for use_fast_dot_product that skips all the status register stuff and just remaps inf to qnan
Add OPCODE_TO_SINGLE to replace the CONVERT_F32_F64 - CONVERT_F64_F32 sequence we used to emit with the idea that a backend could implement a more correct rounding behavior if possible on its arch
Remove some impossible sequences like MUL_HI_I8/I16, MUL_ADD_F32, DIV_V128. These instructions have no equivalent in PPC. Many other instructions are unused/dead code and should be removed to make the x64 backend a better reference for future ones
Add backend_flags to Instr. Basically, flags field that a backend can use for whatever it wants when generating code.
Add backend instr flag to x64 that tells it to not generate code for an instruction. this allows sequences to consume subsequent instructions
Generate actual x64 code for VSL instruction instead of using callnativesafe
Detect repeated COMPARE instructions w/ identical operands and reuse the results in FLAGS if so. this eliminates a ton of garbage compare/set instructions.
If a COMPARE instructions destination is stored to context with no intervening instruction and no additional uses besides the store, do setx [ctx address]
Detect prefetchw and use it in CACHE_CONTROL if prefetch for write is requested instead of doing prefetch to all cache levels
Fixed an accident in an earlier commit by me, VECTOR_DENORMFLUSH was not being emitted at all, so denormal inputs to MUL_ADD_V128 were not becoming zero and outputs from DOT_PRODUCT_X were not either. I believe this introduced a bug into RDR where a wagon wouldnt spawn? (https://discord.com/channels/308194948048486401/308207592482668545/1000443975817252874)
Compute fresx in double precision using RECIP_F64 and then round to single instead of doing (double)(1.0f / (float)value), matching original behavior better
Refactor some of ppc_emit_fpu, much of the InstrEmit function are identical except for whether they round to single or not
Added "tail emitters" to X64Emitter. These are callbacks that get invoked with their label and the X64Emitter after the epilog code. This allows us to move cold code out of the critical path and in the future place constant pools near functions
guest_to_host_thunk/host_to_guest_thunk now gets directly rel32 called, instead of doing a mov
Add X64BackendContext structure, represents data before the start of the PPCContext
Instead of doing branchless sequence, do a compare and jump to tail emitted code for address translation. This makes converting addresses a 3 uop affair in most cases.
Do qnan move for dot product in a tail emitter
Detect whether EFLAGS bits are independent variables for the current cpu (not really detecting it ehe, just checking if zen) and if so generate inc/dec for add/sub 1
Detect whether low 32 bits of membase are 0. If they are then we can use membasereg.cvt32() in place of immediate 0 in many places, particularly in stores
Detect LOAD MODIFY STORE pattern for context variables (currently only done for 64 bit ones) and turn them into modify [context ptr]. This is done for add, sub, and, or, xor, not, neg
Tail emit error handling for TRAP opcodes
Stub out unused trap opcodes like TRAP_TRUE_I32, TRAP_TRUE_I64, TRAP_TRUE_I16 (the call_true/return_true opcodes for these types are also probably unused)
Remove BackpropTruncations. It was poorly written and causes crashes on the game Viva pinata (https://discord.com/channels/308194948048486401/701111856600711208/1000249460451983420)
2022-07-23 12:10:07 -07:00

1235 lines
39 KiB
C++

/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/cpu/compiler/passes/simplification_pass.h"
#include "xenia/base/byte_order.h"
#include "xenia/base/logging.h"
#include "xenia/base/profiling.h"
namespace xe {
namespace cpu {
namespace compiler {
namespace passes {
// TODO(benvanik): remove when enums redefined.
using namespace xe::cpu::hir;
using xe::cpu::hir::HIRBuilder;
using xe::cpu::hir::Instr;
using xe::cpu::hir::Value;
using vmask_portion_t = uint64_t;
template <uint32_t Ndwords>
struct Valuemask_t {
vmask_portion_t bits[Ndwords];
static Valuemask_t create_empty(vmask_portion_t fill = 0) {
Valuemask_t result;
for (uint32_t i = 0; i < Ndwords; ++i) {
result.bits[i] = fill;
}
return result;
}
template <typename TCallable>
Valuemask_t operate(TCallable&& oper) const {
Valuemask_t result = create_empty();
for (uint32_t i = 0; i < Ndwords; ++i) {
result.bits[i] = oper(bits[i]);
}
return result;
}
template <typename TCallable>
Valuemask_t operate(TCallable&& oper, Valuemask_t other) const {
Valuemask_t result = create_empty();
for (uint32_t i = 0; i < Ndwords; ++i) {
result.bits[i] = oper(bits[i], other.bits[i]);
}
return result;
}
Valuemask_t operator&(ValueMask other) const {
return operate([](vmask_portion_t x, vmask_portion_t y) { return x & y; },
other);
}
Valuemask_t operator|(ValueMask other) const {
return operate([](vmask_portion_t x, vmask_portion_t y) { return x | y; },
other);
}
Valuemask_t operator^(ValueMask other) const {
return operate([](vmask_portion_t x, vmask_portion_t y) { return x ^ y; },
other);
}
Valuemask_t operator~() const {
return operate([](vmask_portion_t x) { return ~x; }, other);
}
};
SimplificationPass::SimplificationPass() : ConditionalGroupSubpass() {}
SimplificationPass::~SimplificationPass() {}
bool SimplificationPass::Run(HIRBuilder* builder, bool& result) {
result = false;
bool iter_result = false;
do {
iter_result = false;
iter_result |= SimplifyBitArith(builder);
iter_result |= EliminateConversions(builder);
iter_result |= SimplifyAssignments(builder);
result |= iter_result;
} while (iter_result);
return true;
}
// simplifications that apply to both or and xor
bool SimplificationPass::CheckOrXorZero(hir::Instr* i) {
auto [constant_value, variable_value] = i->BinaryValueArrangeAsConstAndVar();
if (constant_value && constant_value->IsConstantZero()) {
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(variable_value);
return true;
}
return false;
}
static bool IsScalarBasicCmp(Opcode op) {
/*
OPCODE_COMPARE_EQ,
OPCODE_COMPARE_NE,
OPCODE_COMPARE_SLT,
OPCODE_COMPARE_SLE,
OPCODE_COMPARE_SGT,
OPCODE_COMPARE_SGE,
OPCODE_COMPARE_ULT,
OPCODE_COMPARE_ULE,
OPCODE_COMPARE_UGT,
OPCODE_COMPARE_UGE,
*/
return op >= OPCODE_COMPARE_EQ && op <= OPCODE_COMPARE_UGE;
}
static bool SameValueOrEqualConstant(hir::Value* x, hir::Value* y) {
if (x == y) return true;
if (x->IsConstant() && y->IsConstant()) {
return x->AsUint64() == y->AsUint64();
}
return false;
}
static bool CompareDefsHaveSameOpnds(hir::Value* cmp1, hir::Value* cmp2,
hir::Value** out_cmped_l,
hir::Value** out_cmped_r, Opcode* out_l_op,
Opcode* out_r_op) {
auto df1 = cmp1->def;
auto df2 = cmp2->def;
if (!df1 || !df2) return false;
if (df1->src1.value != df2->src1.value) return false;
Opcode lop = df1->opcode->num, rop = df2->opcode->num;
if (!IsScalarBasicCmp(lop) || !IsScalarBasicCmp(rop)) return false;
if (!SameValueOrEqualConstant(df1->src2.value, df2->src2.value)) {
return false;
}
*out_cmped_l = df1->src1.value;
*out_cmped_r = df1->src2.value;
*out_l_op = lop;
*out_r_op = rop;
return true;
}
bool SimplificationPass::CheckOr(hir::Instr* i, hir::HIRBuilder* builder) {
if (CheckOrXorZero(i)) return true;
if (i->src1.value == i->src2.value) {
auto old1 = i->src1.value;
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(old1);
return true;
}
if (i->dest->type == INT8_TYPE) {
Opcode l_op, r_op;
Value *cmpl, *cmpr;
if (!CompareDefsHaveSameOpnds(i->src1.value, i->src2.value, &cmpl, &cmpr,
&l_op, &r_op)) {
return false;
}
auto have_both_ops = [l_op, r_op](Opcode expect1, Opcode expect2) {
return (l_op == expect1 || r_op == expect1) &&
(l_op == expect2 || r_op == expect2);
};
if (have_both_ops(OPCODE_COMPARE_EQ, OPCODE_COMPARE_NE)) {
// both equal and not equal means always true
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(builder->LoadConstantInt8(1));
return true;
}
const OpcodeInfo* new_cmpop = nullptr;
if (have_both_ops(OPCODE_COMPARE_EQ, OPCODE_COMPARE_SLT)) {
new_cmpop = &OPCODE_COMPARE_SLE_info;
} else if (have_both_ops(OPCODE_COMPARE_EQ, OPCODE_COMPARE_SGT)) {
new_cmpop = &OPCODE_COMPARE_SGE_info;
} else if (have_both_ops(OPCODE_COMPARE_EQ, OPCODE_COMPARE_ULT)) {
new_cmpop = &OPCODE_COMPARE_ULE_info;
} else if (have_both_ops(OPCODE_COMPARE_EQ, OPCODE_COMPARE_UGT)) {
new_cmpop = &OPCODE_COMPARE_UGE_info;
}
// todo: also check for pointless compares
if (new_cmpop != nullptr) {
i->Replace(new_cmpop, 0);
i->set_src1(cmpl);
i->set_src2(cmpr);
return true;
}
}
return false;
}
bool SimplificationPass::CheckBooleanXor1(hir::Instr* i,
hir::HIRBuilder* builder,
hir::Value* xored) {
unsigned tunflags = MOVTUNNEL_ASSIGNS | MOVTUNNEL_MOVZX;
Instr* xordef = xored->GetDefTunnelMovs(&tunflags);
if (!xordef) {
return false;
}
Opcode xorop = xordef->opcode->num;
bool need_zx = (tunflags & MOVTUNNEL_MOVZX) != 0;
Value* new_value = nullptr;
if (xorop == OPCODE_IS_FALSE) {
new_value = builder->IsTrue(xordef->src1.value);
} else if (xorop == OPCODE_IS_TRUE) {
new_value = builder->IsFalse(xordef->src1.value);
} else if (xorop == OPCODE_COMPARE_EQ) {
new_value = builder->CompareNE(xordef->src1.value, xordef->src2.value);
} else if (xorop == OPCODE_COMPARE_NE) {
new_value = builder->CompareEQ(xordef->src1.value, xordef->src2.value);
} // todo: other conds
if (!new_value) {
return false;
}
new_value->def->MoveBefore(i);
i->Replace(need_zx ? &OPCODE_ZERO_EXTEND_info : &OPCODE_ASSIGN_info, 0);
i->set_src1(new_value);
return true;
}
bool SimplificationPass::CheckXorOfTwoBools(hir::Instr* i,
hir::HIRBuilder* builder,
hir::Value* b1, hir::Value* b2) {
// todo: implement
return false;
}
bool SimplificationPass::CheckXor(hir::Instr* i, hir::HIRBuilder* builder) {
if (CheckOrXorZero(i)) {
return true;
} else {
Value* src1 = i->src1.value;
Value* src2 = i->src2.value;
if (SameValueOrEqualConstant(src1, src2)) {
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(builder->LoadZero(i->dest->type));
return true;
}
auto [constant_value, variable_value] =
i->BinaryValueArrangeAsConstAndVar();
ScalarNZM nzm1 = GetScalarNZM(src1);
ScalarNZM nzm2 = GetScalarNZM(src2);
if ((nzm1 & nzm2) ==
0) { // no bits of the two sources overlap, this ought to be an OR
// cs:optimizing
/* i->Replace(&OPCODE_OR_info, 0);
i->set_src1(src1);
i->set_src2(src2);*/
i->opcode = &OPCODE_OR_info;
return true;
}
if (nzm1 == 1ULL && nzm2 == 1ULL) {
if (constant_value) {
return CheckBooleanXor1(i, builder, variable_value);
} else {
return CheckXorOfTwoBools(i, builder, src1, src2);
}
}
uint64_t type_mask = GetScalarTypeMask(i->dest->type);
if (!constant_value) return false;
if (constant_value->AsUint64() == type_mask) {
i->Replace(&OPCODE_NOT_info, 0);
i->set_src1(variable_value);
return true;
}
}
return false;
}
bool SimplificationPass::Is1BitOpcode(hir::Opcode def_opcode) {
return def_opcode >= OPCODE_IS_TRUE && def_opcode <= OPCODE_DID_SATURATE;
}
inline static uint64_t RotateOfSize(ScalarNZM nzm, unsigned rotation,
TypeName typ) {
unsigned mask = (static_cast<unsigned int>(GetTypeSize(typ)) * CHAR_BIT) - 1;
rotation &= mask;
return (nzm << rotation) |
(nzm >>
((static_cast<unsigned int>(-static_cast<int>(rotation))) & mask));
}
static inline uint64_t BswapOfSize(ScalarNZM nzm, TypeName typ) {
// should work for all sizes, including uint8
return xe::byte_swap<uint64_t>(nzm) >> (64 - (GetTypeSize(typ) * CHAR_BIT));
}
ScalarNZM SimplificationPass::GetScalarNZM(const hir::Value* value,
unsigned depth) {
redo:
uint64_t typemask = GetScalarTypeMask(value->type);
if (value->IsConstant()) {
return value->constant.u64 & typemask;
}
if (depth >= MAX_NZM_DEPTH) {
return typemask;
}
const hir::Instr* def = value->def;
if (!def) {
return typemask;
}
const hir::Value *def_src1 = def->src1.value, *def_src2 = def->src2.value,
*def_src3 = def->src3.value;
const hir::Opcode def_opcode = def->opcode->num;
auto RecurseScalarNZM = [depth](const hir::Value* value) {
return GetScalarNZM(value, depth + 1);
};
switch (def_opcode) {
case OPCODE_SHL: {
const hir::Value* shifted = def_src1;
const hir::Value* shiftby = def_src2;
// todo: nzm shift
if (shiftby->IsConstant()) {
ScalarNZM shifted_nzm = RecurseScalarNZM(shifted);
return (shifted_nzm << shiftby->constant.u8) & typemask;
}
break;
}
case OPCODE_SHR: {
const hir::Value* shifted = def_src1;
const hir::Value* shiftby = def_src2;
// todo: nzm shift
if (shiftby->IsConstant()) {
ScalarNZM shifted_nzm = RecurseScalarNZM(shifted);
return shifted_nzm >> shiftby->constant.u8;
}
break;
}
case OPCODE_SHA: {
uint64_t signmask = GetScalarSignbitMask(value->type);
const hir::Value* shifted = def_src1;
const hir::Value* shiftby = def_src2;
if (shiftby->IsConstant()) {
ScalarNZM shifted_nzm = RecurseScalarNZM(shifted);
uint32_t shift_amnt = shiftby->constant.u8;
if ((shifted_nzm & signmask) == 0) {
return shifted_nzm >> shift_amnt;
} else {
uint64_t sign_shifted_in = typemask >> shift_amnt; // vacate
sign_shifted_in = (~sign_shifted_in) &
typemask; // generate duplication of the signbit
return ((shifted_nzm >> shift_amnt) | sign_shifted_in) &
typemask; // todo: mask with typemask might not be needed
}
}
break;
}
case OPCODE_ROTATE_LEFT: {
const hir::Value* shifted = def_src1;
const hir::Value* shiftby = def_src2;
// todo: nzm shift
if (shiftby->IsConstant()) {
ScalarNZM rotated_nzm = RecurseScalarNZM(shifted);
return RotateOfSize(rotated_nzm, shiftby->constant.u8, value->type);
}
break;
}
case OPCODE_OR:
case OPCODE_XOR:
return RecurseScalarNZM(def_src1) | RecurseScalarNZM(def_src2);
case OPCODE_NOT:
return typemask; //~GetScalarNZM(def->src1.value);
case OPCODE_ASSIGN:
value = def_src1;
goto redo;
case OPCODE_BYTE_SWAP:
return BswapOfSize(RecurseScalarNZM(def_src1), value->type);
case OPCODE_ZERO_EXTEND:
value = def_src1;
goto redo;
case OPCODE_TRUNCATE:
return RecurseScalarNZM(def_src1) & typemask;
case OPCODE_AND:
return RecurseScalarNZM(def_src1) & RecurseScalarNZM(def_src2);
case OPCODE_MIN:
/*
* for min:
the nzm will be that of the narrowest operand, because if one value
is capable of being much larger than the other it can never actually
reach a value that is outside the range of the other values nzm,
because that would make it not the minimum of the two
ahh, actually, we have to be careful about constants then.... for
now, just return or
*/
case OPCODE_MAX:
return RecurseScalarNZM(def_src1) | RecurseScalarNZM(def_src2);
case OPCODE_SELECT:
return RecurseScalarNZM(def_src2) | RecurseScalarNZM(def_src3);
case OPCODE_CNTLZ: {
size_t type_bit_size = (CHAR_BIT * GetTypeSize(def_src1->type));
// if 0, ==type_bit_size
return type_bit_size | ((type_bit_size)-1);
}
case OPCODE_SIGN_EXTEND: {
ScalarNZM input_nzm = RecurseScalarNZM(def_src1);
if ((input_nzm & GetScalarSignbitMask(def_src1->type)) == 0) {
return input_nzm;
// return input_nzm;
} else {
uint64_t from_mask = GetScalarTypeMask(def_src1->type);
uint64_t to_mask = typemask;
to_mask &= ~from_mask;
to_mask |= input_nzm;
return to_mask & typemask;
}
break;
}
}
if (Is1BitOpcode(def_opcode)) {
return 1ULL;
}
return typemask;
}
bool SimplificationPass::TransformANDROLORSHLSeq(
hir::Instr* i, // the final instr in the rlwinm sequence (the AND)
hir::HIRBuilder* builder,
ScalarNZM input_nzm, // the NZM of the value being rot'ed/masked (input)
uint64_t mask, // mask after rotation
uint64_t rotation, // rot amount prior to masking
hir::Instr* input_def, // the defining instr of the input var
hir::Value* input // the input variable to the rlwinm
) {
/*
Usually you perform a rotation at the source level by decomposing it into
two shifts, one left and one right, and an or of those two. your compiler
then recognizes that you are doing a shift and generates the machine
instruction for a 32 bit int, you would do (value<<LEFT_ROT_AMNT) |
(value>>( (-LEFT_ROT_AMNT)&31 )); in order to reason about the bits actually
used by our rotate and mask operation we decompose our rotate into these two
shifts and check them against our mask to determine whether the masking is
useless, or if it only covers bits from the left or right shift if it is a
right shift, and the mask exactly covers all bits that were not vacated
right, we eliminate the mask
*/
uint32_t right_shift_for_rotate =
(static_cast<uint32_t>(-static_cast<int32_t>(rotation)) & 31);
uint32_t left_shift_for_rotate = (rotation & 31);
uint64_t rotation_shiftmask_right =
static_cast<uint64_t>((~0U) >> right_shift_for_rotate);
uint64_t rotation_shiftmask_left =
static_cast<uint64_t>((~0U) << left_shift_for_rotate);
if (rotation_shiftmask_left == mask) {
Value* rotation_fix = builder->Shl(input, left_shift_for_rotate);
rotation_fix->def->MoveBefore(i);
i->Replace(&OPCODE_AND_info, 0);
i->set_src1(rotation_fix);
i->set_src2(builder->LoadConstantUint64(0xFFFFFFFFULL));
return true;
} else if (rotation_shiftmask_right == mask) {
// no need to AND at all, the bits were vacated
i->Replace(&OPCODE_SHR_info, 0);
i->set_src1(input);
i->set_src2(
builder->LoadConstantInt8(static_cast<int8_t>(right_shift_for_rotate)));
return true;
} else if ((rotation_shiftmask_right & mask) ==
mask) { // only uses bits from right
Value* rotation_fix = builder->Shr(input, right_shift_for_rotate);
rotation_fix->def->MoveBefore(i);
i->set_src1(rotation_fix);
return true;
} else if ((rotation_shiftmask_left & mask) == mask) {
Value* rotation_fix = builder->Shl(input, left_shift_for_rotate);
rotation_fix->def->MoveBefore(i);
i->set_src1(rotation_fix);
return true;
}
return false;
}
/*
todo: investigate breaking this down into several different optimization
rules. one for rotates that can be made into shifts one for 64 bit rotates
that can become 32 bit and probably one or two other rules would need to be
added for this)
*/
bool SimplificationPass::TryHandleANDROLORSHLSeq(hir::Instr* i,
hir::HIRBuilder* builder) {
// todo: could probably make this work for all types without much trouble, but
// not sure if it would benefit any of them
if (i->dest->type != INT64_TYPE) {
return false;
}
auto [defined_by_rotate, mask] =
i->BinaryValueArrangeByDefOpAndConstant(&OPCODE_ROTATE_LEFT_info);
if (!defined_by_rotate) {
return false;
}
uint64_t mask_value = mask->AsUint64();
if (static_cast<uint64_t>(static_cast<uint32_t>(mask_value)) != mask_value) {
return false;
}
Instr* rotinsn = defined_by_rotate->def;
Value* rotated_variable = rotinsn->src1.value;
Value* rotation_amount = rotinsn->src2.value;
if (!rotation_amount->IsConstant()) {
return false;
}
uint32_t rotation_value = rotation_amount->constant.u8;
if (rotation_value > 31) {
return false;
}
if (rotated_variable->IsConstant()) {
return false;
}
Instr* rotated_variable_def = rotated_variable->GetDefSkipAssigns();
if (!rotated_variable_def) {
return false;
}
if (rotated_variable_def->opcode != &OPCODE_OR_info) {
return false;
}
// now we have our or that concatenates the var with itself, find the shift by
// 32
auto [shl_insn_defined, expected_input] =
rotated_variable_def->BinaryValueArrangeByPredicateExclusive(
[](Value* shl_check) {
Instr* shl_def = shl_check->GetDefSkipAssigns();
if (!shl_def) {
return false;
}
Value* shl_left = shl_def->src1.value;
Value* shl_right = shl_def->src2.value;
if (!shl_left || !shl_right) {
return false;
}
if (shl_def->opcode != &OPCODE_SHL_info) {
return false;
}
return !shl_left->IsConstant() && shl_right->IsConstant() &&
shl_right->constant.u8 == 32;
});
if (!shl_insn_defined) {
return false;
}
Instr* shl_insn = shl_insn_defined->GetDefSkipAssigns();
Instr* shl_left_definition = shl_insn->src1.value->GetDefSkipAssigns();
Instr* expected_input_definition = expected_input->GetDefSkipAssigns();
// we expect (x << 32) | x
if (shl_left_definition != expected_input_definition) {
return false;
}
// next check the nzm for the input, if the high 32 bits are clear we know we
// can optimize. the input ought to have been cleared by an AND with ~0U or
// truncate zeroextend if its a rlwinx
// todo: it shouldnt have to search very far for the nzm, we dont need the
// whole depth of the use-def chain traversed, add a maxdef var? or do
// additional opts here depending on the nzm
ScalarNZM input_nzm = GetScalarNZM(expected_input);
if (static_cast<uint64_t>(static_cast<uint32_t>(input_nzm)) != input_nzm) {
return false; // bits from the or may overlap!!
}
return TransformANDROLORSHLSeq(i, builder, input_nzm, mask_value,
rotation_value, expected_input_definition,
expected_input);
}
bool SimplificationPass::CheckAnd(hir::Instr* i, hir::HIRBuilder* builder) {
retry_and_simplification:
auto [constant_value, variable_value] = i->BinaryValueArrangeAsConstAndVar();
if (!constant_value) {
// added this for srawi
ScalarNZM nzml = GetScalarNZM(i->src1.value);
ScalarNZM nzmr = GetScalarNZM(i->src2.value);
if ((nzml & nzmr) == 0) {
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(builder->LoadZero(i->dest->type));
return true;
}
return false;
}
if (TryHandleANDROLORSHLSeq(i, builder)) {
return true;
}
// todo: check if masking with mask that covers all of zero extension source
uint64_t type_mask = GetScalarTypeMask(i->dest->type);
ScalarNZM nzm = GetScalarNZM(variable_value);
// if masking with entire width, pointless instruction so become an assign
// chrispy: changed this to use the nzm instead, this optimizes away many and
// instructions
// chrispy: changed this again. detecting if nzm is a subset of and mask, if
// so eliminate ex: (bool value) & 0xff = (bool value). the nzm is not equal
// to the mask, but it is a subset so can be elimed
if ((constant_value->AsUint64() & nzm) == nzm) {
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(variable_value);
return true;
}
auto variable_def = variable_value->def;
if (variable_def) {
auto true_variable_def = variable_def->GetDestDefSkipAssigns();
if (true_variable_def) {
if (true_variable_def->opcode == &OPCODE_AND_info) {
auto [variable_def_constant, variable_def_variable] =
true_variable_def->BinaryValueArrangeAsConstAndVar();
if (variable_def_constant) {
// todo: check if masked with mask that was a subset of the current
// one and elim if so
if (variable_def_constant->AsUint64() == constant_value->AsUint64()) {
// we already masked the input with the same mask
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(variable_value);
return true;
}
}
} else if (true_variable_def->opcode == &OPCODE_OR_info) {
Value* or_left = true_variable_def->src1.value;
Value* or_right = true_variable_def->src2.value;
ScalarNZM left_nzm = GetScalarNZM(or_left);
// use the other or input instead of the or output
if ((constant_value->AsUint64() & left_nzm) == 0) {
i->Replace(&OPCODE_AND_info, 0);
i->set_src1(or_right);
i->set_src2(constant_value);
return true;
}
ScalarNZM right_nzm = GetScalarNZM(or_right);
if ((constant_value->AsUint64() & right_nzm) == 0) {
i->Replace(&OPCODE_AND_info, 0);
i->set_src1(or_left);
i->set_src2(constant_value);
return true;
}
} else if (true_variable_def->opcode == &OPCODE_ROTATE_LEFT_info) {
if (true_variable_def->src2.value->IsConstant()) {
if (((type_mask << true_variable_def->src2.value->AsUint64()) &
type_mask) ==
constant_value->AsUint64()) { // rotated bits are unused, convert
// to shift if we are the only use
if (true_variable_def->dest->use_head->next == nullptr) {
// one use, convert to shift
true_variable_def->opcode = &OPCODE_SHL_info;
goto retry_and_simplification;
}
}
}
}
}
}
return false;
}
bool SimplificationPass::CheckAdd(hir::Instr* i, hir::HIRBuilder* builder) {
Value* src1 = i->src1.value;
Value* src2 = i->src2.value;
ScalarNZM nzm1 = GetScalarNZM(src1);
ScalarNZM nzm2 = GetScalarNZM(src2);
if ((nzm1 & nzm2) == 0) { // no bits overlap, there will never be a carry
// from any bits to any others, make this an OR
/* i->Replace(&OPCODE_OR_info, 0);
i->set_src1(src1);
i->set_src2(src2);*/
i->opcode = &OPCODE_OR_info;
return true;
}
auto [definition, added_constant] =
i->BinaryValueArrangeByDefOpAndConstant(&OPCODE_NOT_info);
if (!definition) {
auto [added_constant_neg, added_var_neg] =
i->BinaryValueArrangeAsConstAndVar();
if (!added_constant_neg) return false;
if (added_constant_neg->AsUint64() &
GetScalarSignbitMask(added_constant_neg->type)) {
// adding a value that has its signbit set!
Value* negconst = builder->CloneValue(added_constant_neg);
negconst->Neg();
i->Replace(&OPCODE_SUB_info, 0);
i->set_src1(added_var_neg);
i->set_src2(negconst);
return true;
}
return false;
}
if (added_constant->AsUint64() == 1) {
i->Replace(&OPCODE_NEG_info, 0);
i->set_src1(definition->def->src1.value);
return true;
}
return false;
}
bool SimplificationPass::CheckSelect(hir::Instr* i, hir::HIRBuilder* builder,
hir::Value* condition, hir::Value* iftrue,
hir::Value* iffalse) {
return false;
}
bool SimplificationPass::CheckSelect(hir::Instr* i, hir::HIRBuilder* builder) {
Value* src1 = i->src1.value;
Value* src2 = i->src2.value;
Value* src3 = i->src3.value;
return CheckSelect(i, builder, src1, src2, src3);
}
bool SimplificationPass::CheckScalarConstCmp(hir::Instr* i,
hir::HIRBuilder* builder) {
if (!IsScalarIntegralType(i->src1.value->type)) return false;
auto [constant_value, variable] = i->BinaryValueArrangeAsConstAndVar();
if (!constant_value) {
return false;
}
ScalarNZM nzm_for_var = GetScalarNZM(variable);
Opcode cmpop = i->opcode->num;
uint64_t constant_unpacked = constant_value->AsUint64();
uint64_t signbit_for_var = GetScalarSignbitMask(variable->type);
bool signbit_definitely_0 = (nzm_for_var & signbit_for_var) == 0;
Instr* var_definition = variable->def;
Opcode def_opcode = OPCODE_NOP;
if (var_definition) {
var_definition = var_definition->GetDestDefSkipAssigns();
def_opcode = var_definition->opcode->num;
}
// x == 0 -> !x
if (cmpop == OPCODE_COMPARE_EQ && constant_unpacked == 0) {
i->Replace(&OPCODE_IS_FALSE_info, 0);
i->set_src1(variable);
return true;
}
// x != 0 -> !!x
if (cmpop == OPCODE_COMPARE_NE && constant_unpacked == 0) {
i->Replace(&OPCODE_IS_TRUE_info, 0);
i->set_src1(variable);
return true;
}
if (cmpop == OPCODE_COMPARE_ULE &&
constant_unpacked ==
0) { // less than or equal to zero = (== 0) = IS_FALSE
i->Replace(&OPCODE_IS_FALSE_info, 0);
i->set_src1(variable);
return true;
}
// todo: OPCODE_COMPARE_NE too?
if (cmpop == OPCODE_COMPARE_EQ &&
def_opcode == OPCODE_NOT) { // i see this a lot around addic insns
Value* cloned = builder->CloneValue(constant_value);
cloned->Not();
i->Replace(&OPCODE_COMPARE_EQ_info, 0);
i->set_src1(var_definition->src1.value);
i->set_src2(cloned);
return true;
}
if (constant_value != i->src2.value) {
return false;
}
if (cmpop == OPCODE_COMPARE_ULT &&
constant_unpacked == 1) { // unsigned lt 1 means == 0
i->Replace(&OPCODE_IS_FALSE_info, 0);
i->set_src1(variable);
return true;
}
if (cmpop == OPCODE_COMPARE_UGT &&
constant_unpacked == 0) { // unsigned gt 1 means != 0
i->Replace(&OPCODE_IS_TRUE_info, 0);
i->set_src1(variable);
return true;
}
if (cmpop == OPCODE_COMPARE_ULT &&
constant_unpacked == 0) { // impossible to be unsigned lt 0
impossible_compare:
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(builder->LoadZeroInt8());
return true;
} else if (cmpop == OPCODE_COMPARE_UGT &&
nzm_for_var < constant_unpacked) { // impossible!
goto impossible_compare;
} else if (cmpop == OPCODE_COMPARE_SLT && signbit_definitely_0 &&
constant_unpacked == 0) {
goto impossible_compare; // cant be less than 0 because signbit cannot be
// set
} else if (cmpop == OPCODE_COMPARE_SGT && signbit_definitely_0 &&
constant_unpacked == 0) {
// signbit cant be set, and checking if gt 0, so actually checking != 0
i->Replace(&OPCODE_IS_TRUE_info, 0);
i->set_src1(variable);
return true;
}
// value can only be one of two values, 0 or the bit set
if (xe::bit_count(nzm_for_var) != 1) {
return false;
}
if (constant_value->AsUint64() == nzm_for_var) {
const OpcodeInfo* repl = nullptr;
Value* constant_replacement = nullptr;
if (cmpop == OPCODE_COMPARE_EQ || cmpop == OPCODE_COMPARE_UGE) {
repl = &OPCODE_IS_TRUE_info;
} else if (cmpop == OPCODE_COMPARE_NE || cmpop == OPCODE_COMPARE_ULT) {
repl = &OPCODE_IS_FALSE_info;
} else if (cmpop == OPCODE_COMPARE_UGT) {
// impossible, cannot be greater than mask
/* i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(builder->LoadZeroInt8());
return true;
*/
constant_replacement = builder->LoadZeroInt8();
} else if (cmpop == OPCODE_COMPARE_ULE) { // less than or equal to mask =
// always true
constant_replacement = builder->LoadConstantInt8(1);
}
if (repl) {
i->Replace(repl, 0);
i->set_src1(variable);
return true;
}
if (constant_replacement) {
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(constant_replacement);
return true;
}
}
return false;
}
bool SimplificationPass::CheckIsTrueIsFalse(hir::Instr* i,
hir::HIRBuilder* builder) {
bool istrue = i->opcode == &OPCODE_IS_TRUE_info;
bool isfalse = i->opcode == &OPCODE_IS_FALSE_info;
Value* input = i->src1.value;
TypeName input_type = input->type;
if (!IsScalarIntegralType(input_type)) {
return false;
}
ScalarNZM input_nzm = GetScalarNZM(input);
if (istrue &&
input_nzm == 1) { // doing istrue on a value thats already a bool bitwise
if (input_type == INT8_TYPE) {
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(input);
} else {
i->Replace(&OPCODE_TRUNCATE_info, 0);
i->set_src1(input);
}
return true;
} else if (isfalse && input_nzm == 1) {
if (input_type == INT8_TYPE) {
i->Replace(&OPCODE_XOR_info, 0);
i->set_src1(input);
i->set_src2(builder->LoadConstantInt8(1));
return true;
} else {
Value* truncated = builder->Truncate(input, INT8_TYPE);
truncated->def->MoveBefore(i);
i->Replace(&OPCODE_XOR_info, 0);
i->set_src1(truncated);
i->set_src2(builder->LoadConstantInt8(1));
return true;
}
}
/* Instr* input_def = input->def;
if (!input_def) {
return false;
}
input_def = input_def->GetDestDefSkipAssigns();*/
return false;
}
bool SimplificationPass::CheckSHRByConst(hir::Instr* i,
hir::HIRBuilder* builder,
hir::Value* variable,
unsigned int shift) {
if (shift >= 3 && shift <= 6) {
// is possible shift of lzcnt res, do some tunneling
unsigned int tflags = MOVTUNNEL_ASSIGNS | MOVTUNNEL_MOVZX |
MOVTUNNEL_TRUNCATE | MOVTUNNEL_MOVSX |
MOVTUNNEL_AND32FF;
Instr* vardef = variable->def;
hir::Instr* var_def = variable->GetDefTunnelMovs(&tflags);
if (var_def && var_def->opcode == &OPCODE_CNTLZ_info) {
Value* lz_input = var_def->src1.value;
TypeName type_of_lz_input = lz_input->type;
size_t shift_for_zero =
xe::log2_floor(GetTypeSize(type_of_lz_input) * CHAR_BIT);
if (shift == shift_for_zero) {
// we ought to be OPCODE_IS_FALSE!
/*
explanation: if an input to lzcnt is zero, the result will be the
bit size of the input type, which is always a power of two any
nonzero result will be less than the bit size so you can test for
zero by doing, for instance with a 32 bit value, lzcnt32(input) >> 5
this is a very common way of testing for zero without branching on
ppc, and the xb360 ppc compiler used it a lot we optimize this away
for simplicity and to enable further optimizations, but actually this
is also quite fast on modern x86 processors as well, for instance on
zen 2 the rcp through of lzcnt is 0.25, meaning four can be executed
in one cycle
*/
if (variable->type != INT8_TYPE) {
Value* isfalsetest = builder->IsFalse(lz_input);
isfalsetest->def->MoveBefore(i);
i->Replace(&OPCODE_ZERO_EXTEND_info, 0);
i->set_src1(isfalsetest);
} else {
i->Replace(&OPCODE_IS_FALSE_info, 0);
i->set_src1(lz_input);
}
return true;
}
}
}
return false;
}
bool SimplificationPass::CheckSHR(hir::Instr* i, hir::HIRBuilder* builder) {
Value* shr_lhs = i->src1.value;
Value* shr_rhs = i->src2.value;
if (!shr_lhs || !shr_rhs) return false;
if (shr_rhs->IsConstant()) {
return CheckSHRByConst(i, builder, shr_lhs, shr_rhs->AsUint32());
}
return false;
}
bool SimplificationPass::CheckSAR(hir::Instr* i, hir::HIRBuilder* builder) {
Value* l = i->src1.value;
Value* r = i->src2.value;
ScalarNZM l_nzm = GetScalarNZM(l);
uint64_t signbit_mask = GetScalarSignbitMask(l->type);
size_t typesize = GetTypeSize(l->type);
/*
todo: folding this requires the mask of constant bits
if (r->IsConstant()) {
uint32_t const_r = r->AsUint32();
if (const_r == (typesize * CHAR_BIT) - 1) { //the shift is being done to
fill the result with the signbit of the input.
}
}*/
if ((l_nzm & signbit_mask) == 0) { // signbit will never be set, might as
// well be an SHR. (this does happen)
i->opcode = &OPCODE_SHR_info;
return true;
}
return false;
}
bool SimplificationPass::SimplifyBitArith(hir::HIRBuilder* builder) {
bool result = false;
auto block = builder->first_block();
while (block) {
auto i = block->instr_head;
while (i) {
// vector types use the same opcodes as scalar ones for AND/OR/XOR! we
// don't handle these in our simplifications, so skip
if (i->dest && IsScalarIntegralType(i->dest->type)) {
Opcode iop = i->opcode->num;
if (iop == OPCODE_OR) {
result |= CheckOr(i, builder);
} else if (iop == OPCODE_XOR) {
result |= CheckXor(i, builder);
} else if (iop == OPCODE_AND) {
result |= CheckAnd(i, builder);
} else if (iop == OPCODE_ADD) {
result |= CheckAdd(i, builder);
} else if (IsScalarBasicCmp(iop)) {
result |= CheckScalarConstCmp(i, builder);
} else if (iop == OPCODE_IS_FALSE || iop == OPCODE_IS_TRUE) {
result |= CheckIsTrueIsFalse(i, builder);
} else if (iop == OPCODE_SHR) {
result |= CheckSHR(i, builder);
} else if (iop == OPCODE_SHA) {
result |= CheckSAR(i, builder);
}
}
i = i->next;
}
block = block->next;
}
return result;
}
bool SimplificationPass::EliminateConversions(HIRBuilder* builder) {
// First, we check for truncates/extensions that can be skipped.
// This generates some assignments which then the second step will clean up.
// Both zero/sign extends can be skipped:
// v1.i64 = zero/sign_extend v0.i32
// v2.i32 = truncate v1.i64
// becomes:
// v1.i64 = zero/sign_extend v0.i32 (may be dead code removed later)
// v2.i32 = v0.i32
bool result = false;
auto block = builder->first_block();
while (block) {
auto i = block->instr_head;
while (i) {
// To make things easier we check in reverse (source of truncate/extend
// back to definition).
if (i->opcode == &OPCODE_TRUNCATE_info) {
// Matches zero/sign_extend + truncate.
result |= CheckTruncate(i);
} else if (i->opcode == &OPCODE_BYTE_SWAP_info) {
// Matches byte swap + byte swap.
// This is pretty rare within the same basic block, but is in the
// memcpy hot path and (probably) worth it. Maybe.
result |= CheckByteSwap(i);
}
i = i->next;
}
block = block->next;
}
return result;
}
bool SimplificationPass::CheckTruncate(Instr* i) {
// Walk backward up src's chain looking for an extend. We may have
// assigns, so skip those.
auto src = i->src1.value;
auto def = src->def;
while (def && def->opcode == &OPCODE_ASSIGN_info) {
// Skip asignments.
def = def->src1.value->def;
}
if (def) {
if (def->opcode == &OPCODE_SIGN_EXTEND_info) {
// Value comes from a sign extend.
if (def->src1.value->type == i->dest->type) {
// Types match, use original by turning this into an assign.
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(def->src1.value);
return true;
}
} else if (def->opcode == &OPCODE_ZERO_EXTEND_info) {
// Value comes from a zero extend.
if (def->src1.value->type == i->dest->type) {
// Types match, use original by turning this into an assign.
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(def->src1.value);
return true;
}
}
}
return false;
}
bool SimplificationPass::CheckByteSwap(Instr* i) {
// Walk backward up src's chain looking for a byte swap. We may have
// assigns, so skip those.
auto src = i->src1.value;
auto def = src->def;
while (def && def->opcode == &OPCODE_ASSIGN_info) {
// Skip asignments.
def = def->src1.value->def;
}
if (def && def->opcode == &OPCODE_BYTE_SWAP_info) {
// Value comes from a byte swap.
if (def->src1.value->type == i->dest->type) {
// Types match, use original by turning this into an assign.
i->Replace(&OPCODE_ASSIGN_info, 0);
i->set_src1(def->src1.value);
return true;
}
}
return false;
}
bool SimplificationPass::SimplifyAssignments(HIRBuilder* builder) {
// Run over the instructions and rename assigned variables:
// v1 = v0
// v2 = v1
// v3 = add v0, v2
// becomes:
// v1 = v0
// v2 = v0
// v3 = add v0, v0
// This could be run several times, as it could make other passes faster
// to compute (for example, ConstantPropagation). DCE will take care of
// the useless assigns.
//
// We do this by walking each instruction. For each value op we
// look at its def instr to see if it's an assign - if so, we use the src
// of that instr. Because we may have chains, we do this recursively until
// we find a non-assign def.
bool result = false;
auto block = builder->first_block();
while (block) {
auto i = block->instr_head;
while (i) {
i->VisitValueOperands([&result, i, this](Value* value, uint32_t idx) {
bool modified = false;
i->set_srcN(CheckValue(value, modified), idx);
result |= modified;
});
i = i->next;
}
block = block->next;
}
return result;
}
Value* SimplificationPass::CheckValue(Value* value, bool& result) {
auto def = value->def;
if (def && def->opcode == &OPCODE_ASSIGN_info) {
// Value comes from an assignment - recursively find if it comes from
// another assignment. It probably doesn't, if we already replaced it.
auto replacement = def->src1.value;
while (true) {
def = replacement->def;
if (!def || def->opcode != &OPCODE_ASSIGN_info) {
break;
}
replacement = def->src1.value;
}
result = true;
return replacement;
}
result = false;
return value;
}
} // namespace passes
} // namespace compiler
} // namespace cpu
} // namespace xe