Skip to content

Commit

Permalink
[InstCombine] add fold for demand of low bit of abs()
Browse files Browse the repository at this point in the history
  • Loading branch information
rotateright committed Mar 30, 2021
1 parent 79ae419 commit c2ebad8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,11 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
bool KnownBitsComputed = false;
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
switch (II->getIntrinsicID()) {
case Intrinsic::abs: {
if (DemandedMask == 1)
return II->getArgOperand(0);
break;
}
case Intrinsic::bswap: {
// If the only bits demanded come from one byte of the bswap result,
// just shift the input byte into position to eliminate the bswap.
Expand Down
12 changes: 8 additions & 4 deletions llvm/test/Transforms/InstCombine/abs-intrinsic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -352,28 +352,32 @@ define <4 x i8> @trunc_abs_sext_vec(<4 x i8> %x) {
ret <4 x i8> %t
}

; abs() doesn't change the low bit.

define i32 @demand_low_bit(i32 %x) {
; CHECK-LABEL: @demand_low_bit(
; CHECK-NEXT: [[A:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
; CHECK-NEXT: [[R:%.*]] = and i32 [[A]], 1
; CHECK-NEXT: [[R:%.*]] = and i32 [[X:%.*]], 1
; CHECK-NEXT: ret i32 [[R]]
;
%a = call i32 @llvm.abs.i32(i32 %x, i1 false)
%r = and i32 %a, 1
ret i32 %r
}

; Int min behavior doesn't affect the transform.

define <3 x i82> @demand_low_bit_int_min_is_poison(<3 x i82> %x) {
; CHECK-LABEL: @demand_low_bit_int_min_is_poison(
; CHECK-NEXT: [[A:%.*]] = call <3 x i82> @llvm.abs.v3i82(<3 x i82> [[X:%.*]], i1 true)
; CHECK-NEXT: [[R:%.*]] = shl <3 x i82> [[A]], <i82 81, i82 81, i82 81>
; CHECK-NEXT: [[R:%.*]] = shl <3 x i82> [[X:%.*]], <i82 81, i82 81, i82 81>
; CHECK-NEXT: ret <3 x i82> [[R]]
;
%a = call <3 x i82> @llvm.abs.v3i82(<3 x i82> %x, i1 true)
%r = shl <3 x i82> %a, <i82 81, i82 81, i82 81>
ret <3 x i82> %r
}

; Negative test - only low bit is allowed.

define i32 @demand_low_bits(i32 %x) {
; CHECK-LABEL: @demand_low_bits(
; CHECK-NEXT: [[A:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
Expand Down

0 comments on commit c2ebad8

Please sign in to comment.