<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Aug 20, 2014 at 1:22 AM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">----- Original Message -----<br>
> From: "David Majnemer" <<a href="mailto:david.majnemer@gmail.com">david.majnemer@gmail.com</a>><br>
> To: <a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
> Sent: Wednesday, August 20, 2014 2:17:31 AM<br>
> Subject: [llvm] r216045 - InstCombine: Annotate sub with nuw when we prove    it's safe<br>
><br>
> Author: majnemer<br>
> Date: Wed Aug 20 02:17:31 2014<br>
> New Revision: 216045<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=216045&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=216045&view=rev</a><br>
> Log:<br>
> InstCombine: Annotate sub with nuw when we prove it's safe<br>
><br>
> We can prove that a 'sub' can be a 'sub nuw' if the left-hand side is<br>
> negative and the right-hand side is non-negative.<br>
<br>
</div>I thought that we were specifically not doing this kind of thing because it can block further optimizations (as explained by Nick here: <a href="http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140331/211474.html" target="_blank">http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-x/211474.html</a>).</blockquote>
<div><br></div><div>If we are, nobody gave InstCombine the message. Here is some prior art:</div><div><a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?r1=210052&r2=210186">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?r1=210052&r2=210186</a><br>
</div><div><a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?r1=210186&r2=211084">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?r1=210186&r2=211084</a><br>
</div><div><br></div><div>Further, I don't think I agree with Nick's example:</div><div>$ cat t.cvc</div><div><div>A : BITVECTOR(32);</div><div><br></div><div>ASSERT SX(BVPLUS(32, BVXOR(A, 0hex80000000), 0hex00000001), 33) = BVPLUS(33, SX(BVXOR(A, 0hex80000000), 33), SX(0hex00000001, 33));    </div>
<div>QUERY BVPLUS(31, BVXOR(A, 0hex80000000), 0hex00000001) = BVPLUS(31, A, 0hex00000001);</div></div><div>$ cvc3 ~/t.cvc</div><div>Valid.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Is the motivation to get around the depth limit in computeKnownBits?<br>
<br>
   -Hal<br>
<div class=""><div class="h5"><br>
><br>
> Modified:<br>
>     llvm/trunk/lib/Transforms/InstCombine/InstCombine.h<br>
>     llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp<br>
>     llvm/trunk/test/Transforms/InstCombine/sub.ll<br>
><br>
> Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombine.h<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombine.h?rev=216045&r1=216044&r2=216045&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombine.h?rev=216045&r1=216044&r2=216045&view=diff</a><br>

> ==============================================================================<br>
> --- llvm/trunk/lib/Transforms/InstCombine/InstCombine.h (original)<br>
> +++ llvm/trunk/lib/Transforms/InstCombine/InstCombine.h Wed Aug 20<br>
> 02:17:31 2014<br>
> @@ -251,6 +251,7 @@ private:<br>
>    bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);<br>
>    bool WillNotOverflowUnsignedAdd(Value *LHS, Value *RHS);<br>
>    bool WillNotOverflowSignedSub(Value *LHS, Value *RHS);<br>
> +  bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS);<br>
>    Value *EmitGEPOffset(User *GEP);<br>
>    Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);<br>
>    Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int><br>
>    Mask);<br>
><br>
> Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=216045&r1=216044&r2=216045&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=216045&r1=216044&r2=216045&view=diff</a><br>

> ==============================================================================<br>
> --- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp<br>
> (original)<br>
> +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Wed<br>
> Aug 20 02:17:31 2014<br>
> @@ -988,6 +988,20 @@ bool InstCombiner::WillNotOverflowSigned<br>
>    return false;<br>
>  }<br>
><br>
> +/// \brief Return true if we can prove that:<br>
> +///    (sub LHS, RHS)  === (sub nuw LHS, RHS)<br>
> +bool InstCombiner::WillNotOverflowUnsignedSub(Value *LHS, Value<br>
> *RHS) {<br>
> +  // If the LHS is negative and the RHS is non-negative, no unsigned<br>
> wrap.<br>
> +  bool LHSKnownNonNegative, LHSKnownNegative;<br>
> +  bool RHSKnownNonNegative, RHSKnownNegative;<br>
> +  ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, 0);<br>
> +  ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, 0);<br>
> +  if (LHSKnownNegative && RHSKnownNonNegative)<br>
> +    return true;<br>
> +<br>
> +  return false;<br>
> +}<br>
> +<br>
>  // Checks if any operand is negative and we can convert add to sub.<br>
>  // This function checks for following negative patterns<br>
>  //   ADD(XOR(OR(Z, NOT(C)), C)), 1) == NEG(AND(Z, C))<br>
> @@ -1660,6 +1674,10 @@ Instruction *InstCombiner::visitSub(Bina<br>
>      Changed = true;<br>
>      I.setHasNoSignedWrap(true);<br>
>    }<br>
> +  if (!I.hasNoUnsignedWrap() && WillNotOverflowUnsignedSub(Op0,<br>
> Op1)) {<br>
> +    Changed = true;<br>
> +    I.setHasNoUnsignedWrap(true);<br>
> +  }<br>
><br>
>    return Changed ? &I : nullptr;<br>
>  }<br>
><br>
> Modified: llvm/trunk/test/Transforms/InstCombine/sub.ll<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sub.ll?rev=216045&r1=216044&r2=216045&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sub.ll?rev=216045&r1=216044&r2=216045&view=diff</a><br>

> ==============================================================================<br>
> --- llvm/trunk/test/Transforms/InstCombine/sub.ll (original)<br>
> +++ llvm/trunk/test/Transforms/InstCombine/sub.ll Wed Aug 20 02:17:31<br>
> 2014<br>
> @@ -506,6 +506,18 @@ define i4 @test42(i4 %x, i4 %y) {<br>
>  ; CHECK-LABEL: @test42(<br>
>  ; CHECK-NEXT: [[AND:%.*]] = and i4 %y, 7<br>
>  ; CHECK-NEXT: [[AND1:%.*]] = and i4 %x, 7<br>
> -; CHECK-NEXT: [[RET:%.*]] = sub nsw i4 %a, %b<br>
> +; CHECK-NEXT: [[RET:%.*]] = sub nsw i4 [[AND]], [[AND1]]<br>
> +; CHECK: ret i4 [[RET]]<br>
> +}<br>
> +<br>
> +define i4 @test43(i4 %x, i4 %y) {<br>
> +  %a = or i4 %x, -8<br>
> +  %b = and i4 %y, 7<br>
> +  %c = sub i4 %a, %b<br>
> +  ret i4 %c<br>
> +; CHECK-LABEL: @test43(<br>
> +; CHECK-NEXT: [[OR:%.*]] = or i4 %x, -8<br>
> +; CHECK-NEXT: [[AND:%.*]] = and i4 %y, 7<br>
> +; CHECK-NEXT: [[RET:%.*]] = sub nuw i4 [[OR]], [[AND]]<br>
>  ; CHECK: ret i4 [[RET]]<br>
>  }<br>
><br>
><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>
<br>
</div></div><span class=""><font color="#888888">--<br>
Hal Finkel<br>
Assistant Computational Scientist<br>
Leadership Computing Facility<br>
Argonne National Laboratory<br>
</font></span></blockquote></div><br></div></div>