+define i32 @test1(i32 %A) {
+ %c1 = bitcast i32 %A to i32 ; <i32> [#uses=1]
+ %c2 = bitcast i32 %c1 to i32 ; <i32> [#uses=1]
+ ret i32 %c2
+; CHECK: ret i32 %A
+}
+
+define i64 @test2(i8 %A) {
+ %c1 = zext i8 %A to i16 ; <i16> [#uses=1]
+ %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1]
+ %Ret = zext i32 %c2 to i64 ; <i64> [#uses=1]
+ ret i64 %Ret
+; CHECK: %Ret = zext i8 %A to i64
+; CHECK: ret i64 %Ret
+}
+
+; This function should just use bitwise AND
+define i64 @test3(i64 %A) {
+ %c1 = trunc i64 %A to i8 ; <i8> [#uses=1]
+ %c2 = zext i8 %c1 to i64 ; <i64> [#uses=1]
+ ret i64 %c2
+; CHECK: %c2 = and i64 %A, 255
+; CHECK: ret i64 %c2
+}
+
+define i32 @test4(i32 %A, i32 %B) {
+ %COND = icmp slt i32 %A, %B ; <i1> [#uses=1]
+ ; Booleans are unsigned integrals
+ %c = zext i1 %COND to i8 ; <i8> [#uses=1]
+ ; for the cast elim purpose
+ %result = zext i8 %c to i32 ; <i32> [#uses=1]
+ ret i32 %result
+; CHECK: %COND = icmp slt i32 %A, %B
+; CHECK: %result = zext i1 %COND to i32
+; CHECK: ret i32 %result
+}
+
+define i32 @test5(i1 %B) {
+ ; This cast should get folded into
+ %c = zext i1 %B to i8 ; <i8> [#uses=1]
+ ; this cast
+ %result = zext i8 %c to i32 ; <i32> [#uses=1]
+ ret i32 %result
+; CHECK: %result = zext i1 %B to i32
+; CHECK: ret i32 %result
+}
+
+define i32 @test6(i64 %A) {
+ %c1 = trunc i64 %A to i32 ; <i32> [#uses=1]
+ %res = bitcast i32 %c1 to i32 ; <i32> [#uses=1]
+ ret i32 %res
+; CHECK: trunc i64 %A to i32
+; CHECK-NEXT: ret i32
+}
+
+define i64 @test7(i1 %A) {
+ %c1 = zext i1 %A to i32 ; <i32> [#uses=1]
+ %res = sext i32 %c1 to i64 ; <i64> [#uses=1]
+ ret i64 %res
+; CHECK: %res = zext i1 %A to i64
+; CHECK: ret i64 %res
+}
+
+define i64 @test8(i8 %A) {
+ %c1 = sext i8 %A to i64 ; <i64> [#uses=1]
+ %res = bitcast i64 %c1 to i64 ; <i64> [#uses=1]
+ ret i64 %res
+; CHECK: = sext i8 %A to i64
+; CHECK-NEXT: ret i64
+}
+
+define i16 @test9(i16 %A) {
+ %c1 = sext i16 %A to i32 ; <i32> [#uses=1]
+ %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1]
+ ret i16 %c2
+; CHECK: ret i16 %A
+}
+
+define i16 @test10(i16 %A) {
+ %c1 = sext i16 %A to i32 ; <i32> [#uses=1]
+ %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1]
+ ret i16 %c2
+; CHECK: ret i16 %A
+}
+
+declare void @varargs(i32, ...)
+
+define void @test11(i32* %P) {
+ %c = bitcast i32* %P to i16* ; <i16*> [#uses=1]
+ call void (i32, ...)* @varargs( i32 5, i16* %c )
+ ret void
+; CHECK: call void (i32, ...)* @varargs(i32 5, i32* %P)
+; CHECK: ret void
+}
+
+define i32* @test12() {
+ %p = malloc [4 x i8] ; <[4 x i8]*> [#uses=1]
+ %c = bitcast [4 x i8]* %p to i32* ; <i32*> [#uses=1]
+ ret i32* %c
+; CHECK: %malloccall = tail call i8* @malloc(i32 4)
+; CHECK: ret i32* %c
+}
+
+define i8* @test13(i64 %A) {
+ %c = getelementptr [0 x i8]* bitcast ([32832 x i8]* @inbuf to [0 x i8]*), i64 0, i64 %A ; <i8*> [#uses=1]
+ ret i8* %c
+; CHECK: %c = getelementptr [32832 x i8]* @inbuf, i64 0, i64 %A
+; CHECK: ret i8* %c
+}
+
+define i1 @test14(i8 %A) {
+ %c = bitcast i8 %A to i8 ; <i8> [#uses=1]
+ %X = icmp ult i8 %c, -128 ; <i1> [#uses=1]
+ ret i1 %X
+; CHECK: %X = icmp sgt i8 %A, -1
+; CHECK: ret i1 %X
+}
+
+
+; This just won't occur when there's no difference between ubyte and sbyte
+;bool %test15(ubyte %A) {
+; %c = cast ubyte %A to sbyte
+; %X = setlt sbyte %c, 0 ; setgt %A, 127
+; ret bool %X
+;}
+
+define i1 @test16(i32* %P) {
+ %c = icmp ne i32* %P, null ; <i1> [#uses=1]
+ ret i1 %c
+; CHECK: %c = icmp ne i32* %P, null
+; CHECK: ret i1 %c
+}
+
+define i16 @test17(i1 %tmp3) {
+ %c = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
+ %t86 = trunc i32 %c to i16 ; <i16> [#uses=1]
+ ret i16 %t86
+; CHECK: %t86 = zext i1 %tmp3 to i16
+; CHECK: ret i16 %t86
+}
+
+define i16 @test18(i8 %tmp3) {
+ %c = sext i8 %tmp3 to i32 ; <i32> [#uses=1]
+ %t86 = trunc i32 %c to i16 ; <i16> [#uses=1]
+ ret i16 %t86
+; CHECK: %t86 = sext i8 %tmp3 to i16
+; CHECK: ret i16 %t86
+}
+
+define i1 @test19(i32 %X) {
+ %c = sext i32 %X to i64 ; <i64> [#uses=1]
+ %Z = icmp slt i64 %c, 12345 ; <i1> [#uses=1]
+ ret i1 %Z
+; CHECK: %Z = icmp slt i32 %X, 12345
+; CHECK: ret i1 %Z
+}
+
+define i1 @test20(i1 %B) {
+ %c = zext i1 %B to i32 ; <i32> [#uses=1]
+ %D = icmp slt i32 %c, -1 ; <i1> [#uses=1]
+ ;; false
+ ret i1 %D
+; CHECK: ret i1 false
+}
+
+define i32 @test21(i32 %X) {
+ %c1 = trunc i32 %X to i8 ; <i8> [#uses=1]
+ ;; sext -> zext -> and -> nop
+ %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1]
+ %RV = and i32 %c2, 255 ; <i32> [#uses=1]
+ ret i32 %RV
+; CHECK: %c21 = and i32 %X, 255
+; CHECK: ret i32 %c21
+}
+
+define i32 @test22(i32 %X) {
+ %c1 = trunc i32 %X to i8 ; <i8> [#uses=1]
+ ;; sext -> zext -> and -> nop
+ %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1]
+ %RV = shl i32 %c2, 24 ; <i32> [#uses=1]
+ ret i32 %RV
+; CHECK: shl i32 %X, 24
+; CHECK-NEXT: ret i32
+}
+
+define i32 @test23(i32 %X) {
+ ;; Turn into an AND even though X
+ %c1 = trunc i32 %X to i16 ; <i16> [#uses=1]
+ ;; and Z are signed.
+ %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1]
+ ret i32 %c2
+; CHECK: %c2 = and i32 %X, 65535
+; CHECK: ret i32 %c2
+}
+
+define i1 @test24(i1 %C) {
+ %X = select i1 %C, i32 14, i32 1234 ; <i32> [#uses=1]
+ ;; Fold cast into select
+ %c = icmp ne i32 %X, 0 ; <i1> [#uses=1]
+ ret i1 %c
+; CHECK: ret i1 true
+}
+
+define void @test25(i32** %P) {
+ %c = bitcast i32** %P to float** ; <float**> [#uses=1]
+ ;; Fold cast into null
+ store float* null, float** %c
+ ret void
+; CHECK: store i32* null, i32** %P
+; CHECK: ret void
+}