[InstCombine] mark ADD with nuw if no unsigned overflow
[oota-llvm.git] / test / Transforms / InstCombine / AddOverFlow.ll
1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2
3 target 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
7 define 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
15 define 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
25 define 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
37 ; CHECK-LABEL: @ripple_nsw1
38 ; CHECK: add nsw i16 %a, %b
39 define i16 @ripple_nsw1(i16 %x, i16 %y) {
40 ; %a has at most one bit set
41   %a = and i16 %y, 1
42
43 ; %b has a 0 bit other than the sign bit
44   %b = and i16 %x, 49151
45
46   %c = add i16 %a, %b
47   ret i16 %c
48 }
49
50 ; Like the previous test, but flip %a and %b
51 ; CHECK-LABEL: @ripple_nsw2
52 ; CHECK: add nsw i16 %b, %a
53 define i16 @ripple_nsw2(i16 %x, i16 %y) {
54   %a = and i16 %y, 1
55   %b = and i16 %x, 49151
56   %c = add i16 %b, %a
57   ret i16 %c
58 }
59
60 ; CHECK-LABEL: @ripple_no_nsw1
61 ; CHECK: add i32 %a, %x
62 define i32 @ripple_no_nsw1(i32 %x, i32 %y) {
63 ; We know nothing about %x
64   %a = and i32 %y, 1
65   %b = add i32 %a, %x
66   ret i32 %b
67 }
68
69 ; CHECK-LABEL: @ripple_no_nsw2
70 ; CHECK: add nuw i16 %a, %b
71 define i16 @ripple_no_nsw2(i16 %x, i16 %y) {
72 ; %a has at most one bit set
73   %a = and i16 %y, 1
74
75 ; %b has a 0 bit, but it is the sign bit
76   %b = and i16 %x, 32767
77
78   %c = add i16 %a, %b
79   ret i16 %c
80 }