Skip to content

Commit

Permalink
Added optimization for [MaxSignedValue - (x ⊕ c)] transformation (llv…
Browse files Browse the repository at this point in the history
…m#59)

* TEST: add yeaseen_opt function for future optimization use

* Added optimization for MaxSignedValue - (x ⊕ c) transformation

* Added test case for MaxSignedValue - (x ⊕ c) optimization including positive and negative tests

* Updated max_signed_value_xor.ll with additional tests

* put optmization code inside {} block

* cleaning up

* put optimization inside block

* myupdate

* upstream fix
  • Loading branch information
Yeaseen authored Sep 10, 2024
1 parent 7d0d0f1 commit fcb3066
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
20 changes: 20 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5247,6 +5247,26 @@ Instruction* cs6475_optimizer(Instruction *I, InstCombinerImpl &IC, LazyValueInf
}
}
// END JOHN REGEHR

// BEGIN YEASEEN ARAFAT
{
//0x7FFFFFFF - (x ⊕ c) → x ⊕ (0x7FFFFFFF - c)
ConstantInt *C1 = nullptr;
ConstantInt *C2 = nullptr;
Value *X = nullptr;
if(match(I, m_Sub(m_ConstantInt(C1), m_Xor(m_Value(X), m_ConstantInt(C2))))){
cs6475_debug("YA: Matched the left-side pattern 'MaxSignedValue - (x ⊕ c)'\n");
if(C1->getUniqueInteger().isMaxSignedValue()){
auto MaxSignedValue = APInt::getSignedMaxValue(C1->getUniqueInteger().getBitWidth());
auto NewConstant = MaxSignedValue - C2->getValue();
Instruction *NewI = BinaryOperator::CreateXor(X, ConstantInt::get(I->getContext(), NewConstant));
cs6475_debug("YA: Applied the optimization 'x ⊕ (MaxSignedValue - c)'\n");
log_optzn("Yeaseen Arafat");
return NewI;
}
}
}
//END YEASEEN ARAFAT

// BEGIN BRENSEN VILLEGAS
{
Expand Down
65 changes: 65 additions & 0 deletions llvm/test/Transforms/InstCombine/max_signed_value_xor.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
; RUN: opt -O2 -S < %s | FileCheck %s

; This is a positive test case checking that the optimization of the form
; MaxSignedValue - (x ⊕ c) → x ⊕ (MaxSignedValue - c) is correctly applied for 8-bit integers

define i8 @opt8(i8 %x) {
; CHECK-LABEL: @opt8(
; CHECK-NEXT: %b = xor i8 %x, 77
; CHECK-NEXT: ret i8 %b
%a = xor i8 %x, 50
%b = sub i8 127, %a ; MaxSignedValue for 8-bit is 127
ret i8 %b
}


; This is a positive test case checking that the optimization of the form
; MaxSignedValue - (x ⊕ c) → x ⊕ (MaxSignedValue - c) is correctly applied for 16-bit integers

define i16 @opt16(i16 %x) {
; CHECK-LABEL: @opt16(
; CHECK-NEXT: %b = xor i16 %x, 29434
; CHECK-NEXT: ret i16 %b
%a = xor i16 %x, 3333
%b = sub i16 32767, %a ; MaxSignedValue for 16-bit is 32767
ret i16 %b
}


; This is a positive test case checking that the optimization of the form
; MaxSignedValue - (x ⊕ c) → x ⊕ (MaxSignedValue - c) is correctly applied for 32-bit integers

define i32 @opt32(i32 %x) {
; CHECK-LABEL: @opt32(
; CHECK-NEXT: %b = xor i32 %x, 2147433647
; CHECK-NEXT: ret i32 %b
%a = xor i32 %x, 50000
%b = sub i32 2147483647, %a ; MaxSignedValue for 32-bit is 2147483647
ret i32 %b
}


; This is a positive test case checking that the optimization of the form
; MaxSignedValue - (x ⊕ c) → x ⊕ (MaxSignedValue - c) is correctly applied for 64-bit integers

define i64 @opt64(i64 %x) {
; CHECK-LABEL: @opt64(
; CHECK-NEXT: %b = xor i64 %x, 9223372036849775807
; CHECK-NEXT: ret i64 %b
%a = xor i64 %x, 5000000
%b = sub i64 9223372036854775807, %a ; MaxSignedValue for 64-bit is 9223372036854775807
ret i64 %b
}


; This is a negative test case where the optimization should NOT be applied
; because 20000 is not the max signed value for i16 (32767).

define i16 @neg_opt16(i16 %x) {
; CHECK-LABEL: @neg_opt16(
; CHECK-NOT: %b = xor
; CHECK: %b = sub i16 20000, %a
%a = xor i16 %x, 3333
%b = sub i16 20000, %a
ret i16 %b
}

0 comments on commit fcb3066

Please sign in to comment.