Skip to content

Commit

Permalink
Brensen11 a2 - Take 4 (llvm#57)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
brensen11 authored Sep 10, 2024
1 parent 637fb2b commit 2817101
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
57 changes: 57 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions llvm/test/Transforms/InstCombine/select-specific-transform.ll
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2817101

Please sign in to comment.