[llvm] r332726 - [InstCombine] add tests for lack of abs/nabs canonicalization; NFC
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Fri May 18 08:26:39 PDT 2018
Author: spatel
Date: Fri May 18 08:26:38 2018
New Revision: 332726
URL: http://llvm.org/viewvc/llvm-project?rev=332726&view=rev
Log:
[InstCombine] add tests for lack of abs/nabs canonicalization; 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=332726&r1=332725&r2=332726&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/abs-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/abs-1.ll Fri May 18 08:26:38 2018
@@ -1,3 +1,4 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; 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"
@@ -11,9 +12,9 @@ declare i64 @llabs(i64)
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: [[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)
@@ -22,9 +23,9 @@ define i32 @test_abs(i32 %x) {
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: [[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)
@@ -33,24 +34,140 @@ define i64 @test_labs(i64 %x) {
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: [[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)
ret i64 %ret
}
+; FIXME: We should have a canonical form of abs to make CSE easier.
+
+define i8 @abs_canonical_1(i8 %x) {
+; CHECK-LABEL: @abs_canonical_1(
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i8 [[X]], i8 [[NEG]]
+; CHECK-NEXT: ret i8 [[ABS]]
+;
+ %cmp = icmp sgt i8 %x, 0
+ %neg = sub i8 0, %x
+ %abs = select i1 %cmp, i8 %x, i8 %neg
+ ret i8 %abs
+}
+
+; FIXME: Vectors should work too.
+
+define <2 x i8> @abs_canonical_2(<2 x i8> %x) {
+; CHECK-LABEL: @abs_canonical_2(
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 -1, i8 -1>
+; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[X]], <2 x i8> [[NEG]]
+; CHECK-NEXT: ret <2 x i8> [[ABS]]
+;
+ %cmp = icmp sgt <2 x i8> %x, <i8 -1, i8 -1>
+ %neg = sub <2 x i8> zeroinitializer, %x
+ %abs = select <2 x i1> %cmp, <2 x i8> %x, <2 x i8> %neg
+ ret <2 x i8> %abs
+}
+
+; NSW should not change.
+
+define i8 @abs_canonical_3(i8 %x) {
+; CHECK-LABEL: @abs_canonical_3(
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[NEG:%.*]] = sub nsw i8 0, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i8 [[NEG]], i8 [[X]]
+; CHECK-NEXT: ret i8 [[ABS]]
+;
+ %cmp = icmp slt i8 %x, 0
+ %neg = sub nsw i8 0, %x
+ %abs = select i1 %cmp, i8 %neg, i8 %x
+ ret i8 %abs
+}
+
+define i8 @abs_canonical_4(i8 %x) {
+; CHECK-LABEL: @abs_canonical_4(
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 1
+; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i8 [[NEG]], i8 [[X]]
+; CHECK-NEXT: ret i8 [[ABS]]
+;
+ %cmp = icmp slt i8 %x, 1
+ %neg = sub i8 0, %x
+ %abs = select i1 %cmp, i8 %neg, i8 %x
+ ret i8 %abs
+}
+
+; FIXME: We should have a canonical form of nabs to make CSE easier.
+
+define i8 @nabs_canonical_1(i8 %x) {
+; CHECK-LABEL: @nabs_canonical_1(
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i8 [[NEG]], i8 [[X]]
+; CHECK-NEXT: ret i8 [[ABS]]
+;
+ %cmp = icmp sgt i8 %x, 0
+ %neg = sub i8 0, %x
+ %abs = select i1 %cmp, i8 %neg, i8 %x
+ ret i8 %abs
+}
+
+; FIXME: Vectors should work too.
+
+define <2 x i8> @nabs_canonical_2(<2 x i8> %x) {
+; CHECK-LABEL: @nabs_canonical_2(
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 -1, i8 -1>
+; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[NEG]], <2 x i8> [[X]]
+; CHECK-NEXT: ret <2 x i8> [[ABS]]
+;
+ %cmp = icmp sgt <2 x i8> %x, <i8 -1, i8 -1>
+ %neg = sub <2 x i8> zeroinitializer, %x
+ %abs = select <2 x i1> %cmp, <2 x i8> %neg, <2 x i8> %x
+ ret <2 x i8> %abs
+}
+
+; NSW should not change.
+
+define i8 @nabs_canonical_3(i8 %x) {
+; CHECK-LABEL: @nabs_canonical_3(
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[NEG:%.*]] = sub nsw i8 0, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i8 [[X]], i8 [[NEG]]
+; CHECK-NEXT: ret i8 [[ABS]]
+;
+ %cmp = icmp slt i8 %x, 0
+ %neg = sub nsw i8 0, %x
+ %abs = select i1 %cmp, i8 %x, i8 %neg
+ ret i8 %abs
+}
+
+define i8 @nabs_canonical_4(i8 %x) {
+; CHECK-LABEL: @nabs_canonical_4(
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 1
+; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i8 [[X]], i8 [[NEG]]
+; CHECK-NEXT: ret i8 [[ABS]]
+;
+ %cmp = icmp slt i8 %x, 1
+ %neg = sub i8 0, %x
+ %abs = select i1 %cmp, i8 %x, i8 %neg
+ ret i8 %abs
+}
+
; 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: [[TMP1:%.*]] = icmp slt i8 %x, 0
-; CHECK-NEXT: [[TMP2:%.*]] = sub i8 0, %x
-; CHECK-NEXT: [[ABS:%.*]] = select i1 [[TMP1]], i8 [[TMP2]], i8 %x
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[TMP2:%.*]] = sub i8 0, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[TMP1]], i8 [[TMP2]], i8 [[X]]
; CHECK-NEXT: ret i8 [[ABS]]
;
%signbit = ashr i8 %x, 7
@@ -61,9 +178,9 @@ define i8 @shifty_abs_commute0(i8 %x) {
define i8 @shifty_abs_commute0_nsw(i8 %x) {
; CHECK-LABEL: @shifty_abs_commute0_nsw(
-; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i8 %x, 0
-; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i8 0, %x
-; CHECK-NEXT: [[ABS:%.*]] = select i1 [[TMP1]], i8 [[TMP2]], i8 %x
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i8 0, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[TMP1]], i8 [[TMP2]], i8 [[X]]
; CHECK-NEXT: ret i8 [[ABS]]
;
%signbit = ashr i8 %x, 7
@@ -77,8 +194,8 @@ define i8 @shifty_abs_commute0_nsw(i8 %x
; have produced all 1s. We partially optimize this.
define i8 @shifty_abs_commute0_nuw(i8 %x) {
; CHECK-LABEL: @shifty_abs_commute0_nuw(
-; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i8 %x, 0
-; CHECK-NEXT: [[ABS:%.*]] = select i1 [[TMP1]], i8 %x, i8 0
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[TMP1]], i8 [[X]], i8 0
; CHECK-NEXT: ret i8 [[ABS]]
;
%signbit = ashr i8 %x, 7
@@ -89,9 +206,9 @@ define i8 @shifty_abs_commute0_nuw(i8 %x
define <2 x i8> @shifty_abs_commute1(<2 x i8> %x) {
; CHECK-LABEL: @shifty_abs_commute1(
-; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i8> %x, zeroinitializer
-; CHECK-NEXT: [[TMP2:%.*]] = sub <2 x i8> zeroinitializer, %x
-; CHECK-NEXT: [[ABS:%.*]] = select <2 x i1> [[TMP1]], <2 x i8> [[TMP2]], <2 x i8> %x
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i8> [[X:%.*]], zeroinitializer
+; CHECK-NEXT: [[TMP2:%.*]] = sub <2 x i8> zeroinitializer, [[X]]
+; CHECK-NEXT: [[ABS:%.*]] = select <2 x i1> [[TMP1]], <2 x i8> [[TMP2]], <2 x i8> [[X]]
; CHECK-NEXT: ret <2 x i8> [[ABS]]
;
%signbit = ashr <2 x i8> %x, <i8 7, i8 7>
@@ -102,7 +219,7 @@ define <2 x i8> @shifty_abs_commute1(<2
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: [[Y:%.*]] = mul <2 x i8> [[X:%.*]], <i8 3, i8 3>
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i8> [[Y]], zeroinitializer
; CHECK-NEXT: [[TMP2:%.*]] = sub <2 x i8> zeroinitializer, [[Y]]
; CHECK-NEXT: [[ABS:%.*]] = select <2 x i1> [[TMP1]], <2 x i8> [[TMP2]], <2 x i8> [[Y]]
@@ -117,7 +234,7 @@ define <2 x i8> @shifty_abs_commute2(<2
define i8 @shifty_abs_commute3(i8 %x) {
; CHECK-LABEL: @shifty_abs_commute3(
-; CHECK-NEXT: [[Y:%.*]] = mul i8 %x, 3
+; CHECK-NEXT: [[Y:%.*]] = mul i8 [[X:%.*]], 3
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i8 [[Y]], 0
; CHECK-NEXT: [[TMP2:%.*]] = sub i8 0, [[Y]]
; CHECK-NEXT: [[ABS:%.*]] = select i1 [[TMP1]], i8 [[TMP2]], i8 [[Y]]
@@ -136,8 +253,8 @@ 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: [[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]]
More information about the llvm-commits
mailing list