ir/value: Add an IsZero() member function to Value's interface

By far, one of the most common things to check for is whether or not a
value is zero, as it typically allows folding away unnecesary
operations (other close contenders that can help with eliding operations  are 1 and -1).

So instead of requiring a check for an immediate and then actually
retrieving the integral value and checking it, we can wrap it within a
function to make it more convenient.
This commit is contained in:
Lioncash 2018-10-04 04:52:44 -04:00 committed by MerryMage
parent 783fc707fa
commit 4a3c064b15
3 changed files with 20 additions and 13 deletions

View file

@ -36,7 +36,7 @@ void FoldAND(IR::Inst& inst, bool is_32_bit) {
} else {
inst.ReplaceUsesWith(IR::Value{result});
}
} else if ((is_lhs_immediate && lhs.GetImmediateAsU64() == 0) || (is_rhs_immediate && rhs.GetImmediateAsU64() == 0)) {
} else if (lhs.IsZero() || rhs.IsZero()) {
if (is_32_bit) {
inst.ReplaceUsesWith(IR::Value{u32{0}});
} else {
@ -59,10 +59,7 @@ void FoldEOR(IR::Inst& inst, bool is_32_bit) {
const auto lhs = inst.GetArg(0);
const auto rhs = inst.GetArg(1);
const bool is_lhs_immediate = lhs.IsImmediate();
const bool is_rhs_immediate = rhs.IsImmediate();
if (is_lhs_immediate && is_rhs_immediate) {
if (lhs.IsImmediate() && rhs.IsImmediate()) {
const u64 result = lhs.GetImmediateAsU64() ^ rhs.GetImmediateAsU64();
if (is_32_bit) {
@ -70,9 +67,9 @@ void FoldEOR(IR::Inst& inst, bool is_32_bit) {
} else {
inst.ReplaceUsesWith(IR::Value{result});
}
} else if (is_lhs_immediate && lhs.GetImmediateAsU64() == 0) {
} else if (lhs.IsZero()) {
inst.ReplaceUsesWith(rhs);
} else if (is_rhs_immediate && rhs.GetImmediateAsU64() == 0) {
} else if (rhs.IsZero()) {
inst.ReplaceUsesWith(lhs);
}
}
@ -102,10 +99,7 @@ void FoldOR(IR::Inst& inst, bool is_32_bit) {
const auto lhs = inst.GetArg(0);
const auto rhs = inst.GetArg(1);
const bool is_lhs_immediate = lhs.IsImmediate();
const bool is_rhs_immediate = rhs.IsImmediate();
if (is_lhs_immediate && is_rhs_immediate) {
if (lhs.IsImmediate() && rhs.IsImmediate()) {
const u64 result = lhs.GetImmediateAsU64() | rhs.GetImmediateAsU64();
if (is_32_bit) {
@ -113,9 +107,9 @@ void FoldOR(IR::Inst& inst, bool is_32_bit) {
} else {
inst.ReplaceUsesWith(IR::Value{result});
}
} else if (is_lhs_immediate && lhs.GetImmediateAsU64() == 0) {
} else if (lhs.IsZero()) {
inst.ReplaceUsesWith(rhs);
} else if (is_rhs_immediate && rhs.GetImmediateAsU64() == 0) {
} else if (rhs.IsZero()) {
inst.ReplaceUsesWith(lhs);
}
}