| 1 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir |
| 2 |
// RUN: FileCheck --input-file=%t.cir %s |
| 3 |
|
| 4 |
// Should generate basic pointer arithmetics. |
| 5 |
void foo(int *iptr, char *cptr, unsigned ustride) { |
| 6 |
iptr + 2; |
| 7 |
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<2> : !s32i |
| 8 |
// CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i> |
| 9 |
cptr + 3; |
| 10 |
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<3> : !s32i |
| 11 |
// CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s8i>, !s32i) -> !cir.ptr<!s8i> |
| 12 |
|
| 13 |
// We need to assign to a temporary in these cases because otherwise |
| 14 |
// constant folding of the unary minus for thenegative stride value also |
| 15 |
// triggers erasing the unused result of the ptr_stride operation. |
| 16 |
int* iptr2 = iptr - 2; |
| 17 |
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<-2> : !s32i |
| 18 |
// CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i> |
| 19 |
char* cptr2 = cptr - 3; |
| 20 |
|
| 21 |
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<-3> : !s32i |
| 22 |
// CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s8i>, !s32i) -> !cir.ptr<!s8i> |
| 23 |
iptr + ustride; |
| 24 |
// CHECK: %[[#STRIDE:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!u32i>, !u32i |
| 25 |
// CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !u32i) -> !cir.ptr<!s32i> |
| 26 |
|
| 27 |
// Must convert unsigned stride to a signed one. |
| 28 |
iptr - ustride; |
| 29 |
// CHECK: %[[#STRIDE:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!u32i>, !u32i |
| 30 |
// CHECK: %[[#SIGNSTRIDE:]] = cir.cast integral %[[#STRIDE]] : !u32i -> !s32i |
| 31 |
// CHECK: %[[#NEGSTRIDE:]] = cir.unary(minus, %[[#SIGNSTRIDE]]) : !s32i, !s32i |
| 32 |
// CHECK: cir.ptr_stride %{{.+}}, %[[#NEGSTRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i> |
| 33 |
|
| 34 |
4 + iptr; |
| 35 |
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<4> : !s32i |
| 36 |
// CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i> |
| 37 |
|
| 38 |
iptr++; |
| 39 |
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<1> : !s32i |
| 40 |
// CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i> |
| 41 |
|
| 42 |
iptr--; |
| 43 |
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<-1> : !s32i |
| 44 |
// CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i> |
| 45 |
} |
| 46 |
|
| 47 |
void testPointerSubscriptAccess(int *ptr) { |
| 48 |
// CHECK: testPointerSubscriptAccess |
| 49 |
ptr[1]; |
| 50 |
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<1> : !s32i |
| 51 |
// CHECK: %[[#PTR:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i> |
| 52 |
// CHECK: cir.ptr_stride %[[#PTR]], %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i> |
| 53 |
} |
| 54 |
|
| 55 |
void testPointerMultiDimSubscriptAccess(int **ptr) { |
| 56 |
// CHECK: testPointerMultiDimSubscriptAccess |
| 57 |
ptr[1][2]; |
| 58 |
// CHECK: %[[#STRIDE2:]] = cir.const #cir.int<2> : !s32i |
| 59 |
// CHECK: %[[#STRIDE1:]] = cir.const #cir.int<1> : !s32i |
| 60 |
// CHECK: %[[#PTR1:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!cir.ptr<!cir.ptr<!s32i>>>, !cir.ptr<!cir.ptr<!s32i>> |
| 61 |
// CHECK: %[[#PTR2:]] = cir.ptr_stride %[[#PTR1]], %[[#STRIDE1]] : (!cir.ptr<!cir.ptr<!s32i>>, !s32i) -> !cir.ptr<!cir.ptr<!s32i>> |
| 62 |
// CHECK: %[[#PTR3:]] = cir.load{{.*}} %[[#PTR2]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i> |
| 63 |
// CHECK: cir.ptr_stride %[[#PTR3]], %[[#STRIDE2]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i> |
| 64 |
} |
| 65 |
|
| 66 |
// This test is meant to verify code that handles the 'p = nullptr + n' idiom |
| 67 |
// used by some versions of glibc and gcc. This is undefined behavior but |
| 68 |
// it is intended there to act like a conversion from a pointer-sized integer |
| 69 |
// to a pointer, and we would like to tolerate that. |
| 70 |
|
| 71 |
#define NULLPTRINT ((int*)0) |
| 72 |
|
| 73 |
// This should get the inttoptr instruction. |
| 74 |
int *testGnuNullPtrArithmetic(unsigned n) { |
| 75 |
// CHECK: testGnuNullPtrArithmetic |
| 76 |
return NULLPTRINT + n; |
| 77 |
// CHECK: %[[NULLPTR:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i> |
| 78 |
// CHECK: %[[N:.*]] = cir.load{{.*}} %{{.*}} : !cir.ptr<!u32i>, !u32i |
| 79 |
// CHECK: %[[RESULT:.*]] = cir.ptr_stride %[[NULLPTR]], %[[N]] : (!cir.ptr<!s32i>, !u32i) -> !cir.ptr<!s32i> |
| 80 |
} |