From 8013548bbb74f10577ea429f5a9c6f1dcfa6bd64 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 29 Sep 2018 03:57:16 -0400 Subject: [PATCH] constant_propagation_pass: Fold OR operations --- src/ir_opt/constant_propagation_pass.cpp | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/ir_opt/constant_propagation_pass.cpp b/src/ir_opt/constant_propagation_pass.cpp index edcea826..f315a3f1 100644 --- a/src/ir_opt/constant_propagation_pass.cpp +++ b/src/ir_opt/constant_propagation_pass.cpp @@ -76,6 +76,34 @@ void FoldEOR(IR::Inst& inst, bool is_32_bit) { inst.ReplaceUsesWith(lhs); } } + +// Folds OR operations based on the following: +// +// 1. imm_x | imm_y -> result +// 2. x | 0 -> x +// 3. 0 | y -> y +// +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) { + const u64 result = lhs.GetImmediateAsU64() | rhs.GetImmediateAsU64(); + + if (is_32_bit) { + inst.ReplaceUsesWith(IR::Value{static_cast(result)}); + } else { + inst.ReplaceUsesWith(IR::Value{result}); + } + } else if (is_lhs_immediate && lhs.GetImmediateAsU64() == 0) { + inst.ReplaceUsesWith(rhs); + } else if (is_rhs_immediate && rhs.GetImmediateAsU64() == 0) { + inst.ReplaceUsesWith(lhs); + } +} } // Anonymous namespace void ConstantPropagation(IR::Block& block) { @@ -109,6 +137,10 @@ void ConstantPropagation(IR::Block& block) { case IR::Opcode::Eor64: FoldEOR(inst, opcode == IR::Opcode::Eor32); break; + case IR::Opcode::Or32: + case IR::Opcode::Or64: + FoldOR(inst, opcode == IR::Opcode::Or32); + break; case IR::Opcode::ZeroExtendByteToWord: { if (!inst.AreAllArgsImmediates()) break;