From d302d9bd0ca711bf006f000aa29af756fa3e5727 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 23 Nov 2018 23:06:29 -0500 Subject: [PATCH] constant_propagation_pass: Handle folding for Least/MostSignificant{Bit, Byte, Half, Word} opcodes These are quite trivial to fold. --- src/ir_opt/constant_propagation_pass.cpp | 60 ++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/ir_opt/constant_propagation_pass.cpp b/src/ir_opt/constant_propagation_pass.cpp index 887755e4..f7b8601f 100644 --- a/src/ir_opt/constant_propagation_pass.cpp +++ b/src/ir_opt/constant_propagation_pass.cpp @@ -98,6 +98,51 @@ void FoldEOR(IR::Inst& inst, bool is_32_bit) { } } +void FoldLeastSignificantByte(IR::Inst& inst) { + if (!inst.AreAllArgsImmediates()) { + return; + } + + const auto operand = inst.GetArg(0); + inst.ReplaceUsesWith(IR::Value{static_cast(operand.GetImmediateAsU64())}); +} + +void FoldLeastSignificantHalf(IR::Inst& inst) { + if (!inst.AreAllArgsImmediates()) { + return; + } + + const auto operand = inst.GetArg(0); + inst.ReplaceUsesWith(IR::Value{static_cast(operand.GetImmediateAsU64())}); +} + +void FoldLeastSignificantWord(IR::Inst& inst) { + if (!inst.AreAllArgsImmediates()) { + return; + } + + const auto operand = inst.GetArg(0); + inst.ReplaceUsesWith(IR::Value{static_cast(operand.GetImmediateAsU64())}); +} + +void FoldMostSignificantBit(IR::Inst& inst) { + if (!inst.AreAllArgsImmediates()) { + return; + } + + const auto operand = inst.GetArg(0); + inst.ReplaceUsesWith(IR::Value{(operand.GetImmediateAsU64() >> 31) != 0}); +} + +void FoldMostSignificantWord(IR::Inst& inst) { + if (!inst.AreAllArgsImmediates()) { + return; + } + + const auto operand = inst.GetArg(0); + inst.ReplaceUsesWith(IR::Value{static_cast(operand.GetImmediateAsU64() >> 32)}); +} + // Folds multiplication operations based on the following: // // 1. imm_x * imm_y -> result @@ -216,6 +261,21 @@ void ConstantPropagation(IR::Block& block) { const auto opcode = inst.GetOpcode(); switch (opcode) { + case IR::Opcode::LeastSignificantWord: + FoldLeastSignificantWord(inst); + break; + case IR::Opcode::MostSignificantWord: + FoldMostSignificantWord(inst); + break; + case IR::Opcode::LeastSignificantHalf: + FoldLeastSignificantHalf(inst); + break; + case IR::Opcode::LeastSignificantByte: + FoldLeastSignificantByte(inst); + break; + case IR::Opcode::MostSignificantBit: + FoldMostSignificantBit(inst); + break; case IR::Opcode::LogicalShiftLeft32: case IR::Opcode::LogicalShiftLeft64: case IR::Opcode::LogicalShiftRight32: