Skip to content

Commit be4d746

Browse files
jakeknow17Jacob Knowlton
and
Jacob Knowlton
authored
Jacob Knowlton - Assignment 2 Optimization (llvm#58)
* Added working optimization * Finished tests * Added back whitespace before return * Wrapped optimization in scope block --------- Co-authored-by: Jacob Knowlton <u1117344@utah.edu>
1 parent 2817101 commit be4d746

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -5247,6 +5247,59 @@ Instruction* cs6475_optimizer(Instruction *I, InstCombinerImpl &IC, LazyValueInf
52475247
}
52485248
}
52495249
// END JOHN REGEHR
5250+
5251+
// BEGIN JACOB KNOWLTON
5252+
{
5253+
Value *X1 = nullptr;
5254+
Value *X2 = nullptr;
5255+
// >= case
5256+
ICmpInst::Predicate Pred1 = ICmpInst::ICMP_SGE;
5257+
if (match(I, m_ICmp(Pred1, m_Value(X), m_Value(Y)))) {
5258+
if (match(Y, m_ConstantInt(C))) {
5259+
if (C->isZero()) {
5260+
if (match(X, m_Mul(m_Value(X1), m_Value(X2)))) {
5261+
auto Mul1 = dyn_cast<BinaryOperator>(X);
5262+
if (Mul1->hasNoSignedWrap()) {
5263+
if (match(X1, m_Mul(m_Specific(X2), m_ConstantInt(C))) || match(X1, m_Shl(m_Specific(X2), m_ConstantInt(C)))) {
5264+
auto Mul2 = dyn_cast<BinaryOperator>(X1);
5265+
if (Mul2->hasNoSignedWrap()) {
5266+
if (C->getUniqueInteger().isNonNegative()) {
5267+
log_optzn("Jacob Knowlton");
5268+
ICmpInst::Predicate Pred3 = ICmpInst::ICMP_EQ;
5269+
return new ICmpInst(Pred3, ConstantInt::getTrue(I->getContext()), ConstantInt::getTrue(I->getContext()));
5270+
}
5271+
}
5272+
}
5273+
}
5274+
}
5275+
}
5276+
}
5277+
}
5278+
// > case
5279+
ICmpInst::Predicate Pred2 = ICmpInst::ICMP_SGT;
5280+
if (match(I, m_ICmp(Pred2, m_Value(X), m_Value(Y)))) {
5281+
if (match(Y, m_ConstantInt(C))) {
5282+
if (C->isMinusOne()) {
5283+
if (match(X, m_Mul(m_Value(X1), m_Value(X2)))) {
5284+
auto Mul1 = dyn_cast<BinaryOperator>(X);
5285+
if (Mul1->hasNoSignedWrap()) {
5286+
if (match(X1, m_Mul(m_Specific(X2), m_ConstantInt(C))) || match(X1, m_Shl(m_Specific(X2), m_ConstantInt(C)))) {
5287+
auto Mul2 = dyn_cast<BinaryOperator>(X1);
5288+
if (Mul2->hasNoSignedWrap()) {
5289+
if (C->getUniqueInteger().isNonNegative()) {
5290+
log_optzn("Jacob Knowlton");
5291+
ICmpInst::Predicate Pred3 = ICmpInst::ICMP_EQ;
5292+
return new ICmpInst(Pred3, ConstantInt::getTrue(I->getContext()), ConstantInt::getTrue(I->getContext()));
5293+
}
5294+
}
5295+
}
5296+
}
5297+
}
5298+
}
5299+
}
5300+
}
5301+
}
5302+
// END JACOB KNOWLTON
52505303

52515304
// BEGIN BRENSEN VILLEGAS
52525305
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
2+
3+
; Test 1: Simple Case (Optimization Applied)
4+
define i1 @test1(i16 %x) {
5+
%mul1 = mul nsw i16 %x, 3 ; Positive constant
6+
%mul2 = mul nsw i16 %mul1, %x
7+
%cmp = icmp sge i16 %mul2, 0
8+
ret i1 %cmp
9+
}
10+
11+
; CHECK-LABEL: @test1(
12+
; CHECK-NEXT: ret i1 true
13+
14+
; Test 2: No `nsw` Flag (Optimization Not Applied)
15+
define i1 @test2(i16 %x) {
16+
%mul1 = mul i16 %x, 3 ; No 'nsw' flag
17+
%mul2 = mul i16 %mul1, %x
18+
%cmp = icmp sge i16 %mul2, 0
19+
ret i1 %cmp
20+
}
21+
22+
; CHECK-LABEL: @test2(
23+
; CHECK-NOT: ret i1 true
24+
; CHECK: ret i1 %cmp
25+
26+
; Test 3: Non-Positive Constant (Optimization Not Applied)
27+
define i1 @test3(i16 %x) {
28+
%mul1 = mul nsw i16 %x, -5 ; Non-positive constant
29+
%mul2 = mul nsw i16 %mul1, %x
30+
%cmp = icmp sge i16 %mul2, 0
31+
ret i1 %cmp
32+
}
33+
34+
; CHECK-LABEL: @test3(
35+
; CHECK-NOT: ret i1 true
36+
; CHECK: ret i1 %cmp

0 commit comments

Comments
 (0)