llvm-mirror/test/Transforms/InstSimplify/constantfold-add-nuw-allones-to-allones.ll
Roman Lebedev 84dcefc37c [InstSimplify] add nuw %x, -1 -> -1 fold.
Summary:
`%ret = add nuw i8 %x, C`
From [[ https://llvm.org/docs/LangRef.html#add-instruction | langref ]]:
    nuw and nsw stand for “No Unsigned Wrap” and “No Signed Wrap”,
    respectively. If the nuw and/or nsw keywords are present,
    the result value of the add is a poison value if unsigned
    and/or signed overflow, respectively, occurs.

So if `C` is `-1`, `%x` can only be `0`, and the result is always `-1`.

I'm not sure we want to use `KnownBits`/`LVI` here, because there is
exactly one possible value (all bits set, `-1`), so some other pass
should take care of replacing the known-all-ones with constant `-1`.

The `test/Transforms/InstCombine/set-lowbits-mask-canonicalize.ll` change *is* confusing.
What happening is, before this: (omitting `nuw` for simplicity)
1. First, InstCombine D47428/rL334127 folds `shl i32 1, %NBits`) to `shl nuw i32 -1, %NBits`
2. Then, InstSimplify D47883/rL334222 folds `shl nuw i32 -1, %NBits` to `-1`,
3. `-1` is inverted to `0`.
But now:
1. *This* InstSimplify fold `%ret = add nuw i32 %setbit, -1` -> `-1` happens first,
   before InstCombine D47428/rL334127 fold could happen.
Thus we now end up with the opposite constant,
and it is all good: https://rise4fun.com/Alive/OA9

https://rise4fun.com/Alive/sldC
Was mentioned in D47428 review.
Follow-up for D47883.

Reviewers: spatel, craig.topper

Reviewed By: spatel

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D47908

llvm-svn: 334298
2018-06-08 15:44:47 +00:00

141 lines
3.7 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instsimplify -S | FileCheck %s
; %ret = add nuw i8 %x, C
; nuw means no unsigned wrap, from -1 to 0.
; So if C is -1, %x can only be 0, and the result is always -1.
define i8 @add_nuw (i8 %x) {
; CHECK-LABEL: @add_nuw(
; CHECK-NEXT: ret i8 -1
;
%ret = add nuw i8 %x, -1
; nuw here means that %x can only be 0
ret i8 %ret
}
define i8 @add_nuw_nsw (i8 %x) {
; CHECK-LABEL: @add_nuw_nsw(
; CHECK-NEXT: ret i8 -1
;
%ret = add nuw nsw i8 %x, -1
; nuw here means that %x can only be 0
ret i8 %ret
}
define i8 @add_nuw_commute (i8 %x) {
; CHECK-LABEL: @add_nuw_commute(
; CHECK-NEXT: ret i8 -1
;
%ret = add nuw i8 -1, %x ; swapped
; nuw here means that %x can only be 0
ret i8 %ret
}
; ============================================================================ ;
; Positive tests with value range known
; ============================================================================ ;
declare void @llvm.assume(i1 %cond);
define i8 @knownbits_allones(i8 %x, i8 %y) {
; CHECK-LABEL: @knownbits_allones(
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[Y:%.*]], -2
; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X:%.*]], [[Y]]
; CHECK-NEXT: ret i8 [[RET]]
;
%cmp = icmp slt i8 %y, 254
tail call void @llvm.assume(i1 %cmp)
%ret = add nuw i8 %x, %y
ret i8 %ret
}
; ============================================================================ ;
; Vectors
; ============================================================================ ;
define <2 x i8> @add_vec(<2 x i8> %x) {
; CHECK-LABEL: @add_vec(
; CHECK-NEXT: ret <2 x i8> <i8 -1, i8 -1>
;
%ret = add nuw <2 x i8> %x, <i8 -1, i8 -1>
ret <2 x i8> %ret
}
define <3 x i8> @add_vec_undef(<3 x i8> %x) {
; CHECK-LABEL: @add_vec_undef(
; CHECK-NEXT: ret <3 x i8> <i8 -1, i8 undef, i8 -1>
;
%ret = add nuw <3 x i8> %x, <i8 -1, i8 undef, i8 -1>
ret <3 x i8> %ret
}
; ============================================================================ ;
; Negative tests. Should not be folded.
; ============================================================================ ;
define i8 @bad_add (i8 %x) {
; CHECK-LABEL: @bad_add(
; CHECK-NEXT: [[RET:%.*]] = add i8 [[X:%.*]], -1
; CHECK-NEXT: ret i8 [[RET]]
;
%ret = add i8 %x, -1 ; need nuw
ret i8 %ret
}
define i8 @bad_add_nsw (i8 %x) {
; CHECK-LABEL: @bad_add_nsw(
; CHECK-NEXT: [[RET:%.*]] = add nsw i8 [[X:%.*]], -1
; CHECK-NEXT: ret i8 [[RET]]
;
%ret = add nsw i8 %x, -1 ; need nuw
ret i8 %ret
}
; Second `add` operand is not `-1` constant
define i8 @bad_add0(i8 %x, i8 %addop2) {
; CHECK-LABEL: @bad_add0(
; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X:%.*]], [[ADDOP2:%.*]]
; CHECK-NEXT: ret i8 [[RET]]
;
%ret = add nuw i8 %x, %addop2
ret i8 %ret
}
; Bad constant
define i8 @bad_add1(i8 %x) {
; CHECK-LABEL: @bad_add1(
; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X:%.*]], 1
; CHECK-NEXT: ret i8 [[RET]]
;
%ret = add nuw i8 %x, 1 ; not -1
ret i8 %ret
}
define <2 x i8> @bad_add_vec_nonsplat(<2 x i8> %x) {
; CHECK-LABEL: @bad_add_vec_nonsplat(
; CHECK-NEXT: [[RET:%.*]] = add nuw <2 x i8> [[X:%.*]], <i8 -1, i8 1>
; CHECK-NEXT: ret <2 x i8> [[RET]]
;
%ret = add nuw <2 x i8> %x, <i8 -1, i8 1>
ret <2 x i8> %ret
}
; Bad known bits
define i8 @bad_knownbits(i8 %x, i8 %y) {
; CHECK-LABEL: @bad_knownbits(
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], -3
; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X]], [[Y:%.*]]
; CHECK-NEXT: ret i8 [[RET]]
;
%cmp = icmp slt i8 %x, 253
tail call void @llvm.assume(i1 %cmp)
%ret = add nuw i8 %x, %y
ret i8 %ret
}