1; RUN: opt < %s -instcombine -S | FileCheck %s 2 3target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 4 5; CHECK-LABEL: @oppositesign 6; CHECK: add nsw i16 %a, %b 7define i16 @oppositesign(i16 %x, i16 %y) { 8; %a is negative, %b is positive 9 %a = or i16 %x, 32768 10 %b = and i16 %y, 32767 11 %c = add i16 %a, %b 12 ret i16 %c 13} 14 15define i16 @zero_sign_bit(i16 %a) { 16; CHECK-LABEL: @zero_sign_bit( 17; CHECK-NEXT: and 18; CHECK-NEXT: add nuw 19; CHECK-NEXT: ret 20 %1 = and i16 %a, 32767 21 %2 = add i16 %1, 512 22 ret i16 %2 23} 24 25define i16 @zero_sign_bit2(i16 %a, i16 %b) { 26; CHECK-LABEL: @zero_sign_bit2( 27; CHECK-NEXT: and 28; CHECK-NEXT: and 29; CHECK-NEXT: add nuw 30; CHECK-NEXT: ret 31 %1 = and i16 %a, 32767 32 %2 = and i16 %b, 32767 33 %3 = add i16 %1, %2 34 ret i16 %3 35} 36 37declare i16 @bounded(i16 %input); 38declare i32 @__gxx_personality_v0(...); 39!0 = !{i16 0, i16 32768} ; [0, 32767] 40!1 = !{i16 0, i16 32769} ; [0, 32768] 41 42define i16 @add_bounded_values(i16 %a, i16 %b) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { 43; CHECK-LABEL: @add_bounded_values( 44entry: 45 %c = call i16 @bounded(i16 %a), !range !0 46 %d = invoke i16 @bounded(i16 %b) to label %cont unwind label %lpad, !range !0 47cont: 48; %c and %d are in [0, 32767]. Therefore, %c + %d doesn't unsigned overflow. 49 %e = add i16 %c, %d 50; CHECK: add nuw i16 %c, %d 51 ret i16 %e 52lpad: 53 %0 = landingpad { i8*, i32 } 54 filter [0 x i8*] zeroinitializer 55 ret i16 42 56} 57 58define i16 @add_bounded_values_2(i16 %a, i16 %b) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { 59; CHECK-LABEL: @add_bounded_values_2( 60entry: 61 %c = call i16 @bounded(i16 %a), !range !1 62 %d = invoke i16 @bounded(i16 %b) to label %cont unwind label %lpad, !range !1 63cont: 64; Similar to add_bounded_values, but %c and %d are in [0, 32768]. Therefore, 65; %c + %d may unsigned overflow and we cannot add NUW. 66 %e = add i16 %c, %d 67; CHECK: add i16 %c, %d 68 ret i16 %e 69lpad: 70 %0 = landingpad { i8*, i32 } 71 filter [0 x i8*] zeroinitializer 72 ret i16 42 73} 74 75; CHECK-LABEL: @ripple_nsw1 76; CHECK: add nsw i16 %a, %b 77define i16 @ripple_nsw1(i16 %x, i16 %y) { 78; %a has at most one bit set 79 %a = and i16 %y, 1 80 81; %b has a 0 bit other than the sign bit 82 %b = and i16 %x, 49151 83 84 %c = add i16 %a, %b 85 ret i16 %c 86} 87 88; Like the previous test, but flip %a and %b 89; CHECK-LABEL: @ripple_nsw2 90; CHECK: add nsw i16 %b, %a 91define i16 @ripple_nsw2(i16 %x, i16 %y) { 92 %a = and i16 %y, 1 93 %b = and i16 %x, 49151 94 %c = add i16 %b, %a 95 ret i16 %c 96} 97 98; CHECK-LABEL: @ripple_nsw3 99; CHECK: add nsw i16 %a, %b 100define i16 @ripple_nsw3(i16 %x, i16 %y) { 101 %a = and i16 %y, 43691 102 %b = and i16 %x, 21843 103 %c = add i16 %a, %b 104 ret i16 %c 105} 106 107; Like the previous test, but flip %a and %b 108; CHECK-LABEL: @ripple_nsw4 109; CHECK: add nsw i16 %b, %a 110define i16 @ripple_nsw4(i16 %x, i16 %y) { 111 %a = and i16 %y, 43691 112 %b = and i16 %x, 21843 113 %c = add i16 %b, %a 114 ret i16 %c 115} 116 117; CHECK-LABEL: @ripple_nsw5 118; CHECK: add nsw i16 %a, %b 119define i16 @ripple_nsw5(i16 %x, i16 %y) { 120 %a = or i16 %y, 43691 121 %b = or i16 %x, 54613 122 %c = add i16 %a, %b 123 ret i16 %c 124} 125 126; Like the previous test, but flip %a and %b 127; CHECK-LABEL: @ripple_nsw6 128; CHECK: add nsw i16 %b, %a 129define i16 @ripple_nsw6(i16 %x, i16 %y) { 130 %a = or i16 %y, 43691 131 %b = or i16 %x, 54613 132 %c = add i16 %b, %a 133 ret i16 %c 134} 135 136; CHECK-LABEL: @ripple_no_nsw1 137; CHECK: add i32 %a, %x 138define i32 @ripple_no_nsw1(i32 %x, i32 %y) { 139; We know nothing about %x 140 %a = and i32 %y, 1 141 %b = add i32 %a, %x 142 ret i32 %b 143} 144 145; CHECK-LABEL: @ripple_no_nsw2 146; CHECK: add nuw i16 %a, %b 147define i16 @ripple_no_nsw2(i16 %x, i16 %y) { 148; %a has at most one bit set 149 %a = and i16 %y, 1 150 151; %b has a 0 bit, but it is the sign bit 152 %b = and i16 %x, 32767 153 154 %c = add i16 %a, %b 155 ret i16 %c 156} 157 158; CHECK-LABEL: @ripple_no_nsw3 159; CHECK: add i16 %a, %b 160define i16 @ripple_no_nsw3(i16 %x, i16 %y) { 161 %a = and i16 %y, 43691 162 %b = and i16 %x, 21845 163 %c = add i16 %a, %b 164 ret i16 %c 165} 166 167; Like the previous test, but flip %a and %b 168; CHECK-LABEL: @ripple_no_nsw4 169; CHECK: add i16 %b, %a 170define i16 @ripple_no_nsw4(i16 %x, i16 %y) { 171 %a = and i16 %y, 43691 172 %b = and i16 %x, 21845 173 %c = add i16 %b, %a 174 ret i16 %c 175} 176 177; CHECK-LABEL: @ripple_no_nsw5 178; CHECK: add i16 %a, %b 179define i16 @ripple_no_nsw5(i16 %x, i16 %y) { 180 %a = or i16 %y, 43689 181 %b = or i16 %x, 54613 182 %c = add i16 %a, %b 183 ret i16 %c 184} 185 186; Like the previous test, but flip %a and %b 187; CHECK-LABEL: @ripple_no_nsw6 188; CHECK: add i16 %b, %a 189define i16 @ripple_no_nsw6(i16 %x, i16 %y) { 190 %a = or i16 %y, 43689 191 %b = or i16 %x, 54613 192 %c = add i16 %b, %a 193 ret i16 %c 194} 195