[llvm] 0788392 - [InstCombine] add tests for reassociative sub/add expressions; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue May 26 11:49:26 PDT 2020


Author: Sanjay Patel
Date: 2020-05-26T14:49:16-04:00
New Revision: 0788392637f414c312a995f3202177a2919eba2f

URL: https://github.com/llvm/llvm-project/commit/0788392637f414c312a995f3202177a2919eba2f
DIFF: https://github.com/llvm/llvm-project/commit/0788392637f414c312a995f3202177a2919eba2f.diff

LOG: [InstCombine] add tests for reassociative sub/add expressions; NFC

Added: 
    

Modified: 
    llvm/test/Transforms/InstCombine/sub.ll

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index f6fa797eb0c8..52c51c70f02c 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -1538,3 +1538,86 @@ define i8 @test75(i8 %x) {
   %t1 = sub i8 %x, %t0
   ret i8 %t1
 }
+
+; ((w-x) + y) - z --> (w+y) - (x+z)
+
+define i8 @sub_add_sub_reassoc(i8 %w, i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @sub_add_sub_reassoc(
+; CHECK-NEXT:    [[S1:%.*]] = sub i8 [[W:%.*]], [[X:%.*]]
+; CHECK-NEXT:    [[A:%.*]] = add i8 [[S1]], [[Y:%.*]]
+; CHECK-NEXT:    [[S2:%.*]] = sub i8 [[A]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[S2]]
+;
+  %s1 = sub i8 %w, %x
+  %a = add i8 %s1, %y
+  %s2 = sub i8 %a, %z
+  ret i8 %s2
+}
+
+; vectors work too.
+
+define <2 x i8> @sub_add_sub_reassoc_commute(<2 x i8> %w, <2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
+; CHECK-LABEL: @sub_add_sub_reassoc_commute(
+; CHECK-NEXT:    [[D:%.*]] = sdiv <2 x i8> [[Y:%.*]], <i8 42, i8 -42>
+; CHECK-NEXT:    [[S1:%.*]] = sub <2 x i8> [[W:%.*]], [[X:%.*]]
+; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[D]], [[S1]]
+; CHECK-NEXT:    [[S2:%.*]] = sub <2 x i8> [[A]], [[Z:%.*]]
+; CHECK-NEXT:    ret <2 x i8> [[S2]]
+;
+  %d = sdiv <2 x i8> %y, <i8 42, i8 -42> ; thwart complexity-based canonicalization
+  %s1 = sub <2 x i8> %w, %x
+  %a = add <2 x i8> %d, %s1
+  %s2 = sub <2 x i8> %a, %z
+  ret <2 x i8> %s2
+}
+
+; (v-w) + (x-y) - z --> (v+x) - (w+y+z)
+
+define i8 @sub_add_sub_reassoc_twice(i8 %v, i8 %w, i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @sub_add_sub_reassoc_twice(
+; CHECK-NEXT:    [[S1:%.*]] = sub i8 [[V:%.*]], [[W:%.*]]
+; CHECK-NEXT:    [[S2:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[A:%.*]] = add i8 [[S1]], [[S2]]
+; CHECK-NEXT:    [[S3:%.*]] = sub i8 [[A]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[S3]]
+;
+  %s1 = sub i8 %v, %w
+  %s2 = sub i8 %x, %y
+  %a = add i8 %s1, %s2
+  %s3 = sub i8 %a, %z
+  ret i8 %s3
+}
+
+; negative test - uses
+
+define i8 @sub_add_sub_reassoc_use1(i8 %w, i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @sub_add_sub_reassoc_use1(
+; CHECK-NEXT:    [[S1:%.*]] = sub i8 [[W:%.*]], [[X:%.*]]
+; CHECK-NEXT:    call void @use8(i8 [[S1]])
+; CHECK-NEXT:    [[A:%.*]] = add i8 [[S1]], [[Y:%.*]]
+; CHECK-NEXT:    [[S2:%.*]] = sub i8 [[A]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[S2]]
+;
+  %s1 = sub i8 %w, %x
+  call void @use8(i8 %s1)
+  %a = add i8 %s1, %y
+  %s2 = sub i8 %a, %z
+  ret i8 %s2
+}
+
+; negative test - uses
+
+define i8 @sub_add_sub_reassoc_use2(i8 %w, i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @sub_add_sub_reassoc_use2(
+; CHECK-NEXT:    [[S1:%.*]] = sub i8 [[W:%.*]], [[X:%.*]]
+; CHECK-NEXT:    [[A:%.*]] = add i8 [[S1]], [[Y:%.*]]
+; CHECK-NEXT:    call void @use8(i8 [[A]])
+; CHECK-NEXT:    [[S2:%.*]] = sub i8 [[A]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[S2]]
+;
+  %s1 = sub i8 %w, %x
+  %a = add i8 %s1, %y
+  call void @use8(i8 %a)
+  %s2 = sub i8 %a, %z
+  ret i8 %s2
+}


        


More information about the llvm-commits mailing list