From 2da2cf9058027039774f39ea9fb7cc2b4dabffa2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 5 Oct 2018 18:08:16 -0400 Subject: [PATCH] constant_propagation_pass: Fold SignExtend{Type}ToWord opcodes if possible --- src/ir_opt/constant_propagation_pass.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ir_opt/constant_propagation_pass.cpp b/src/ir_opt/constant_propagation_pass.cpp index 037f522e..10f1d720 100644 --- a/src/ir_opt/constant_propagation_pass.cpp +++ b/src/ir_opt/constant_propagation_pass.cpp @@ -114,6 +114,15 @@ void FoldOR(IR::Inst& inst, bool is_32_bit) { } } +void FoldSignExtendXToWord(IR::Inst& inst) { + if (!inst.AreAllArgsImmediates()) { + return; + } + + const s64 value = inst.GetArg(0).GetImmediateAsS64(); + inst.ReplaceUsesWith(IR::Value{static_cast(value)}); +} + void FoldZeroExtendXToWord(IR::Inst& inst) { if (!inst.AreAllArgsImmediates()) { return; @@ -172,6 +181,10 @@ void ConstantPropagation(IR::Block& block) { case IR::Opcode::Not64: FoldNOT(inst, opcode == IR::Opcode::Not32); break; + case IR::Opcode::SignExtendByteToWord: + case IR::Opcode::SignExtendHalfToWord: + FoldSignExtendXToWord(inst); + break; case IR::Opcode::ZeroExtendByteToWord: case IR::Opcode::ZeroExtendHalfToWord: FoldZeroExtendXToWord(inst);