1 ; RUN: opt < %s -instcombine -S | FileCheck %s 2 3 ; PR1738 4 define i1 @test1(double %X, double %Y) { 5 %tmp9 = fcmp ord double %X, 0.000000e+00 6 %tmp13 = fcmp ord double %Y, 0.000000e+00 7 %bothcond = and i1 %tmp13, %tmp9 8 ret i1 %bothcond 9 ; CHECK: fcmp ord double %Y, %X 10 } 11 12 define i1 @test2(i1 %X, i1 %Y) { 13 %a = and i1 %X, %Y 14 %b = and i1 %a, %X 15 ret i1 %b 16 ; CHECK-LABEL: @test2( 17 ; CHECK-NEXT: and i1 %X, %Y 18 ; CHECK-NEXT: ret 19 } 20 21 define i32 @test3(i32 %X, i32 %Y) { 22 %a = and i32 %X, %Y 23 %b = and i32 %Y, %a 24 ret i32 %b 25 ; CHECK-LABEL: @test3( 26 ; CHECK-NEXT: and i32 %X, %Y 27 ; CHECK-NEXT: ret 28 } 29 30 define i1 @test4(i32 %X) { 31 %a = icmp ult i32 %X, 31 32 %b = icmp slt i32 %X, 0 33 %c = and i1 %a, %b 34 ret i1 %c 35 ; CHECK-LABEL: @test4( 36 ; CHECK-NEXT: ret i1 false 37 } 38 39 ; Make sure we don't go into an infinite loop with this test 40 define <4 x i32> @test5(<4 x i32> %A) { 41 %1 = xor <4 x i32> %A, <i32 1, i32 2, i32 3, i32 4> 42 %2 = and <4 x i32> <i32 1, i32 2, i32 3, i32 4>, %1 43 ret <4 x i32> %2 44 } 45 46 ; Check that we combine "if x!=0 && x!=-1" into "if x+1u>1" 47 define i32 @test6(i64 %x) nounwind { 48 ; CHECK-LABEL: @test6( 49 ; CHECK-NEXT: add i64 %x, 1 50 ; CHECK-NEXT: icmp ugt i64 %x.off, 1 51 %cmp1 = icmp ne i64 %x, -1 52 %not.cmp = icmp ne i64 %x, 0 53 %.cmp1 = and i1 %cmp1, %not.cmp 54 %land.ext = zext i1 %.cmp1 to i32 55 ret i32 %land.ext 56 } 57 58 define i1 @test7(i32 %i, i1 %b) { 59 ; CHECK-LABEL: @test7( 60 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 %i, 0 61 ; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP]], %b 62 ; CHECK-NEXT: ret i1 [[AND]] 63 %cmp1 = icmp slt i32 %i, 1 64 %cmp2 = icmp sgt i32 %i, -1 65 %and1 = and i1 %cmp1, %b 66 %and2 = and i1 %and1, %cmp2 67 ret i1 %and2 68 } 69 70 define i1 @test8(i32 %i) { 71 ; CHECK-LABEL: @test8( 72 ; CHECK-NEXT: [[DEC:%.*]] = add i32 %i, -1 73 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[DEC]], 13 74 ; CHECK-NEXT: ret i1 [[CMP]] 75 %cmp1 = icmp ne i32 %i, 0 76 %cmp2 = icmp ult i32 %i, 14 77 %cond = and i1 %cmp1, %cmp2 78 ret i1 %cond 79 } 80 81 ; combine -x & 1 into x & 1 82 define i64 @test9(i64 %x) { 83 ; CHECK-LABEL: @test9( 84 ; CHECK-NOT: %sub = sub nsw i64 0, %x 85 ; CHECK-NOT: %and = and i64 %sub, 1 86 ; CHECK-NEXT: %and = and i64 %x, 1 87 ; CHECK-NEXT: ret i64 %and 88 %sub = sub nsw i64 0, %x 89 %and = and i64 %sub, 1 90 ret i64 %and 91 } 92 93 define i64 @test10(i64 %x) { 94 ; CHECK-LABEL: @test10( 95 ; CHECK-NOT: %sub = sub nsw i64 0, %x 96 ; CHECK-NEXT: %and = and i64 %x, 1 97 ; CHECK-NOT: %add = add i64 %sub, %and 98 ; CHECK-NEXT: %add = sub i64 %and, %x 99 ; CHECK-NEXT: ret i64 %add 100 %sub = sub nsw i64 0, %x 101 %and = and i64 %sub, 1 102 %add = add i64 %sub, %and 103 ret i64 %add 104 } 105 106 define i64 @fabs_double(double %x) { 107 ; CHECK-LABEL: @fabs_double( 108 ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x) 109 ; CHECK-NEXT: %and = bitcast double %fabs to i64 110 ; CHECK-NEXT: ret i64 %and 111 %bc = bitcast double %x to i64 112 %and = and i64 %bc, 9223372036854775807 113 ret i64 %and 114 } 115 116 define i64 @fabs_double_swap(double %x) { 117 ; CHECK-LABEL: @fabs_double_swap( 118 ; CHECK-NEXT: %fabs = call double @llvm.fabs.f64(double %x) 119 ; CHECK-NEXT: %and = bitcast double %fabs to i64 120 ; CHECK-NEXT: ret i64 %and 121 %bc = bitcast double %x to i64 122 %and = and i64 9223372036854775807, %bc 123 ret i64 %and 124 } 125 126 define i32 @fabs_float(float %x) { 127 ; CHECK-LABEL: @fabs_float( 128 ; CHECK-NEXT: %fabs = call float @llvm.fabs.f32(float %x) 129 ; CHECK-NEXT: %and = bitcast float %fabs to i32 130 ; CHECK-NEXT: ret i32 %and 131 %bc = bitcast float %x to i32 132 %and = and i32 %bc, 2147483647 133 ret i32 %and 134 } 135 136 ; Make sure that only a bitcast is transformed. 137 138 define i64 @fabs_double_not_bitcast(double %x) { 139 ; CHECK-LABEL: @fabs_double_not_bitcast( 140 ; CHECK-NEXT: %bc = fptoui double %x to i64 141 ; CHECK-NEXT: %and = and i64 %bc, 9223372036854775807 142 ; CHECK-NEXT: ret i64 %and 143 %bc = fptoui double %x to i64 144 %and = and i64 %bc, 9223372036854775807 145 ret i64 %and 146 } 147 148