[llvm] 5ab489d - [NFC][InstCombine] Add tests for '((X - Y) - Z)' pattern (PR49858)

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 6 05:59:29 PDT 2021


Author: Roman Lebedev
Date: 2021-04-06T15:58:14+03:00
New Revision: 5ab489d9ae99c60d6f7e1e01194e0b4aacaf68af

URL: https://github.com/llvm/llvm-project/commit/5ab489d9ae99c60d6f7e1e01194e0b4aacaf68af
DIFF: https://github.com/llvm/llvm-project/commit/5ab489d9ae99c60d6f7e1e01194e0b4aacaf68af.diff

LOG: [NFC][InstCombine] Add tests for '((X - Y) - Z)' pattern (PR49858)

Added: 
    llvm/test/Transforms/InstCombine/sub-from-sub.ll

Modified: 
    

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/InstCombine/sub-from-sub.ll b/llvm/test/Transforms/InstCombine/sub-from-sub.ll
new file mode 100644
index 000000000000..202dbd1072a4
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/sub-from-sub.ll
@@ -0,0 +1,170 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -instcombine %s -S -o - | FileCheck %s
+
+; ((X - Y) - Z)  -->  X - (Y + Z)  because we prefer add's.
+
+declare void @use8(i8)
+
+; Basic test
+define i8 @t0(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @t0(
+; CHECK-NEXT:    [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 %x, %y
+  %r = sub i8 %i0, %z
+  ret i8 %r
+}
+
+; No flags are propagated
+define i8 @t1_flags(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @t1_flags(
+; CHECK-NEXT:    [[O0:%.*]] = sub nuw nsw i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = sub nuw nsw i8 [[O0]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %o0 = sub nuw nsw i8 %x, %y
+  %r = sub nuw nsw i8 %o0, %z
+  ret i8 %r
+}
+
+; The inner sub must have single use.
+define i8 @n2(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @n2(
+; CHECK-NEXT:    [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    call void @use8(i8 [[I0]])
+; CHECK-NEXT:    [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 %x, %y ; extra use
+  call void @use8(i8 %i0)
+  %r = sub i8 %i0, %z
+  ret i8 %r
+}
+
+; What if some operand is constant?
+
+define i8 @t3_c0(i8 %y, i8 %z) {
+; CHECK-LABEL: @t3_c0(
+; CHECK-NEXT:    [[I0:%.*]] = sub i8 42, [[Y:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 42, %y
+  %r = sub i8 %i0, %z
+  ret i8 %r
+}
+
+define i8 @t4_c1(i8 %x, i8 %z) {
+; CHECK-LABEL: @t4_c1(
+; CHECK-NEXT:    [[I0:%.*]] = add i8 [[X:%.*]], -42
+; CHECK-NEXT:    [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 %x, 42
+  %r = sub i8 %i0, %z
+  ret i8 %r
+}
+
+define i8 @t5_c2(i8 %x, i8 %y) {
+; CHECK-LABEL: @t5_c2(
+; CHECK-NEXT:    [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = add i8 [[I0]], -42
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 %x, %y
+  %r = sub i8 %i0, 42
+  ret i8 %r
+}
+
+; What if some operand is constant and there was extra use?
+
+define i8 @t6_c0_extrause(i8 %y, i8 %z) {
+; CHECK-LABEL: @t6_c0_extrause(
+; CHECK-NEXT:    [[I0:%.*]] = sub i8 42, [[Y:%.*]]
+; CHECK-NEXT:    call void @use8(i8 [[I0]])
+; CHECK-NEXT:    [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 42, %y
+  call void @use8(i8 %i0)
+  %r = sub i8 %i0, %z
+  ret i8 %r
+}
+
+define i8 @t7_c1_extrause(i8 %x, i8 %z) {
+; CHECK-LABEL: @t7_c1_extrause(
+; CHECK-NEXT:    [[I0:%.*]] = add i8 [[X:%.*]], -42
+; CHECK-NEXT:    call void @use8(i8 [[I0]])
+; CHECK-NEXT:    [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 %x, 42
+  call void @use8(i8 %i0)
+  %r = sub i8 %i0, %z
+  ret i8 %r
+}
+
+define i8 @t8_c2_extrause(i8 %x, i8 %y) {
+; CHECK-LABEL: @t8_c2_extrause(
+; CHECK-NEXT:    [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    call void @use8(i8 [[I0]])
+; CHECK-NEXT:    [[R:%.*]] = add i8 [[I0]], -42
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 %x, %y
+  call void @use8(i8 %i0)
+  %r = sub i8 %i0, 42
+  ret i8 %r
+}
+
+; What if two operands are constants?
+
+define i8 @t9_c0_c2(i8 %y, i8 %z) {
+; CHECK-LABEL: @t9_c0_c2(
+; CHECK-NEXT:    [[R:%.*]] = sub i8 18, [[Y:%.*]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 42, %y
+  %r = sub i8 %i0, 24
+  ret i8 %r
+}
+
+define i8 @t10_c1_c2(i8 %x, i8 %z) {
+; CHECK-LABEL: @t10_c1_c2(
+; CHECK-NEXT:    [[R:%.*]] = add i8 [[X:%.*]], -66
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 %x, 42
+  %r = sub i8 %i0, 24
+  ret i8 %r
+}
+
+; What if two operands are constants and there was extra use?
+
+define i8 @t11_c0_c2_extrause(i8 %y, i8 %z) {
+; CHECK-LABEL: @t11_c0_c2_extrause(
+; CHECK-NEXT:    [[I0:%.*]] = sub i8 42, [[Y:%.*]]
+; CHECK-NEXT:    call void @use8(i8 [[I0]])
+; CHECK-NEXT:    [[R:%.*]] = sub i8 18, [[Y]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 42, %y
+  call void @use8(i8 %i0)
+  %r = sub i8 %i0, 24
+  ret i8 %r
+}
+
+define i8 @t12_c1_c2_exrause(i8 %x, i8 %z) {
+; CHECK-LABEL: @t12_c1_c2_exrause(
+; CHECK-NEXT:    [[I0:%.*]] = add i8 [[X:%.*]], -42
+; CHECK-NEXT:    call void @use8(i8 [[I0]])
+; CHECK-NEXT:    [[R:%.*]] = add i8 [[X]], -66
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i0 = sub i8 %x, 42
+  call void @use8(i8 %i0)
+  %r = sub i8 %i0, 24
+  ret i8 %r
+}


        


More information about the llvm-commits mailing list