Merge pull request #404 from lioncash/fold

constant_propagation_pass: Extend folding of sign-extension/zero-extension opcodes
This commit is contained in:
Merry 2018-10-07 16:40:11 +01:00 committed by MerryMage
commit 546cfbc0ee

View file

@ -113,6 +113,42 @@ void FoldOR(IR::Inst& inst, bool is_32_bit) {
inst.ReplaceUsesWith(lhs); inst.ReplaceUsesWith(lhs);
} }
} }
void FoldSignExtendXToWord(IR::Inst& inst) {
if (!inst.AreAllArgsImmediates()) {
return;
}
const s64 value = inst.GetArg(0).GetImmediateAsS64();
inst.ReplaceUsesWith(IR::Value{static_cast<u32>(value)});
}
void FoldSignExtendXToLong(IR::Inst& inst) {
if (!inst.AreAllArgsImmediates()) {
return;
}
const s64 value = inst.GetArg(0).GetImmediateAsS64();
inst.ReplaceUsesWith(IR::Value{static_cast<u64>(value)});
}
void FoldZeroExtendXToWord(IR::Inst& inst) {
if (!inst.AreAllArgsImmediates()) {
return;
}
const u64 value = inst.GetArg(0).GetImmediateAsU64();
inst.ReplaceUsesWith(IR::Value{static_cast<u32>(value)});
}
void FoldZeroExtendXToLong(IR::Inst& inst) {
if (!inst.AreAllArgsImmediates()) {
return;
}
const u64 value = inst.GetArg(0).GetImmediateAsU64();
inst.ReplaceUsesWith(IR::Value{value});
}
} // Anonymous namespace } // Anonymous namespace
void ConstantPropagation(IR::Block& block) { void ConstantPropagation(IR::Block& block) {
@ -154,24 +190,24 @@ void ConstantPropagation(IR::Block& block) {
case IR::Opcode::Not64: case IR::Opcode::Not64:
FoldNOT(inst, opcode == IR::Opcode::Not32); FoldNOT(inst, opcode == IR::Opcode::Not32);
break; break;
case IR::Opcode::ZeroExtendByteToWord: { case IR::Opcode::SignExtendByteToWord:
if (!inst.AreAllArgsImmediates()) case IR::Opcode::SignExtendHalfToWord:
break; FoldSignExtendXToWord(inst);
u8 byte = inst.GetArg(0).GetU8();
u32 value = static_cast<u32>(byte);
inst.ReplaceUsesWith(IR::Value{value});
break; break;
} case IR::Opcode::SignExtendByteToLong:
case IR::Opcode::ZeroExtendHalfToWord: { case IR::Opcode::SignExtendHalfToLong:
if (!inst.AreAllArgsImmediates()) case IR::Opcode::SignExtendWordToLong:
break; FoldSignExtendXToLong(inst);
break;
u16 half = inst.GetArg(0).GetU16(); case IR::Opcode::ZeroExtendByteToWord:
u32 value = static_cast<u32>(half); case IR::Opcode::ZeroExtendHalfToWord:
inst.ReplaceUsesWith(IR::Value{value}); FoldZeroExtendXToWord(inst);
break;
case IR::Opcode::ZeroExtendByteToLong:
case IR::Opcode::ZeroExtendHalfToLong:
case IR::Opcode::ZeroExtendWordToLong:
FoldZeroExtendXToLong(inst);
break; break;
}
default: default:
break; break;
} }