VarAction2: Flatten constant operations on constants in more cases

This commit is contained in:
Jonathan G Rennison
2022-05-26 22:31:02 +01:00
parent 2360b3e93e
commit f0a0b2e200

View File

@@ -5684,23 +5684,13 @@ static void NewSpriteGroup(ByteReader *buf)
}
};
if ((prev_inference & VA2AIF_PREV_TERNARY) && adjust.variable == 0x1A && IsEvalAdjustUsableForConstantPropagation(adjust.operation)) {
/* Propagate constant operation back into previous ternary */
DeterministicSpriteGroupAdjust &prev = group->adjusts[group->adjusts.size() - 2];
prev.and_mask = EvaluateDeterministicSpriteGroupAdjust(group->size, adjust, nullptr, prev.and_mask, UINT_MAX);
prev.add_val = EvaluateDeterministicSpriteGroupAdjust(group->size, adjust, nullptr, prev.add_val, UINT_MAX);
auto replace_with_constant_load = [&](uint32 constant) {
group->adjusts.pop_back();
if ((prev_inference & VA2AIF_HAVE_CONSTANT) && constant == current_constant) {
/* Don't create a new constant load for the same constant as was there previously */
inference = prev_inference;
} else if (adjust.type == DSGA_TYPE_NONE && group->adjusts.size() > 1) {
/* Not the first adjustment */
if (adjust.variable != 0x7E) {
if (adjust.and_mask == 0 && IsEvalAdjustWithZeroRemovable(adjust.operation)) {
/* Delete useless zero operations */
group->adjusts.pop_back();
inference = prev_inference;
} else if (adjust.and_mask == 0 && IsEvalAdjustWithZeroAlwaysZero(adjust.operation)) {
/* Operation always returns 0, replace it and any useless prior operations */
group->adjusts.pop_back();
return;
}
while (!group->adjusts.empty()) {
const DeterministicSpriteGroupAdjust &prev = group->adjusts.back();
if (prev.variable != 0x7E && !IsEvalAdjustWithSideEffects(prev.operation)) {
@@ -5715,10 +5705,34 @@ static void NewSpriteGroup(ByteReader *buf)
replacement.variable = 0x1A;
replacement.shift_num = 0;
replacement.type = DSGA_TYPE_NONE;
replacement.and_mask = 0;
replacement.and_mask = constant;
replacement.add_val = 0;
replacement.divmod_val = 0;
inference = VA2AIF_SIGNED_NON_NEGATIVE | VA2AIF_ONE_OR_ZERO | VA2AIF_PREV_MASK_ADJUST;
inference = VA2AIF_PREV_MASK_ADJUST | VA2AIF_HAVE_CONSTANT;
add_inferences_from_mask(constant);
current_constant = constant;
};
if ((prev_inference & VA2AIF_PREV_TERNARY) && adjust.variable == 0x1A && IsEvalAdjustUsableForConstantPropagation(adjust.operation)) {
/* Propagate constant operation back into previous ternary */
DeterministicSpriteGroupAdjust &prev = group->adjusts[group->adjusts.size() - 2];
prev.and_mask = EvaluateDeterministicSpriteGroupAdjust(group->size, adjust, nullptr, prev.and_mask, UINT_MAX);
prev.add_val = EvaluateDeterministicSpriteGroupAdjust(group->size, adjust, nullptr, prev.add_val, UINT_MAX);
group->adjusts.pop_back();
inference = prev_inference;
} else if ((prev_inference & VA2AIF_HAVE_CONSTANT) && adjust.variable == 0x1A && IsEvalAdjustUsableForConstantPropagation(adjust.operation)) {
/* Reduce constant operation on previous constant */
replace_with_constant_load(EvaluateDeterministicSpriteGroupAdjust(group->size, adjust, nullptr, current_constant, UINT_MAX));
} else if (adjust.type == DSGA_TYPE_NONE && group->adjusts.size() > 1) {
/* Not the first adjustment */
if (adjust.variable != 0x7E) {
if (adjust.and_mask == 0 && IsEvalAdjustWithZeroRemovable(adjust.operation)) {
/* Delete useless zero operations */
group->adjusts.pop_back();
inference = prev_inference;
} else if (adjust.and_mask == 0 && IsEvalAdjustWithZeroAlwaysZero(adjust.operation)) {
/* Operation always returns 0, replace it and any useless prior operations */
replace_with_constant_load(0);
} else {
switch (adjust.operation) {
case DSGA_OP_SMIN:
@@ -5902,15 +5916,7 @@ static void NewSpriteGroup(ByteReader *buf)
}
}
if (adjust.variable == 0x1A || adjust.and_mask == 0) {
uint32 val = EvaluateDeterministicSpriteGroupAdjust(group->size, adjust, nullptr, 0, UINT_MAX);
if ((prev_inference & VA2AIF_HAVE_CONSTANT) && current_constant == val) {
/* reloading previous constant, remove */
group->adjusts.pop_back();
inference = prev_inference;
break;
}
inference |= VA2AIF_HAVE_CONSTANT;
current_constant = val;
replace_with_constant_load(EvaluateDeterministicSpriteGroupAdjust(group->size, adjust, nullptr, 0, UINT_MAX));
}
break;
case DSGA_OP_SHR: