From 126b2c30bd339d11b573dea32537f44fc792d5dd Mon Sep 17 00:00:00 2001 From: korenkonder Date: Sun, 22 Sep 2024 15:02:20 +0300 Subject: [PATCH] Optimise out unnecessary shifts (#1021) --- src/shader_recompiler/ir/ir_emitter.cpp | 12 ++++++++++++ .../ir/passes/constant_propagation_pass.cpp | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/src/shader_recompiler/ir/ir_emitter.cpp b/src/shader_recompiler/ir/ir_emitter.cpp index ce809514d..a7edb6d9c 100644 --- a/src/shader_recompiler/ir/ir_emitter.cpp +++ b/src/shader_recompiler/ir/ir_emitter.cpp @@ -1079,6 +1079,10 @@ U32 IREmitter::IAbs(const U32& value) { } U32U64 IREmitter::ShiftLeftLogical(const U32U64& base, const U32& shift) { + if (shift.IsImmediate() && shift.U32() == 0) { + return base; + } + switch (base.Type()) { case Type::U32: return Inst(Opcode::ShiftLeftLogical32, base, shift); @@ -1090,6 +1094,10 @@ U32U64 IREmitter::ShiftLeftLogical(const U32U64& base, const U32& shift) { } U32U64 IREmitter::ShiftRightLogical(const U32U64& base, const U32& shift) { + if (shift.IsImmediate() && shift.U32() == 0) { + return base; + } + switch (base.Type()) { case Type::U32: return Inst(Opcode::ShiftRightLogical32, base, shift); @@ -1101,6 +1109,10 @@ U32U64 IREmitter::ShiftRightLogical(const U32U64& base, const U32& shift) { } U32U64 IREmitter::ShiftRightArithmetic(const U32U64& base, const U32& shift) { + if (shift.IsImmediate() && shift.U32() == 0) { + return base; + } + switch (base.Type()) { case Type::U32: return Inst(Opcode::ShiftRightArithmetic32, base, shift); diff --git a/src/shader_recompiler/ir/passes/constant_propagation_pass.cpp b/src/shader_recompiler/ir/passes/constant_propagation_pass.cpp index 87a069338..53cb35a74 100644 --- a/src/shader_recompiler/ir/passes/constant_propagation_pass.cpp +++ b/src/shader_recompiler/ir/passes/constant_propagation_pass.cpp @@ -278,6 +278,12 @@ void ConstantPropagation(IR::Block& block, IR::Inst& inst) { case IR::Opcode::FPCmpClass32: FoldCmpClass(inst); return; + case IR::Opcode::ShiftLeftLogical32: + FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return static_cast(a << b); }); + return; + case IR::Opcode::ShiftRightLogical32: + FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return static_cast(a >> b); }); + return; case IR::Opcode::ShiftRightArithmetic32: FoldWhenAllImmediates(inst, [](s32 a, s32 b) { return static_cast(a >> b); }); return;