From 28171015242e2e6c34ad9d658b7eec4673bf5387 Mon Sep 17 00:00:00 2001 From: brensen11 <90063365+brensen11@users.noreply.github.com> Date: Tue, 10 Sep 2024 16:25:13 -0600 Subject: [PATCH] Brensen11 a2 - Take 4 (#57) * working optimization to get rid of extra xor * cleanup and added negative test case in addition to the positive test case * removed unrelated changes * added brackets for avoiding variable name collisions --- .../InstCombine/InstructionCombining.cpp | 57 +++++++++++++++++++ .../InstCombine/select-specific-transform.ll | 33 +++++++++++ 2 files changed, 90 insertions(+) create mode 100644 llvm/test/Transforms/InstCombine/select-specific-transform.ll diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 38c483f5f33f62..c28e49f591dcd8 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -5060,6 +5060,55 @@ void cs6475_debug(std::string DbgString) { dbgs() << DbgString; } +Instruction* cs6475_optimizer_brensen(Instruction *I) { + // BEGIN BRENSEN VILLEGAS + Value *XorLhs = nullptr; + + Value *X = nullptr; + Value *Y = nullptr; + + Value *Cond = nullptr; + CmpInst::Predicate Pred; + + ConstantInt *RhsCond = nullptr; + Value *LhsCond = nullptr; + + if (match(I, m_Xor(m_Value(XorLhs), m_Value(X)))) { + cs6475_debug("BV: matched 'xor'\n"); + if (match(XorLhs, m_Select( + m_Value(Cond), + m_Value(Y), + m_ZeroInt() + ))) { + cs6475_debug("BV: matched 'select'\n"); + if (match(Cond, m_ICmp(Pred, m_Value(LhsCond), m_ConstantInt(RhsCond)))) { + cs6475_debug("BV: matched 'condition of select'\n"); + if (! (Pred == CmpInst::ICMP_EQ)) + return nullptr; + cs6475_debug("BV: matched 'icmp eq'\n"); + + if (!RhsCond->getUniqueInteger().isAllOnes()) + return nullptr; + cs6475_debug("BV: matched '-1 of eq rhs'\n"); + + if (match(LhsCond, m_Xor(m_Specific(Y), m_Value(X)))) { + cs6475_debug("BV: matched '(Y^X) of eq lhs'\n"); + log_optzn("\nBrensen Villegas\n"); + auto MinOne = APInt::getAllOnes(RhsCond->getUniqueInteger().getBitWidth()); + return SelectInst::Create( + Cond, + ConstantInt::get(I->getContext(), MinOne), + X + ); + } + } + } + } + return nullptr; + // END BRENSEN VILLEGAS +} + + Instruction *cs6475_optimizer_suraj(Instruction *I) { // Converts // if x is even, return x+1 else return x-1 @@ -5199,6 +5248,14 @@ Instruction* cs6475_optimizer(Instruction *I, InstCombinerImpl &IC, LazyValueInf } // END JOHN REGEHR + // BEGIN BRENSEN VILLEGAS + { + Instruction *BV_I = cs6475_optimizer_brensen(I); + if (BV_I != nullptr) + return BV_I; + } + // END BRENSEN VILLEGAS + // BEGIN SURAJ YADAV { auto *NewI = cs6475_optimizer_suraj(I); diff --git a/llvm/test/Transforms/InstCombine/select-specific-transform.ll b/llvm/test/Transforms/InstCombine/select-specific-transform.ll new file mode 100644 index 00000000000000..2cd85bb2636226 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/select-specific-transform.ll @@ -0,0 +1,33 @@ +; RUN: opt -passes=instcombine -S < %s | FileCheck %s + +; ----------------- POSITIVE TEST CASE ----------------- + +define i32 @src(i32 %x, i32 %y) { + %xor1 = xor i32 %y, %x + %cmp = icmp eq i32 %xor1, -1 + %sel = select i1 %cmp, i32 %y, i32 0 + %xor2 = xor i32 %sel, %x + ret i32 %xor2 +} + +; CHECK: %xor1 = xor i32 %y, %x +; CHECK: %cmp = icmp eq i32 %xor1, -1 +; CHECK: %xor2 = select i1 %cmp, i32 -1, i32 %x +; CHECK-NOT: %xor2 = xor i32 %sel, %x +; CHECK: ret i32 %xor2 + +; ----------------- NEGATIVE TEST CASE ----------------- + +define i32 @src2(i32 %x, i32 %y) { + %xor1 = xor i32 %y, %x + %cmp = icmp eq i32 %xor1, -2 + %sel = select i1 %cmp, i32 %y, i32 0 + %xor2 = xor i32 %sel, %x + ret i32 %xor2 +} + +; CHECK: %xor1 = xor i32 %y, %x +; CHECK: %cmp = icmp eq i32 %xor1, -2 +; CHECK: %sel = select i1 %cmp, i32 %y, i32 0 +; CHECK: %xor2 = xor i32 %sel, %x +; CHECK: ret i32 %xor2 \ No newline at end of file