Skip to content

Commit 637fb2b

Browse files
authored
Missing optimization assignment: Get Sibling node in a 1 indexed binary heap (llvm#47)
* Test Remote Change * Implementation * Add test cases * Fix possible PR comments * Fix PR comments
1 parent a0cdc6f commit 637fb2b

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5060,6 +5060,46 @@ void cs6475_debug(std::string DbgString) {
50605060
dbgs() << DbgString;
50615061
}
50625062

5063+
Instruction *cs6475_optimizer_suraj(Instruction *I) {
5064+
// Converts
5065+
// if x is even, return x+1 else return x-1
5066+
// to
5067+
// return x^1
5068+
5069+
Value *INPUT, *A, *B, *C;
5070+
ConstantInt *CI = nullptr;
5071+
5072+
if (!match(I, m_c_Add(m_Value(C), m_Value(INPUT)))) {
5073+
return nullptr;
5074+
}
5075+
cs6475_debug("Add Expression Matched\n");
5076+
5077+
if (!(match(C, m_Select(m_Value(B), m_One(), m_ConstantInt(CI))) &&
5078+
CI->isMinusOne())) {
5079+
return nullptr;
5080+
}
5081+
cs6475_debug("Select Expression Matched\n");
5082+
5083+
if (!match(B, m_SpecificICmp(ICmpInst::ICMP_EQ, m_Value(A), m_ZeroInt()))) {
5084+
return nullptr;
5085+
}
5086+
cs6475_debug("Compare Expression Matched\n");
5087+
5088+
if (!match(A, m_And(m_Specific(INPUT), m_One()))) {
5089+
return nullptr;
5090+
}
5091+
cs6475_debug("And Expression Matched\n");
5092+
5093+
cs6475_debug("Suraj: Optimization Possible\n");
5094+
auto Bitwidth = A->getType()->getIntegerBitWidth();
5095+
auto One = APInt(Bitwidth, 1);
5096+
5097+
log_optzn("Suraj Yadav");
5098+
5099+
auto *OptIns =
5100+
BinaryOperator::CreateXor(INPUT, ConstantInt::get(I->getContext(), One));
5101+
return OptIns;
5102+
}
50635103

50645104
Instruction *cs6475_optimizer_tavakkoli(Instruction *I) {
50655105
cs6475_debug("\nCS 6475 matcher: running now\n");
@@ -5159,6 +5199,15 @@ Instruction* cs6475_optimizer(Instruction *I, InstCombinerImpl &IC, LazyValueInf
51595199
}
51605200
// END JOHN REGEHR
51615201

5202+
// BEGIN SURAJ YADAV
5203+
{
5204+
auto *NewI = cs6475_optimizer_suraj(I);
5205+
if (NewI != nullptr) {
5206+
return NewI;
5207+
}
5208+
}
5209+
// END SURAJ YADAV
5210+
51625211
// BEGIN ASHTON WIERSDORF
51635212
// x : float; c1, c2 are literal constants
51645213
// x * x + c1 > c2 && c1 > c2 ⇒ is_nan(x)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
; RUN: opt -O2 -S < %s | FileCheck %s
2+
3+
; Positive Tests
4+
define i4 @sibling-node-i4(i4 %0) {
5+
; CHECK-LABEL: @sibling-node-i4(
6+
; CHECK: [[d:%.+]] = xor i4 %0, 1
7+
; CHECK-NEXT: ret i4 [[d]]
8+
;
9+
%a = urem i4 %0, 2
10+
%b = icmp eq i4 %a, 1
11+
br i1 %b, label %right,label %left
12+
left:
13+
%c = add i4 %0, 1
14+
ret i4 %c
15+
right:
16+
%d = sub i4 %0, 1
17+
ret i4 %d
18+
}
19+
20+
21+
define i16 @sibling-node-i16(i16 %0) {
22+
; CHECK-LABEL: @sibling-node-i16(
23+
; CHECK: [[d:%.+]] = xor i16 %0, 1
24+
; CHECK-NEXT: ret i16 [[d]]
25+
;
26+
%a = and i16 %0, 1
27+
%b = icmp eq i16 %a, 0
28+
%c = select i1 %b, i16 1, i16 -1
29+
%d = add i16 %c, %0
30+
ret i16 %d
31+
}
32+
33+
34+
; Negative Tests
35+
define i8 @sibling-node-i8-fail(i8 %0) {
36+
; CHECK-LABEL: @sibling-node-i8-fail(
37+
; CHECK: [[a:%.+]] = and i8 %0, 1
38+
; CHECK-NEXT: [[b:%.+]] = icmp eq i8 [[a]], 0
39+
; CHECK-NEXT: [[c:%.+]] = select i1 [[b]], i8 -1, i8 1
40+
; CHECK-NEXT: [[d:%.+]] = add i8 [[c]], %0
41+
; CHECK-NEXT: ret i8 [[d]]
42+
;
43+
%a = urem i8 %0, 2
44+
%b = icmp eq i8 %a, 1
45+
br i1 %b, label %right,label %left
46+
left:
47+
%c = sub i8 %0, 1
48+
ret i8 %c
49+
right:
50+
%d = add i8 %0, 1
51+
ret i8 %d
52+
}

0 commit comments

Comments
 (0)