<div dir="ltr">Thanks Benjamin!<div><br></div><div>Thanks to Suyog Sarda for working with me on this!</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, May 31, 2014 at 8:40 PM, Rahul Jain <span dir="ltr"><<a href="mailto:rahul1.jain@samsung.com" target="_blank">rahul1.jain@samsung.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Closed by commit rL209973 (authored by d0k).<br>
<br>
<a href="http://reviews.llvm.org/D3835" target="_blank">http://reviews.llvm.org/D3835</a><br>
<br>
Files:<br>
llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp<br>
llvm/trunk/test/Transforms/Reassociate/inverses.ll<br>
<br>
Index: llvm/trunk/test/Transforms/Reassociate/inverses.ll<br>
===================================================================<br>
--- llvm/trunk/test/Transforms/Reassociate/inverses.ll<br>
+++ llvm/trunk/test/Transforms/Reassociate/inverses.ll<br>
<div class="">@@ -32,3 +32,15 @@<br>
; CHECK: %tmp.5 = add i32 %b, 1234<br>
; CHECK: ret i32 %tmp.5<br>
}<br>
+<br>
+define i32 @test4(i32 %b, i32 %a) {<br>
+ %tmp.1 = add i32 %a, 1234<br>
+ %tmp.2 = add i32 %b, %tmp.1<br>
+ %tmp.4 = xor i32 %a, -1<br>
+ ; (b+(a+1234))+~a -> b+1233<br>
+ %tmp.5 = add i32 %tmp.2, %tmp.4<br>
+ ret i32 %tmp.5<br>
+; CHECK-LABEL: @test4(<br>
+; CHECK: %tmp.5 = add i32 %b, 1233<br>
+; CHECK: ret i32 %tmp.5<br>
+}<br>
</div>Index: llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp<br>
===================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp<br>
+++ llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp<br>
@@ -1368,11 +1368,10 @@<br>
Value *Reassociate::OptimizeAdd(Instruction *I,<br>
<div class="HOEnZb"><div class="h5"> SmallVectorImpl<ValueEntry> &Ops) {<br>
// Scan the operand lists looking for X and -X pairs. If we find any, we<br>
- // can simplify the expression. X+-X == 0. While we're at it, scan for any<br>
+ // can simplify expressions like X+-X == 0 and X+~X ==-1. While we're at it,<br>
+ // scan for any<br>
// duplicates. We want to canonicalize Y+Y+Y+Z -> 3*Y+Z.<br>
- //<br>
- // TODO: We could handle "X + ~X" -> "-1" if we wanted, since "-X = ~X+1".<br>
- //<br>
+<br>
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {<br>
Value *TheOp = Ops[i].Op;<br>
// Check to see if we've seen this operand before. If so, we factor all<br>
@@ -1412,19 +1411,28 @@<br>
continue;<br>
}<br>
<br>
- // Check for X and -X in the operand list.<br>
- if (!BinaryOperator::isNeg(TheOp))<br>
+ // Check for X and -X or X and ~X in the operand list.<br>
+ if (!BinaryOperator::isNeg(TheOp) && !BinaryOperator::isNot(TheOp))<br>
continue;<br>
<br>
- Value *X = BinaryOperator::getNegArgument(TheOp);<br>
+ Value *X = nullptr;<br>
+ if (BinaryOperator::isNeg(TheOp))<br>
+ X = BinaryOperator::getNegArgument(TheOp);<br>
+ else if (BinaryOperator::isNot(TheOp))<br>
+ X = BinaryOperator::getNotArgument(TheOp);<br>
+<br>
unsigned FoundX = FindInOperandList(Ops, i, X);<br>
if (FoundX == i)<br>
continue;<br>
<br>
// Remove X and -X from the operand list.<br>
- if (Ops.size() == 2)<br>
+ if (Ops.size() == 2 && BinaryOperator::isNeg(TheOp))<br>
return Constant::getNullValue(X->getType());<br>
<br>
+ // Remove X and ~X from the operand list.<br>
+ if (Ops.size() == 2 && BinaryOperator::isNot(TheOp))<br>
+ return Constant::getAllOnesValue(X->getType());<br>
+<br>
Ops.erase(Ops.begin()+i);<br>
if (i < FoundX)<br>
--FoundX;<br>
@@ -1434,6 +1442,13 @@<br>
++NumAnnihil;<br>
--i; // Revisit element.<br>
e -= 2; // Removed two elements.<br>
+<br>
+ // if X and ~X we append -1 to the operand list.<br>
+ if (BinaryOperator::isNot(TheOp)) {<br>
+ Value *V = Constant::getAllOnesValue(X->getType());<br>
+ Ops.insert(Ops.end(), ValueEntry(getRank(V), V));<br>
+ e += 1;<br>
+ }<br>
}<br>
<br>
// Scan the operand list, checking to see if there are any common factors<br>
</div></div><br>_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div><br></div>