Skip to content

Commit

Permalink
Jacob Knowlton - Assignment 2 Optimization (llvm#58)
Browse files Browse the repository at this point in the history
* Added working optimization

* Finished tests

* Added back whitespace before return

* Wrapped optimization in scope block

---------

Co-authored-by: Jacob Knowlton <u1117344@utah.edu>
  • Loading branch information
jakeknow17 and Jacob Knowlton authored Sep 10, 2024
1 parent 2817101 commit be4d746
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
53 changes: 53 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5247,6 +5247,59 @@ Instruction* cs6475_optimizer(Instruction *I, InstCombinerImpl &IC, LazyValueInf
}
}
// END JOHN REGEHR

// BEGIN JACOB KNOWLTON
{
Value *X1 = nullptr;
Value *X2 = nullptr;
// >= case
ICmpInst::Predicate Pred1 = ICmpInst::ICMP_SGE;
if (match(I, m_ICmp(Pred1, m_Value(X), m_Value(Y)))) {
if (match(Y, m_ConstantInt(C))) {
if (C->isZero()) {
if (match(X, m_Mul(m_Value(X1), m_Value(X2)))) {
auto Mul1 = dyn_cast<BinaryOperator>(X);
if (Mul1->hasNoSignedWrap()) {
if (match(X1, m_Mul(m_Specific(X2), m_ConstantInt(C))) || match(X1, m_Shl(m_Specific(X2), m_ConstantInt(C)))) {
auto Mul2 = dyn_cast<BinaryOperator>(X1);
if (Mul2->hasNoSignedWrap()) {
if (C->getUniqueInteger().isNonNegative()) {
log_optzn("Jacob Knowlton");
ICmpInst::Predicate Pred3 = ICmpInst::ICMP_EQ;
return new ICmpInst(Pred3, ConstantInt::getTrue(I->getContext()), ConstantInt::getTrue(I->getContext()));
}
}
}
}
}
}
}
}
// > case
ICmpInst::Predicate Pred2 = ICmpInst::ICMP_SGT;
if (match(I, m_ICmp(Pred2, m_Value(X), m_Value(Y)))) {
if (match(Y, m_ConstantInt(C))) {
if (C->isMinusOne()) {
if (match(X, m_Mul(m_Value(X1), m_Value(X2)))) {
auto Mul1 = dyn_cast<BinaryOperator>(X);
if (Mul1->hasNoSignedWrap()) {
if (match(X1, m_Mul(m_Specific(X2), m_ConstantInt(C))) || match(X1, m_Shl(m_Specific(X2), m_ConstantInt(C)))) {
auto Mul2 = dyn_cast<BinaryOperator>(X1);
if (Mul2->hasNoSignedWrap()) {
if (C->getUniqueInteger().isNonNegative()) {
log_optzn("Jacob Knowlton");
ICmpInst::Predicate Pred3 = ICmpInst::ICMP_EQ;
return new ICmpInst(Pred3, ConstantInt::getTrue(I->getContext()), ConstantInt::getTrue(I->getContext()));
}
}
}
}
}
}
}
}
}
// END JACOB KNOWLTON

// BEGIN BRENSEN VILLEGAS
{
Expand Down
36 changes: 36 additions & 0 deletions llvm/test/Transforms/InstCombine/mul-const-icmp.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

; Test 1: Simple Case (Optimization Applied)
define i1 @test1(i16 %x) {
%mul1 = mul nsw i16 %x, 3 ; Positive constant
%mul2 = mul nsw i16 %mul1, %x
%cmp = icmp sge i16 %mul2, 0
ret i1 %cmp
}

; CHECK-LABEL: @test1(
; CHECK-NEXT: ret i1 true

; Test 2: No `nsw` Flag (Optimization Not Applied)
define i1 @test2(i16 %x) {
%mul1 = mul i16 %x, 3 ; No 'nsw' flag
%mul2 = mul i16 %mul1, %x
%cmp = icmp sge i16 %mul2, 0
ret i1 %cmp
}

; CHECK-LABEL: @test2(
; CHECK-NOT: ret i1 true
; CHECK: ret i1 %cmp

; Test 3: Non-Positive Constant (Optimization Not Applied)
define i1 @test3(i16 %x) {
%mul1 = mul nsw i16 %x, -5 ; Non-positive constant
%mul2 = mul nsw i16 %mul1, %x
%cmp = icmp sge i16 %mul2, 0
ret i1 %cmp
}

; CHECK-LABEL: @test3(
; CHECK-NOT: ret i1 true
; CHECK: ret i1 %cmp

0 comments on commit be4d746

Please sign in to comment.