[llvm] r371537 - [NFC][InstCombine][InstSimplify] PR43251 - and some patterns with offset != 0
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 10 10:13:59 PDT 2019
Author: lebedevri
Date: Tue Sep 10 10:13:59 2019
New Revision: 371537
URL: http://llvm.org/viewvc/llvm-project?rev=371537&view=rev
Log:
[NFC][InstCombine][InstSimplify] PR43251 - and some patterns with offset != 0
https://rise4fun.com/Alive/21b
Added:
llvm/trunk/test/Transforms/InstSimplify/result-of-usub-by-nonzero-is-non-zero-and-no-overflow.ll
Modified:
llvm/trunk/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll
Modified: llvm/trunk/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll?rev=371537&r1=371536&r2=371537&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/result-of-usub-is-non-zero-and-no-overflow.ll Tue Sep 10 10:13:59 2019
@@ -11,6 +11,8 @@ declare void @use1(i1)
declare {i8, i1} @llvm.usub.with.overflow(i8, i8)
declare void @useagg({i8, i1})
+declare void @llvm.assume(i1)
+
; There is a number of base patterns..
define i1 @t0_noncanonical_ignoreme(i8 %base, i8 %offset) {
@@ -371,7 +373,7 @@ define i1 @t12(i8 %base, i8 %offset) {
;
%adjusted = sub i8 %base, %offset
call void @use8(i8 %adjusted)
- %underflow = icmp ugt i8 %adjusted, %base
+ %underflow = icmp ult i8 %base, %offset
call void @use1(i1 %underflow)
%null = icmp eq i8 %adjusted, 0
call void @use1(i1 %null)
@@ -421,3 +423,57 @@ define i1 @t13(i8 %base, i8 %offset) {
%r = and i1 %null, %underflow
ret i1 %r
}
+
+;-------------------------------------------------------------------------------
+
+define i1 @t15(i8 %base, i8 %offset) {
+; CHECK-LABEL: @t15(
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
+; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
+; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]])
+; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ult i8 [[ADJUSTED]], [[BASE]]
+; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]])
+; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[ADJUSTED]], 0
+; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]])
+; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp = icmp ne i8 %offset, 0
+ call void @llvm.assume(i1 %cmp)
+
+ %adjusted = sub i8 %base, %offset
+ call void @use8(i8 %adjusted)
+ %no_underflow = icmp ult i8 %adjusted, %base
+ call void @use1(i1 %no_underflow)
+ %not_null = icmp ne i8 %adjusted, 0
+ call void @use1(i1 %not_null)
+ %r = and i1 %not_null, %no_underflow
+ ret i1 %r
+}
+
+define i1 @t20(i8 %base, i8 %offset) {
+; CHECK-LABEL: @t20(
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
+; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
+; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]])
+; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[ADJUSTED]], [[BASE]]
+; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]])
+; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp eq i8 [[ADJUSTED]], 0
+; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]])
+; CHECK-NEXT: [[R:%.*]] = or i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp = icmp ne i8 %offset, 0
+ call void @llvm.assume(i1 %cmp)
+
+ %adjusted = sub i8 %base, %offset
+ call void @use8(i8 %adjusted)
+ %no_underflow = icmp uge i8 %adjusted, %base
+ call void @use1(i1 %no_underflow)
+ %not_null = icmp eq i8 %adjusted, 0
+ call void @use1(i1 %not_null)
+ %r = or i1 %not_null, %no_underflow
+ ret i1 %r
+}
Added: llvm/trunk/test/Transforms/InstSimplify/result-of-usub-by-nonzero-is-non-zero-and-no-overflow.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/result-of-usub-by-nonzero-is-non-zero-and-no-overflow.ll?rev=371537&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/result-of-usub-by-nonzero-is-non-zero-and-no-overflow.ll (added)
+++ llvm/trunk/test/Transforms/InstSimplify/result-of-usub-by-nonzero-is-non-zero-and-no-overflow.ll Tue Sep 10 10:13:59 2019
@@ -0,0 +1,84 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt %s -instsimplify -S | FileCheck %s
+
+; Here we subtract two values, check that subtraction did not overflow AND
+; that the result is non-zero. This can be simplified just to a comparison
+; between the base and offset.
+
+declare void @llvm.assume(i1)
+
+define i1 @t0(i8 %base, i8 %offset) {
+; CHECK-LABEL: @t0(
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
+; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
+; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[ADJUSTED]], [[BASE]]
+; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[ADJUSTED]], 0
+; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp = icmp ne i8 %offset, 0
+ call void @llvm.assume(i1 %cmp)
+
+ %adjusted = sub i8 %base, %offset
+ %no_underflow = icmp uge i8 %adjusted, %base
+ %not_null = icmp ne i8 %adjusted, 0
+ %r = and i1 %not_null, %no_underflow
+ ret i1 %r
+}
+
+define i1 @t1(i8 %base, i8 %offset) {
+; CHECK-LABEL: @t1(
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
+; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
+; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ult i8 [[ADJUSTED]], [[BASE]]
+; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp eq i8 [[ADJUSTED]], 0
+; CHECK-NEXT: [[R:%.*]] = or i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp = icmp ne i8 %offset, 0
+ call void @llvm.assume(i1 %cmp)
+
+ %adjusted = sub i8 %base, %offset
+ %no_underflow = icmp ult i8 %adjusted, %base
+ %not_null = icmp eq i8 %adjusted, 0
+ %r = or i1 %not_null, %no_underflow
+ ret i1 %r
+}
+
+define i1 @t2_commutative(i8 %base, i8 %offset) {
+; CHECK-LABEL: @t2_commutative(
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
+; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
+; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ule i8 [[BASE]], [[ADJUSTED]]
+; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[ADJUSTED]], 0
+; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp = icmp ne i8 %offset, 0
+ call void @llvm.assume(i1 %cmp)
+
+ %adjusted = sub i8 %base, %offset
+ %no_underflow = icmp ule i8 %base, %adjusted
+ %not_null = icmp ne i8 %adjusted, 0
+ %r = and i1 %not_null, %no_underflow
+ ret i1 %r
+}
+
+; We don't know that offset is non-zero, so we can't fold.
+define i1 @t3_bad(i8 %base, i8 %offset) {
+; CHECK-LABEL: @t3_bad(
+; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]]
+; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[ADJUSTED]], [[BASE]]
+; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[ADJUSTED]], 0
+; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %adjusted = sub i8 %base, %offset
+ %no_underflow = icmp uge i8 %adjusted, %base
+ %not_null = icmp ne i8 %adjusted, 0
+ %r = and i1 %not_null, %no_underflow
+ ret i1 %r
+}
More information about the llvm-commits
mailing list