1 ; This test makes sure that div instructions are properly eliminated. 2 3 ; RUN: opt < %s -instcombine -S | FileCheck %s 4 5 define i32 @test1(i32 %A) { 6 %B = sdiv i32 %A, 1 ; <i32> [#uses=1] 7 ret i32 %B 8 ; CHECK-LABEL: @test1( 9 ; CHECK-NEXT: ret i32 %A 10 } 11 12 define i32 @test2(i32 %A) { 13 ; => Shift 14 %B = udiv i32 %A, 8 ; <i32> [#uses=1] 15 ret i32 %B 16 ; CHECK-LABEL: @test2( 17 ; CHECK-NEXT: lshr i32 %A, 3 18 } 19 20 define i32 @test3(i32 %A) { 21 ; => 0, don't need to keep traps 22 %B = sdiv i32 0, %A ; <i32> [#uses=1] 23 ret i32 %B 24 ; CHECK-LABEL: @test3( 25 ; CHECK-NEXT: ret i32 0 26 } 27 28 define i32 @test4(i32 %A) { 29 ; 0-A 30 %B = sdiv i32 %A, -1 ; <i32> [#uses=1] 31 ret i32 %B 32 ; CHECK-LABEL: @test4( 33 ; CHECK-NEXT: sub i32 0, %A 34 } 35 36 define i32 @test5(i32 %A) { 37 %B = udiv i32 %A, -16 ; <i32> [#uses=1] 38 %C = udiv i32 %B, -4 ; <i32> [#uses=1] 39 ret i32 %C 40 ; CHECK-LABEL: @test5( 41 ; CHECK-NEXT: ret i32 0 42 } 43 44 define i1 @test6(i32 %A) { 45 %B = udiv i32 %A, 123 ; <i32> [#uses=1] 46 ; A < 123 47 %C = icmp eq i32 %B, 0 ; <i1> [#uses=1] 48 ret i1 %C 49 ; CHECK-LABEL: @test6( 50 ; CHECK-NEXT: icmp ult i32 %A, 123 51 } 52 53 define i1 @test7(i32 %A) { 54 %B = udiv i32 %A, 10 ; <i32> [#uses=1] 55 ; A >= 20 && A < 30 56 %C = icmp eq i32 %B, 2 ; <i1> [#uses=1] 57 ret i1 %C 58 ; CHECK-LABEL: @test7( 59 ; CHECK-NEXT: add i32 %A, -20 60 ; CHECK-NEXT: icmp ult i32 61 } 62 63 define i1 @test8(i8 %A) { 64 %B = udiv i8 %A, 123 ; <i8> [#uses=1] 65 ; A >= 246 66 %C = icmp eq i8 %B, 2 ; <i1> [#uses=1] 67 ret i1 %C 68 ; CHECK-LABEL: @test8( 69 ; CHECK-NEXT: icmp ugt i8 %A, -11 70 } 71 72 define i1 @test9(i8 %A) { 73 %B = udiv i8 %A, 123 ; <i8> [#uses=1] 74 ; A < 246 75 %C = icmp ne i8 %B, 2 ; <i1> [#uses=1] 76 ret i1 %C 77 ; CHECK-LABEL: @test9( 78 ; CHECK-NEXT: icmp ult i8 %A, -10 79 } 80 81 define i32 @test10(i32 %X, i1 %C) { 82 %V = select i1 %C, i32 64, i32 8 ; <i32> [#uses=1] 83 %R = udiv i32 %X, %V ; <i32> [#uses=1] 84 ret i32 %R 85 ; CHECK-LABEL: @test10( 86 ; CHECK-NEXT: select i1 %C, i32 6, i32 3 87 ; CHECK-NEXT: lshr i32 %X 88 } 89 90 define i32 @test11(i32 %X, i1 %C) { 91 %A = select i1 %C, i32 1024, i32 32 ; <i32> [#uses=1] 92 %B = udiv i32 %X, %A ; <i32> [#uses=1] 93 ret i32 %B 94 ; CHECK-LABEL: @test11( 95 ; CHECK-NEXT: select i1 %C, i32 10, i32 5 96 ; CHECK-NEXT: lshr i32 %X 97 } 98 99 ; PR2328 100 define i32 @test12(i32 %x) nounwind { 101 %tmp3 = udiv i32 %x, %x ; 1 102 ret i32 %tmp3 103 ; CHECK-LABEL: @test12( 104 ; CHECK-NEXT: ret i32 1 105 } 106 107 define i32 @test13(i32 %x) nounwind { 108 %tmp3 = sdiv i32 %x, %x ; 1 109 ret i32 %tmp3 110 ; CHECK-LABEL: @test13( 111 ; CHECK-NEXT: ret i32 1 112 } 113 114 define i32 @test14(i8 %x) nounwind { 115 %zext = zext i8 %x to i32 116 %div = udiv i32 %zext, 257 ; 0 117 ret i32 %div 118 ; CHECK-LABEL: @test14( 119 ; CHECK-NEXT: ret i32 0 120 } 121 122 ; PR9814 123 define i32 @test15(i32 %a, i32 %b) nounwind { 124 %shl = shl i32 1, %b 125 %div = lshr i32 %shl, 2 126 %div2 = udiv i32 %a, %div 127 ret i32 %div2 128 ; CHECK-LABEL: @test15( 129 ; CHECK-NEXT: add i32 %b, -2 130 ; CHECK-NEXT: lshr i32 %a, 131 ; CHECK-NEXT: ret i32 132 } 133 134 define <2 x i64> @test16(<2 x i64> %x) nounwind { 135 %shr = lshr <2 x i64> %x, <i64 3, i64 5> 136 %div = udiv <2 x i64> %shr, <i64 4, i64 6> 137 ret <2 x i64> %div 138 ; CHECK-LABEL: @test16( 139 ; CHECK-NEXT: udiv <2 x i64> %x, <i64 32, i64 192> 140 ; CHECK-NEXT: ret <2 x i64> 141 } 142 143 define <2 x i64> @test17(<2 x i64> %x) nounwind { 144 %neg = sub nsw <2 x i64> zeroinitializer, %x 145 %div = sdiv <2 x i64> %neg, <i64 3, i64 4> 146 ret <2 x i64> %div 147 ; CHECK-LABEL: @test17( 148 ; CHECK-NEXT: sdiv <2 x i64> %x, <i64 -3, i64 -4> 149 ; CHECK-NEXT: ret <2 x i64> 150 } 151 152 define <2 x i64> @test18(<2 x i64> %x) nounwind { 153 %div = sdiv <2 x i64> %x, <i64 -1, i64 -1> 154 ret <2 x i64> %div 155 ; CHECK-LABEL: @test18( 156 ; CHECK-NEXT: sub <2 x i64> zeroinitializer, %x 157 ; CHECK-NEXT: ret <2 x i64> 158 } 159 160 define i32 @test19(i32 %x) { 161 %A = udiv i32 1, %x 162 ret i32 %A 163 ; CHECK-LABEL: @test19( 164 ; CHECK-NEXT: icmp eq i32 %x, 1 165 ; CHECK-NEXT: zext i1 %{{.*}} to i32 166 ; CHECK-NEXT ret i32 167 } 168 169 define i32 @test20(i32 %x) { 170 %A = sdiv i32 1, %x 171 ret i32 %A 172 ; CHECK-LABEL: @test20( 173 ; CHECK-NEXT: add i32 %x, 1 174 ; CHECK-NEXT: icmp ult i32 %{{.*}}, 3 175 ; CHECK-NEXT: select i1 %{{.*}}, i32 %x, i32 {{.*}} 176 ; CHECK-NEXT: ret i32 177 } 178