[llvm] r216045 - InstCombine: Annotate sub with nuw when we prove it's safe

David Majnemer david.majnemer at gmail.com
Wed Aug 20 21:20:56 PDT 2014


On Wed, Aug 20, 2014 at 1:22 AM, Hal Finkel <hfinkel at anl.gov> wrote:

> ----- Original Message -----
> > From: "David Majnemer" <david.majnemer at gmail.com>
> > To: llvm-commits at cs.uiuc.edu
> > Sent: Wednesday, August 20, 2014 2:17:31 AM
> > Subject: [llvm] r216045 - InstCombine: Annotate sub with nuw when we
> prove    it's safe
> >
> > Author: majnemer
> > Date: Wed Aug 20 02:17:31 2014
> > New Revision: 216045
> >
> > URL: http://llvm.org/viewvc/llvm-project?rev=216045&view=rev
> > Log:
> > InstCombine: Annotate sub with nuw when we prove it's safe
> >
> > We can prove that a 'sub' can be a 'sub nuw' if the left-hand side is
> > negative and the right-hand side is non-negative.
>
> I thought that we were specifically not doing this kind of thing because
> it can block further optimizations (as explained by Nick here:
> http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-x/211474.html
> <http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140331/211474.html>
> ).


If we are, nobody gave InstCombine the message. Here is some prior art:
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=210186&r2=211084

Further, I don't think I agree with Nick's example:
$ cat t.cvc
A : BITVECTOR(32);

ASSERT SX(BVPLUS(32, BVXOR(A, 0hex80000000), 0hex00000001), 33) =
BVPLUS(33, SX(BVXOR(A, 0hex80000000), 33), SX(0hex00000001, 33));
QUERY BVPLUS(31, BVXOR(A, 0hex80000000), 0hex00000001) = BVPLUS(31, A,
0hex00000001);
$ cvc3 ~/t.cvc
Valid.


> Is the motivation to get around the depth limit in computeKnownBits?
>
>    -Hal
>
> >
> > Modified:
> >     llvm/trunk/lib/Transforms/InstCombine/InstCombine.h
> >     llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
> >     llvm/trunk/test/Transforms/InstCombine/sub.ll
> >
> > Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombine.h
> > URL:
> >
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombine.h?rev=216045&r1=216044&r2=216045&view=diff
> >
> ==============================================================================
> > --- llvm/trunk/lib/Transforms/InstCombine/InstCombine.h (original)
> > +++ llvm/trunk/lib/Transforms/InstCombine/InstCombine.h Wed Aug 20
> > 02:17:31 2014
> > @@ -251,6 +251,7 @@ private:
> >    bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);
> >    bool WillNotOverflowUnsignedAdd(Value *LHS, Value *RHS);
> >    bool WillNotOverflowSignedSub(Value *LHS, Value *RHS);
> > +  bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS);
> >    Value *EmitGEPOffset(User *GEP);
> >    Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
> >    Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int>
> >    Mask);
> >
> > Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
> > URL:
> >
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=216045&r1=216044&r2=216045&view=diff
> >
> ==============================================================================
> > --- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
> > (original)
> > +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Wed
> > Aug 20 02:17:31 2014
> > @@ -988,6 +988,20 @@ bool InstCombiner::WillNotOverflowSigned
> >    return false;
> >  }
> >
> > +/// \brief Return true if we can prove that:
> > +///    (sub LHS, RHS)  === (sub nuw LHS, RHS)
> > +bool InstCombiner::WillNotOverflowUnsignedSub(Value *LHS, Value
> > *RHS) {
> > +  // If the LHS is negative and the RHS is non-negative, no unsigned
> > wrap.
> > +  bool LHSKnownNonNegative, LHSKnownNegative;
> > +  bool RHSKnownNonNegative, RHSKnownNegative;
> > +  ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, 0);
> > +  ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, 0);
> > +  if (LHSKnownNegative && RHSKnownNonNegative)
> > +    return true;
> > +
> > +  return false;
> > +}
> > +
> >  // Checks if any operand is negative and we can convert add to sub.
> >  // This function checks for following negative patterns
> >  //   ADD(XOR(OR(Z, NOT(C)), C)), 1) == NEG(AND(Z, C))
> > @@ -1660,6 +1674,10 @@ Instruction *InstCombiner::visitSub(Bina
> >      Changed = true;
> >      I.setHasNoSignedWrap(true);
> >    }
> > +  if (!I.hasNoUnsignedWrap() && WillNotOverflowUnsignedSub(Op0,
> > Op1)) {
> > +    Changed = true;
> > +    I.setHasNoUnsignedWrap(true);
> > +  }
> >
> >    return Changed ? &I : nullptr;
> >  }
> >
> > Modified: llvm/trunk/test/Transforms/InstCombine/sub.ll
> > URL:
> >
> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sub.ll?rev=216045&r1=216044&r2=216045&view=diff
> >
> ==============================================================================
> > --- llvm/trunk/test/Transforms/InstCombine/sub.ll (original)
> > +++ llvm/trunk/test/Transforms/InstCombine/sub.ll Wed Aug 20 02:17:31
> > 2014
> > @@ -506,6 +506,18 @@ define i4 @test42(i4 %x, i4 %y) {
> >  ; CHECK-LABEL: @test42(
> >  ; CHECK-NEXT: [[AND:%.*]] = and i4 %y, 7
> >  ; CHECK-NEXT: [[AND1:%.*]] = and i4 %x, 7
> > -; CHECK-NEXT: [[RET:%.*]] = sub nsw i4 %a, %b
> > +; CHECK-NEXT: [[RET:%.*]] = sub nsw i4 [[AND]], [[AND1]]
> > +; CHECK: ret i4 [[RET]]
> > +}
> > +
> > +define i4 @test43(i4 %x, i4 %y) {
> > +  %a = or i4 %x, -8
> > +  %b = and i4 %y, 7
> > +  %c = sub i4 %a, %b
> > +  ret i4 %c
> > +; CHECK-LABEL: @test43(
> > +; CHECK-NEXT: [[OR:%.*]] = or i4 %x, -8
> > +; CHECK-NEXT: [[AND:%.*]] = and i4 %y, 7
> > +; CHECK-NEXT: [[RET:%.*]] = sub nuw i4 [[OR]], [[AND]]
> >  ; CHECK: ret i4 [[RET]]
> >  }
> >
> >
> > _______________________________________________
> > llvm-commits mailing list
> > llvm-commits at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> >
>
> --
> Hal Finkel
> Assistant Computational Scientist
> Leadership Computing Facility
> Argonne National Laboratory
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140820/bdac55cf/attachment.html>


More information about the llvm-commits mailing list