[llvm] r320068 - [InstCombine] add tests for abs using bit hackery; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 7 10:13:33 PST 2017


Author: spatel
Date: Thu Dec  7 10:13:33 2017
New Revision: 320068

URL: http://llvm.org/viewvc/llvm-project?rev=320068&view=rev
Log:
[InstCombine] add tests for abs using bit hackery; NFC

Modified:
    llvm/trunk/test/Transforms/InstCombine/abs-1.ll

Modified: llvm/trunk/test/Transforms/InstCombine/abs-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/abs-1.ll?rev=320068&r1=320067&r2=320068&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/abs-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/abs-1.ll Thu Dec  7 10:13:33 2017
@@ -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
+}
+




More information about the llvm-commits mailing list