From be4d7464ac98f444ea0196f53df136fcbbfb40cc Mon Sep 17 00:00:00 2001 From: Jacob Knowlton <63576748+jakeknow17@users.noreply.github.com> Date: Tue, 10 Sep 2024 16:26:12 -0600 Subject: [PATCH] Jacob Knowlton - Assignment 2 Optimization (#58) * Added working optimization * Finished tests * Added back whitespace before return * Wrapped optimization in scope block --------- Co-authored-by: Jacob Knowlton --- .../InstCombine/InstructionCombining.cpp | 53 +++++++++++++++++++ .../Transforms/InstCombine/mul-const-icmp.ll | 36 +++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 llvm/test/Transforms/InstCombine/mul-const-icmp.ll diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index c28e49f591dcd8..6606ce827fe171 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -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(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(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(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(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 { diff --git a/llvm/test/Transforms/InstCombine/mul-const-icmp.ll b/llvm/test/Transforms/InstCombine/mul-const-icmp.ll new file mode 100644 index 00000000000000..78d7955c10e079 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/mul-const-icmp.ll @@ -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