2009-11-04 09:05:20 +01:00
|
|
|
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
|
|
|
|
|
|
|
|
; Optimize subtracts.
|
2002-05-06 07:51:26 +02:00
|
|
|
;
|
2009-11-04 08:57:05 +01:00
|
|
|
; RUN: opt < %s -instcombine -S | FileCheck %s
|
2002-05-06 07:51:26 +02:00
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test1(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = sub i32 %A, %A
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %B
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test1
|
|
|
|
; CHECK: ret i32 0
|
2002-05-06 07:51:26 +02:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test2(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = sub i32 %A, 0
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %B
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test2
|
|
|
|
; CHECK: ret i32 %A
|
2002-05-06 07:51:26 +02:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test3(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = sub i32 0, %A
|
|
|
|
%C = sub i32 0, %B
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %C
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test3
|
|
|
|
; CHECK: ret i32 %A
|
2002-05-06 18:19:16 +02:00
|
|
|
}
|
2002-05-09 01:15:01 +02:00
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test4(i32 %A, i32 %x) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = sub i32 0, %A
|
|
|
|
%C = sub i32 %x, %B
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %C
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test4
|
|
|
|
; CHECK: %C = add i32 %x, %A
|
|
|
|
; CHECK: ret i32 %C
|
2002-05-09 01:15:01 +02:00
|
|
|
}
|
|
|
|
|
2009-11-04 08:57:05 +01:00
|
|
|
define i32 @test5(i32 %A, i32 %B, i32 %C) {
|
|
|
|
%D = sub i32 %B, %C
|
|
|
|
%E = sub i32 %A, %D
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %E
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test5
|
|
|
|
; CHECK: %D = sub i32 %C, %B
|
|
|
|
; CHECK: %E = add
|
|
|
|
; CHECK: ret i32 %E
|
2002-05-09 01:15:01 +02:00
|
|
|
}
|
2003-02-18 20:28:47 +01:00
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test6(i32 %A, i32 %B) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%C = and i32 %A, %B
|
|
|
|
%D = sub i32 %A, %C
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %D
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test6
|
|
|
|
; CHECK-NEXT: xor i32 %B, -1
|
|
|
|
; CHECK-NEXT: %D = and i32
|
|
|
|
; CHECK-NEXT: ret i32 %D
|
2003-02-18 20:28:47 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test7(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = sub i32 -1, %A
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %B
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test7
|
|
|
|
; CHECK: %B = xor i32 %A, -1
|
|
|
|
; CHECK: ret i32 %B
|
2003-02-18 20:28:47 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test8(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = mul i32 9, %A
|
|
|
|
%C = sub i32 %B, %A
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %C
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test8
|
|
|
|
; CHECK: %C = shl i32 %A, 3
|
|
|
|
; CHECK: ret i32 %C
|
2003-02-18 20:55:31 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test9(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = mul i32 3, %A
|
|
|
|
%C = sub i32 %A, %B
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %C
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test9
|
|
|
|
; CHECK: %C = mul i32 %A, -2
|
|
|
|
; CHECK: ret i32 %C
|
2003-02-18 20:55:31 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test10(i32 %A, i32 %B) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%C = sub i32 0, %A
|
|
|
|
%D = sub i32 0, %B
|
|
|
|
%E = mul i32 %C, %D
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %E
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test10
|
|
|
|
; CHECK: %E = mul i32 %A, %B
|
|
|
|
; CHECK: ret i32 %E
|
2003-03-11 00:22:50 +01:00
|
|
|
}
|
|
|
|
|
2009-11-04 08:57:05 +01:00
|
|
|
define i32 @test10a(i32 %A) {
|
|
|
|
%C = sub i32 0, %A
|
|
|
|
%E = mul i32 %C, 7
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %E
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test10a
|
|
|
|
; CHECK: %E = mul i32 %A, -7
|
|
|
|
; CHECK: ret i32 %E
|
2003-03-11 00:22:50 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i1 @test11(i8 %A, i8 %B) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%C = sub i8 %A, %B
|
|
|
|
%cD = icmp ne i8 %C, 0
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i1 %cD
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test11
|
|
|
|
; CHECK: %cD = icmp ne i8 %A, %B
|
|
|
|
; CHECK: ret i1 %cD
|
2003-08-13 07:27:57 +02:00
|
|
|
}
|
2004-03-13 00:52:51 +01:00
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test12(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = ashr i32 %A, 31
|
|
|
|
%C = sub i32 0, %B
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %C
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test12
|
|
|
|
; CHECK: %C = lshr i32 %A, 31
|
|
|
|
; CHECK: ret i32 %C
|
2004-03-13 00:52:51 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test13(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = lshr i32 %A, 31
|
|
|
|
%C = sub i32 0, %B
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %C
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test13
|
|
|
|
; CHECK: %C = ashr i32 %A, 31
|
|
|
|
; CHECK: ret i32 %C
|
2004-03-13 00:52:51 +01:00
|
|
|
}
|
2004-03-13 01:11:38 +01:00
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test14(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%B = lshr i32 %A, 31
|
|
|
|
%C = bitcast i32 %B to i32
|
|
|
|
%D = sub i32 0, %C
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %D
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test14
|
|
|
|
; CHECK: %D = ashr i32 %A, 31
|
|
|
|
; CHECK: ret i32 %D
|
2004-03-13 01:11:38 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test15(i32 %A, i32 %B) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%C = sub i32 0, %A
|
|
|
|
%D = srem i32 %B, %C
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %D
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test15
|
|
|
|
; CHECK: %D = srem i32 %B, %A
|
|
|
|
; CHECK: ret i32 %D
|
2004-07-06 09:00:11 +02:00
|
|
|
}
|
2004-10-06 17:07:56 +02:00
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test16(i32 %A) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%X = sdiv i32 %A, 1123
|
|
|
|
%Y = sub i32 0, %X
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %Y
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test16
|
|
|
|
; CHECK: %Y = sdiv i32 %A, -1123
|
|
|
|
; CHECK: ret i32 %Y
|
2004-10-06 17:07:56 +02:00
|
|
|
}
|
2004-10-09 04:50:01 +02:00
|
|
|
|
2008-11-30 04:42:12 +01:00
|
|
|
; Can't fold subtract here because negation it might oveflow.
|
|
|
|
; PR3142
|
2009-11-04 08:57:05 +01:00
|
|
|
define i32 @test17(i32 %A) {
|
|
|
|
%B = sub i32 0, %A
|
|
|
|
%C = sdiv i32 %B, 1234
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %C
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test17
|
|
|
|
; CHECK: %B = sub i32 0, %A
|
|
|
|
; CHECK: %C = sdiv i32 %B, 1234
|
|
|
|
; CHECK: ret i32 %C
|
2004-10-09 04:50:01 +02:00
|
|
|
}
|
2004-11-13 20:49:39 +01:00
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i64 @test18(i64 %Y) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%tmp.4 = shl i64 %Y, 2
|
|
|
|
%tmp.12 = shl i64 %Y, 2
|
|
|
|
%tmp.8 = sub i64 %tmp.4, %tmp.12
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i64 %tmp.8
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test18
|
|
|
|
; CHECK: ret i64 0
|
2004-11-13 20:49:39 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i32 @test19(i32 %X, i32 %Y) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%Z = sub i32 %X, %Y
|
|
|
|
%Q = add i32 %Z, %Y
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i32 %Q
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test19
|
|
|
|
; CHECK: ret i32 %X
|
2005-04-07 18:41:45 +02:00
|
|
|
}
|
2006-02-27 02:43:02 +01:00
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i1 @test20(i32 %g, i32 %h) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%tmp.2 = sub i32 %g, %h
|
|
|
|
%tmp.4 = icmp ne i32 %tmp.2, %g
|
2008-03-01 10:15:35 +01:00
|
|
|
ret i1 %tmp.4
|
2009-11-04 08:57:05 +01:00
|
|
|
; CHECK: @test20
|
|
|
|
; CHECK: %tmp.4 = icmp ne i32 %h, 0
|
|
|
|
; CHECK: ret i1 %tmp.4
|
2006-02-27 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 10:15:35 +01:00
|
|
|
define i1 @test21(i32 %g, i32 %h) {
|
2009-11-04 08:57:05 +01:00
|
|
|
%tmp.2 = sub i32 %g, %h
|
|
|
|
%tmp.4 = icmp ne i32 %tmp.2, %g
|
|
|
|
ret i1 %tmp.4
|
|
|
|
; CHECK: @test21
|
|
|
|
; CHECK: %tmp.4 = icmp ne i32 %h, 0
|
|
|
|
; CHECK: ret i1 %tmp.4
|
2006-02-27 02:43:02 +01:00
|
|
|
}
|
2008-05-09 07:19:28 +02:00
|
|
|
|
|
|
|
; PR2298
|
2009-11-04 08:57:05 +01:00
|
|
|
define i1 @test22(i32 %a, i32 %b) zeroext nounwind {
|
|
|
|
%tmp2 = sub i32 0, %a
|
|
|
|
%tmp4 = sub i32 0, %b
|
|
|
|
%tmp5 = icmp eq i32 %tmp2, %tmp4
|
|
|
|
ret i1 %tmp5
|
|
|
|
; CHECK: @test22
|
|
|
|
; CHECK: %tmp5 = icmp eq i32 %a, %b
|
|
|
|
; CHECK: ret i1 %tmp5
|
2008-05-09 07:19:28 +02:00
|
|
|
}
|
|
|
|
|
2009-11-04 09:05:20 +01:00
|
|
|
; rdar://7362831
|
|
|
|
define i32 @test23(i8* %P, i64 %A){
|
|
|
|
%B = getelementptr inbounds i8* %P, i64 %A
|
|
|
|
%C = ptrtoint i8* %B to i64
|
|
|
|
%D = trunc i64 %C to i32
|
|
|
|
%E = ptrtoint i8* %P to i64
|
|
|
|
%F = trunc i64 %E to i32
|
|
|
|
%G = sub i32 %D, %F
|
|
|
|
ret i32 %G
|
|
|
|
; CHECK: @test23
|
2010-01-05 22:16:30 +01:00
|
|
|
; CHECK-NEXT: = trunc i64 %A to i32
|
|
|
|
; CHECK-NEXT: ret i32
|
2009-11-04 09:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
define i64 @test24(i8* %P, i64 %A){
|
|
|
|
%B = getelementptr inbounds i8* %P, i64 %A
|
|
|
|
%C = ptrtoint i8* %B to i64
|
|
|
|
%E = ptrtoint i8* %P to i64
|
|
|
|
%G = sub i64 %C, %E
|
|
|
|
ret i64 %G
|
|
|
|
; CHECK: @test24
|
|
|
|
; CHECK-NEXT: ret i64 %A
|
|
|
|
}
|
|
|
|
|
|
|
|
define i64 @test24a(i8* %P, i64 %A){
|
|
|
|
%B = getelementptr inbounds i8* %P, i64 %A
|
|
|
|
%C = ptrtoint i8* %B to i64
|
|
|
|
%E = ptrtoint i8* %P to i64
|
|
|
|
%G = sub i64 %E, %C
|
|
|
|
ret i64 %G
|
|
|
|
; CHECK: @test24a
|
|
|
|
; CHECK-NEXT: sub i64 0, %A
|
|
|
|
; CHECK-NEXT: ret i64
|
|
|
|
}
|
|
|
|
|
2010-01-01 23:29:12 +01:00
|
|
|
@Arr = external global [42 x i16]
|
|
|
|
|
|
|
|
define i64 @test24b(i8* %P, i64 %A){
|
|
|
|
%B = getelementptr inbounds [42 x i16]* @Arr, i64 0, i64 %A
|
|
|
|
%C = ptrtoint i16* %B to i64
|
|
|
|
%G = sub i64 %C, ptrtoint ([42 x i16]* @Arr to i64)
|
|
|
|
ret i64 %G
|
|
|
|
; CHECK: @test24b
|
|
|
|
; CHECK-NEXT: shl i64 %A, 1
|
|
|
|
; CHECK-NEXT: ret i64
|
|
|
|
}
|
|
|
|
|
2010-01-01 23:42:29 +01:00
|
|
|
|
|
|
|
define i64 @test25(i8* %P, i64 %A){
|
|
|
|
%B = getelementptr inbounds [42 x i16]* @Arr, i64 0, i64 %A
|
|
|
|
%C = ptrtoint i16* %B to i64
|
|
|
|
%G = sub i64 %C, ptrtoint (i16* getelementptr ([42 x i16]* @Arr, i64 1, i64 0) to i64)
|
|
|
|
ret i64 %G
|
|
|
|
; CHECK: @test25
|
|
|
|
; CHECK-NEXT: shl i64 %A, 1
|
|
|
|
; CHECK-NEXT: add i64 {{.*}}, -84
|
|
|
|
; CHECK-NEXT: ret i64
|
|
|
|
}
|
|
|
|
|
2010-01-31 03:30:23 +01:00
|
|
|
define i32 @test26(i32 %x) {
|
|
|
|
%shl = shl i32 3, %x
|
|
|
|
%neg = sub i32 0, %shl
|
|
|
|
ret i32 %neg
|
|
|
|
; CHECK: @test26
|
|
|
|
; CHECK-NEXT: shl i32 -3
|
|
|
|
; CHECK-NEXT: ret i32
|
|
|
|
}
|
2010-01-01 23:42:29 +01:00
|
|
|
|