From 2274214ff015e08e226ebf24413d864b1606e036 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 5 Oct 2018 17:45:33 -0400 Subject: [PATCH] constant_propagation_pass: Combine zero-extension folding code into its own function Separates the behavior from the actual switch statement and gets rid of duplication, now that we can use the general GetImmediateAsU64() function. --- src/ir_opt/constant_propagation_pass.cpp | 29 ++++++++++-------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/ir_opt/constant_propagation_pass.cpp b/src/ir_opt/constant_propagation_pass.cpp index aec32546..6cab7fc4 100644 --- a/src/ir_opt/constant_propagation_pass.cpp +++ b/src/ir_opt/constant_propagation_pass.cpp @@ -113,6 +113,15 @@ void FoldOR(IR::Inst& inst, bool is_32_bit) { inst.ReplaceUsesWith(lhs); } } + +void FoldZeroExtendXToWord(IR::Inst& inst) { + if (!inst.AreAllArgsImmediates()) { + return; + } + + const u64 value = inst.GetArg(0).GetImmediateAsU64(); + inst.ReplaceUsesWith(IR::Value{static_cast(value)}); +} } // Anonymous namespace void ConstantPropagation(IR::Block& block) { @@ -154,24 +163,10 @@ void ConstantPropagation(IR::Block& block) { case IR::Opcode::Not64: FoldNOT(inst, opcode == IR::Opcode::Not32); break; - case IR::Opcode::ZeroExtendByteToWord: { - if (!inst.AreAllArgsImmediates()) - break; - - u8 byte = inst.GetArg(0).GetU8(); - u32 value = static_cast(byte); - inst.ReplaceUsesWith(IR::Value{value}); + case IR::Opcode::ZeroExtendByteToWord: + case IR::Opcode::ZeroExtendHalfToWord: + FoldZeroExtendXToWord(inst); break; - } - case IR::Opcode::ZeroExtendHalfToWord: { - if (!inst.AreAllArgsImmediates()) - break; - - u16 half = inst.GetArg(0).GetU16(); - u32 value = static_cast(half); - inst.ReplaceUsesWith(IR::Value{value}); - break; - } default: break; }