blob: 0ec1cc1e05266f6402ca1caa1c54332ae32660c2 [file] [log] [blame]
Richard Smith2c9f87c2012-08-24 00:54:33 +00001// RUN: %clang_cc1 -fcatch-undefined-behavior -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s
2
3// CHECK: @_Z17reference_binding
4void reference_binding(int *p) {
5 // C++ core issue 453: If an lvalue to which a reference is directly bound
6 // designates neither an existing object or function of an appropriate type,
7 // nor a region of storage of suitable size and alignment to contain an object
8 // of the reference's type, the behavior is undefined.
9
10 // CHECK: icmp ne {{.*}}, null
11
12 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
13 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
14
15 // CHECK: %[[PTRINT:.*]] = ptrtoint
16 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
17 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
18 int &r = *p;
19}
20
21struct S {
22 double d;
23 int a, b;
24 virtual int f();
25};
26
27// CHECK: @_Z13member_access
28void member_access(S *p) {
29 // (1) Check 'p' is appropriately sized and aligned for member access.
30
31 // FIXME: Check vptr is for 'S' or a class derived from 'S'.
32
33 // CHECK: icmp ne {{.*}}, null
34
35 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
36 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
37
38 // CHECK: %[[PTRINT:.*]] = ptrtoint
39 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
40 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
41
42 // (2) Check 'p->b' is appropriately sized and aligned for a load.
43
44 // FIXME: Suppress this in the trivial case of a member access, because we
45 // know we've just checked the member access expression itself.
46
47 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
48 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
49
50 // CHECK: %[[PTRINT:.*]] = ptrtoint
51 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
52 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
53 int k = p->b;
54
55 // (3) Check 'p' is appropriately sized and aligned for member function call.
56
57 // FIXME: Check vptr is for 'S' or a class derived from 'S'.
58
59 // CHECK: icmp ne {{.*}}, null
60
61 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
62 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
63
64 // CHECK: %[[PTRINT:.*]] = ptrtoint
65 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
66 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
67 k = p->f();
68}
Richard Smith9d3e2262012-08-25 00:32:28 +000069
70// CHECK: @_Z12lsh_overflow
71int lsh_overflow(int a, int b) {
72 // CHECK: %[[INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
73 // CHECK-NEXT: br i1 %[[INBOUNDS]]
74
75 // CHECK: %[[SHIFTED_OUT_WIDTH:.*]] = sub nuw nsw i32 31, %[[RHS]]
76 // CHECK-NEXT: %[[SHIFTED_OUT:.*]] = lshr i32 %[[LHS:.*]], %[[SHIFTED_OUT_WIDTH]]
77
78 // This is present for C++11 but not for C: C++ core issue 1457 allows a '1'
79 // to be shifted into the sign bit, but not out of it.
80 // CHECK-NEXT: %[[SHIFTED_OUT_NOT_SIGN:.*]] = lshr i32 %[[SHIFTED_OUT]], 1
81
82 // CHECK-NEXT: %[[NO_OVERFLOW:.*]] = icmp eq i32 %[[SHIFTED_OUT_NOT_SIGN]], 0
83 // CHECK-NEXT: br i1 %[[NO_OVERFLOW]]
84
85 // CHECK: %[[RET:.*]] = shl i32 %[[LHS]], %[[RHS]]
86 // CHECK-NEXT: ret i32 %[[RET]]
87 return a << b;
88}