diff options
| author | Sanjay Patel <spatel@rotateright.com> | 2017-12-07 18:13:33 +0000 |
|---|---|---|
| committer | Sanjay Patel <spatel@rotateright.com> | 2017-12-07 18:13:33 +0000 |
| commit | 6cfc1368704d403d7be9136ac1561eff43d387d5 (patch) | |
| tree | b3b800371ae6ad966167a0ba0c609d40fe321918 | |
| parent | e2564051be1d5ec4c35de569d9b054cb1f8d2edf (diff) | |
| download | bcm5719-llvm-6cfc1368704d403d7be9136ac1561eff43d387d5.tar.gz bcm5719-llvm-6cfc1368704d403d7be9136ac1561eff43d387d5.zip | |
[InstCombine] add tests for abs using bit hackery; NFC
llvm-svn: 320068
| -rw-r--r-- | llvm/test/Transforms/InstCombine/abs-1.ll | 124 |
1 files changed, 103 insertions, 21 deletions
diff --git a/llvm/test/Transforms/InstCombine/abs-1.ll b/llvm/test/Transforms/InstCombine/abs-1.ll index 2c223edcfa7..de782f7248f 100644 --- a/llvm/test/Transforms/InstCombine/abs-1.ll +++ b/llvm/test/Transforms/InstCombine/abs-1.ll @@ -1,5 +1,3 @@ -; Test that the abs library call simplifier works correctly. -; ; RUN: opt < %s -instcombine -S | FileCheck %s target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128" @@ -8,34 +6,118 @@ declare i32 @abs(i32) declare i64 @labs(i64) declare i64 @llabs(i64) -; Check abs(x) -> x >s -1 ? x : -x. +; Test that the abs library call simplifier works correctly. +; abs(x) -> x >s -1 ? x : -x. -define i32 @test_simplify1(i32 %x) { -; CHECK-LABEL: @test_simplify1( +define i32 @test_abs(i32 %x) { +; CHECK-LABEL: @test_abs( +; CHECK-NEXT: [[ISPOS:%.*]] = icmp sgt i32 %x, -1 +; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, %x +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[ISPOS]], i32 %x, i32 [[NEG]] +; CHECK-NEXT: ret i32 [[TMP1]] +; %ret = call i32 @abs(i32 %x) -; CHECK-NEXT: [[ISPOS:%[a-z0-9]+]] = icmp sgt i32 %x, -1 -; CHECK-NEXT: [[NEG:%[a-z0-9]+]] = sub i32 0, %x -; CHECK-NEXT: [[RET:%[a-z0-9]+]] = select i1 [[ISPOS]], i32 %x, i32 [[NEG]] ret i32 %ret -; CHECK-NEXT: ret i32 [[RET]] } -define i64 @test_simplify2(i64 %x) { -; CHECK-LABEL: @test_simplify2( +define i64 @test_labs(i64 %x) { +; CHECK-LABEL: @test_labs( +; CHECK-NEXT: [[ISPOS:%.*]] = icmp sgt i64 %x, -1 +; CHECK-NEXT: [[NEG:%.*]] = sub i64 0, %x +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[ISPOS]], i64 %x, i64 [[NEG]] +; CHECK-NEXT: ret i64 [[TMP1]] +; %ret = call i64 @labs(i64 %x) -; CHECK-NEXT: [[ISPOS:%[a-z0-9]+]] = icmp sgt i64 %x, -1 -; CHECK-NEXT: [[NEG:%[a-z0-9]+]] = sub i64 0, %x -; CHECK-NEXT: [[RET:%[a-z0-9]+]] = select i1 [[ISPOS]], i64 %x, i64 [[NEG]] ret i64 %ret -; CHECK-NEXT: ret i64 [[RET]] } -define i64 @test_simplify3(i64 %x) { -; CHECK-LABEL: @test_simplify3( +define i64 @test_llabs(i64 %x) { +; CHECK-LABEL: @test_llabs( +; CHECK-NEXT: [[ISPOS:%.*]] = icmp sgt i64 %x, -1 +; CHECK-NEXT: [[NEG:%.*]] = sub i64 0, %x +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[ISPOS]], i64 %x, i64 [[NEG]] +; CHECK-NEXT: ret i64 [[TMP1]] +; %ret = call i64 @llabs(i64 %x) -; CHECK-NEXT: [[ISPOS:%[a-z0-9]+]] = icmp sgt i64 %x, -1 -; CHECK-NEXT: [[NEG:%[a-z0-9]+]] = sub i64 0, %x -; CHECK-NEXT: [[RET:%[a-z0-9]+]] = select i1 [[ISPOS]], i64 %x, i64 [[NEG]] ret i64 %ret -; CHECK-NEXT: ret i64 [[RET]] } + +; The following 5 tests use a shift+add+xor to implement abs(): +; B = ashr i8 A, 7 -- smear the sign bit. +; xor (add A, B), B -- add -1 and flip bits if negative + +define i8 @shifty_abs_commute0(i8 %x) { +; CHECK-LABEL: @shifty_abs_commute0( +; CHECK-NEXT: [[SIGNBIT:%.*]] = ashr i8 %x, 7 +; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SIGNBIT]], %x +; CHECK-NEXT: [[ABS:%.*]] = xor i8 [[ADD]], [[SIGNBIT]] +; CHECK-NEXT: ret i8 [[ABS]] +; + %signbit = ashr i8 %x, 7 + %add = add i8 %signbit, %x + %abs = xor i8 %add, %signbit + ret i8 %abs +} + +define <2 x i8> @shifty_abs_commute1(<2 x i8> %x) { +; CHECK-LABEL: @shifty_abs_commute1( +; CHECK-NEXT: [[SIGNBIT:%.*]] = ashr <2 x i8> %x, <i8 7, i8 7> +; CHECK-NEXT: [[ADD:%.*]] = add <2 x i8> [[SIGNBIT]], %x +; CHECK-NEXT: [[ABS:%.*]] = xor <2 x i8> [[SIGNBIT]], [[ADD]] +; CHECK-NEXT: ret <2 x i8> [[ABS]] +; + %signbit = ashr <2 x i8> %x, <i8 7, i8 7> + %add = add <2 x i8> %signbit, %x + %abs = xor <2 x i8> %signbit, %add + ret <2 x i8> %abs +} + +define <2 x i8> @shifty_abs_commute2(<2 x i8> %x) { +; CHECK-LABEL: @shifty_abs_commute2( +; CHECK-NEXT: [[Y:%.*]] = mul <2 x i8> %x, <i8 3, i8 3> +; CHECK-NEXT: [[SIGNBIT:%.*]] = ashr <2 x i8> [[Y]], <i8 7, i8 7> +; CHECK-NEXT: [[ADD:%.*]] = add <2 x i8> [[Y]], [[SIGNBIT]] +; CHECK-NEXT: [[ABS:%.*]] = xor <2 x i8> [[SIGNBIT]], [[ADD]] +; CHECK-NEXT: ret <2 x i8> [[ABS]] +; + %y = mul <2 x i8> %x, <i8 3, i8 3> ; extra op to thwart complexity-based canonicalization + %signbit = ashr <2 x i8> %y, <i8 7, i8 7> + %add = add <2 x i8> %y, %signbit + %abs = xor <2 x i8> %signbit, %add + ret <2 x i8> %abs +} + +define i8 @shifty_abs_commute3(i8 %x) { +; CHECK-LABEL: @shifty_abs_commute3( +; CHECK-NEXT: [[Y:%.*]] = mul i8 %x, 3 +; CHECK-NEXT: [[SIGNBIT:%.*]] = ashr i8 [[Y]], 7 +; CHECK-NEXT: [[ADD:%.*]] = add i8 [[Y]], [[SIGNBIT]] +; CHECK-NEXT: [[ABS:%.*]] = xor i8 [[ADD]], [[SIGNBIT]] +; CHECK-NEXT: ret i8 [[ABS]] +; + %y = mul i8 %x, 3 ; extra op to thwart complexity-based canonicalization + %signbit = ashr i8 %y, 7 + %add = add i8 %y, %signbit + %abs = xor i8 %add, %signbit + ret i8 %abs +} + +; Negative test - don't transform if it would increase instruction count. + +declare void @extra_use(i8) + +define i8 @shifty_abs_too_many_uses(i8 %x) { +; CHECK-LABEL: @shifty_abs_too_many_uses( +; CHECK-NEXT: [[SIGNBIT:%.*]] = ashr i8 %x, 7 +; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SIGNBIT]], %x +; CHECK-NEXT: [[ABS:%.*]] = xor i8 [[ADD]], [[SIGNBIT]] +; CHECK-NEXT: call void @extra_use(i8 [[SIGNBIT]]) +; CHECK-NEXT: ret i8 [[ABS]] +; + %signbit = ashr i8 %x, 7 + %add = add i8 %x, %signbit + %abs = xor i8 %add, %signbit + call void @extra_use(i8 %signbit) + ret i8 %abs +} + |

