2002-05-06 05:35:20 +00:00
|
|
|
; This test makes sure that div instructions are properly eliminated.
|
|
|
|
|
2011-04-30 18:15:53 +00:00
|
|
|
; RUN: opt < %s -instcombine -S | FileCheck %s
|
2002-05-06 05:35:20 +00:00
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i32 @test1(i32 %A) {
|
|
|
|
%B = sdiv i32 %A, 1 ; <i32> [#uses=1]
|
|
|
|
ret i32 %B
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test1
|
|
|
|
; CHECK-NEXT: ret i32 %A
|
2003-02-18 19:16:45 +00:00
|
|
|
}
|
2003-02-18 19:28:47 +00:00
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i32 @test2(i32 %A) {
|
|
|
|
; => Shift
|
|
|
|
%B = udiv i32 %A, 8 ; <i32> [#uses=1]
|
|
|
|
ret i32 %B
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test2
|
|
|
|
; CHECK-NEXT: lshr i32 %A, 3
|
2003-02-18 19:28:47 +00:00
|
|
|
}
|
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i32 @test3(i32 %A) {
|
|
|
|
; => 0, don't need to keep traps
|
|
|
|
%B = sdiv i32 0, %A ; <i32> [#uses=1]
|
|
|
|
ret i32 %B
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test3
|
|
|
|
; CHECK-NEXT: ret i32 0
|
2003-02-18 19:28:47 +00:00
|
|
|
}
|
2004-04-26 14:01:47 +00:00
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i32 @test4(i32 %A) {
|
|
|
|
; 0-A
|
|
|
|
%B = sdiv i32 %A, -1 ; <i32> [#uses=1]
|
|
|
|
ret i32 %B
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test4
|
|
|
|
; CHECK-NEXT: sub i32 0, %A
|
2004-04-26 14:01:47 +00:00
|
|
|
}
|
2004-09-28 18:21:01 +00:00
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i32 @test5(i32 %A) {
|
|
|
|
%B = udiv i32 %A, -16 ; <i32> [#uses=1]
|
|
|
|
%C = udiv i32 %B, -4 ; <i32> [#uses=1]
|
|
|
|
ret i32 %C
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test5
|
|
|
|
; CHECK-NEXT: ret i32 0
|
2004-09-28 18:21:01 +00:00
|
|
|
}
|
2004-09-29 17:37:07 +00:00
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i1 @test6(i32 %A) {
|
|
|
|
%B = udiv i32 %A, 123 ; <i32> [#uses=1]
|
|
|
|
; A < 123
|
|
|
|
%C = icmp eq i32 %B, 0 ; <i1> [#uses=1]
|
|
|
|
ret i1 %C
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test6
|
|
|
|
; CHECK-NEXT: icmp ult i32 %A, 123
|
2008-03-18 03:45:45 +00:00
|
|
|
}
|
2004-09-29 17:37:07 +00:00
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i1 @test7(i32 %A) {
|
|
|
|
%B = udiv i32 %A, 10 ; <i32> [#uses=1]
|
|
|
|
; A >= 20 && A < 30
|
|
|
|
%C = icmp eq i32 %B, 2 ; <i1> [#uses=1]
|
|
|
|
ret i1 %C
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test7
|
|
|
|
; CHECK-NEXT: add i32 %A, -20
|
|
|
|
; CHECK-NEXT: icmp ult i32
|
2004-09-29 17:37:07 +00:00
|
|
|
}
|
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i1 @test8(i8 %A) {
|
|
|
|
%B = udiv i8 %A, 123 ; <i8> [#uses=1]
|
|
|
|
; A >= 246
|
|
|
|
%C = icmp eq i8 %B, 2 ; <i1> [#uses=1]
|
|
|
|
ret i1 %C
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test8
|
|
|
|
; CHECK-NEXT: icmp ugt i8 %A, -11
|
2008-03-18 03:45:45 +00:00
|
|
|
}
|
2004-09-29 17:37:07 +00:00
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i1 @test9(i8 %A) {
|
|
|
|
%B = udiv i8 %A, 123 ; <i8> [#uses=1]
|
|
|
|
; A < 246
|
|
|
|
%C = icmp ne i8 %B, 2 ; <i1> [#uses=1]
|
|
|
|
ret i1 %C
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test9
|
|
|
|
; CHECK-NEXT: icmp ult i8 %A, -10
|
2008-03-18 03:45:45 +00:00
|
|
|
}
|
2004-09-29 17:37:07 +00:00
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i32 @test10(i32 %X, i1 %C) {
|
|
|
|
%V = select i1 %C, i32 64, i32 8 ; <i32> [#uses=1]
|
|
|
|
%R = udiv i32 %X, %V ; <i32> [#uses=1]
|
|
|
|
ret i32 %R
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test10
|
|
|
|
; CHECK-NEXT: select i1 %C, i32 6, i32 3
|
|
|
|
; CHECK-NEXT: lshr i32 %X
|
2004-12-12 21:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-03-18 03:45:45 +00:00
|
|
|
define i32 @test11(i32 %X, i1 %C) {
|
|
|
|
%A = select i1 %C, i32 1024, i32 32 ; <i32> [#uses=1]
|
|
|
|
%B = udiv i32 %X, %A ; <i32> [#uses=1]
|
|
|
|
ret i32 %B
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test11
|
|
|
|
; CHECK-NEXT: select i1 %C, i32 10, i32 5
|
|
|
|
; CHECK-NEXT: lshr i32 %X
|
2006-02-05 07:52:47 +00:00
|
|
|
}
|
2008-05-16 02:59:42 +00:00
|
|
|
|
|
|
|
; PR2328
|
|
|
|
define i32 @test12(i32 %x) nounwind {
|
|
|
|
%tmp3 = udiv i32 %x, %x ; 1
|
|
|
|
ret i32 %tmp3
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test12
|
|
|
|
; CHECK-NEXT: ret i32 1
|
2008-05-16 02:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @test13(i32 %x) nounwind {
|
|
|
|
%tmp3 = sdiv i32 %x, %x ; 1
|
|
|
|
ret i32 %tmp3
|
2011-04-30 18:15:53 +00:00
|
|
|
; CHECK: @test13
|
|
|
|
; CHECK-NEXT: ret i32 1
|
2008-05-16 02:59:42 +00:00
|
|
|
}
|
|
|
|
|
2011-04-30 18:16:00 +00:00
|
|
|
define i32 @test14(i8 %x) nounwind {
|
|
|
|
%zext = zext i8 %x to i32
|
|
|
|
%div = udiv i32 %zext, 257 ; 0
|
|
|
|
ret i32 %div
|
|
|
|
; CHECK: @test14
|
|
|
|
; CHECK-NEXT: ret i32 0
|
|
|
|
}
|
Carve out a place in instcombine to put transformations which work knowing that their
result is non-zero. Implement an example optimization (PR9814), which allows us to
transform:
A / ((1 << B) >>u 2)
into:
A >>u (B-2)
which we compile into:
_divu3: ## @divu3
leal -2(%rsi), %ecx
shrl %cl, %edi
movl %edi, %eax
ret
instead of:
_divu3: ## @divu3
movb %sil, %cl
movl $1, %esi
shll %cl, %esi
shrl $2, %esi
movl %edi, %eax
xorl %edx, %edx
divl %esi, %eax
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@131860 91177308-0d34-0410-b5e6-96231b3b80d8
2011-05-22 18:18:41 +00:00
|
|
|
|
|
|
|
; PR9814
|
|
|
|
define i32 @test15(i32 %a, i32 %b) nounwind {
|
|
|
|
%shl = shl i32 1, %b
|
|
|
|
%div = lshr i32 %shl, 2
|
|
|
|
%div2 = udiv i32 %a, %div
|
|
|
|
ret i32 %div2
|
|
|
|
; CHECK: @test15
|
|
|
|
; CHECK-NEXT: add i32 %b, -2
|
|
|
|
; CHECK-NEXT: lshr i32 %a,
|
|
|
|
; CHECK-NEXT: ret i32
|
|
|
|
}
|
|
|
|
|
|
|
|
|