78 lines · cpp
1// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir2// RUN: FileCheck --input-file=%t.cir %s3 4// Should generate basic pointer arithmetics.5void foo(int *iptr, char *cptr, unsigned ustride) {6 iptr + 2;7 // CHECK: %[[#STRIDE:]] = cir.const #cir.int<2> : !s32i8 // CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i>9 cptr + 3;10 // CHECK: %[[#STRIDE:]] = cir.const #cir.int<3> : !s32i11 // CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s8i>, !s32i) -> !cir.ptr<!s8i>12 iptr - 2;13 // CHECK: %[[#STRIDE:]] = cir.const #cir.int<2> : !s32i14 // CHECK: %[[#NEGSTRIDE:]] = cir.unary(minus, %[[#STRIDE]]) : !s32i, !s32i15 // CHECK: cir.ptr_stride %{{.+}}, %[[#NEGSTRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i>16 cptr - 3;17 // CHECK: %[[#STRIDE:]] = cir.const #cir.int<3> : !s32i18 // CHECK: %[[#NEGSTRIDE:]] = cir.unary(minus, %[[#STRIDE]]) : !s32i, !s32i19 // CHECK: cir.ptr_stride %{{.+}}, %[[#NEGSTRIDE]] : (!cir.ptr<!s8i>, !s32i) -> !cir.ptr<!s8i>20 iptr + ustride;21 // CHECK: %[[#STRIDE:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!u32i>, !u32i22 // CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !u32i) -> !cir.ptr<!s32i>23 24 // Must convert unsigned stride to a signed one.25 iptr - ustride;26 // CHECK: %[[#STRIDE:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!u32i>, !u32i27 // CHECK: %[[#SIGNSTRIDE:]] = cir.cast integral %[[#STRIDE]] : !u32i -> !s32i28 // CHECK: %[[#NEGSTRIDE:]] = cir.unary(minus, %[[#SIGNSTRIDE]]) : !s32i, !s32i29 // CHECK: cir.ptr_stride %{{.+}}, %[[#NEGSTRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i>30 31 4 + iptr;32 // CHECK: %[[#STRIDE:]] = cir.const #cir.int<4> : !s32i33 // CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i>34 35 iptr++;36 // CHECK: %[[#STRIDE:]] = cir.const #cir.int<1> : !s32i37 // CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i>38 39 iptr--;40 // CHECK: %[[#STRIDE:]] = cir.const #cir.int<-1> : !s32i41 // CHECK: cir.ptr_stride %{{.+}}, %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i>42}43 44void testPointerSubscriptAccess(int *ptr) {45// CHECK: testPointerSubscriptAccess46 ptr[1];47 // CHECK: %[[#STRIDE:]] = cir.const #cir.int<1> : !s32i48 // CHECK: %[[#PTR:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>49 // CHECK: cir.ptr_stride %[[#PTR]], %[[#STRIDE]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i>50}51 52void testPointerMultiDimSubscriptAccess(int **ptr) {53// CHECK: testPointerMultiDimSubscriptAccess54 ptr[1][2];55 // CHECK: %[[#STRIDE2:]] = cir.const #cir.int<2> : !s32i56 // CHECK: %[[#STRIDE1:]] = cir.const #cir.int<1> : !s32i57 // CHECK: %[[#PTR1:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!cir.ptr<!cir.ptr<!s32i>>>, !cir.ptr<!cir.ptr<!s32i>>58 // CHECK: %[[#PTR2:]] = cir.ptr_stride %[[#PTR1]], %[[#STRIDE1]] : (!cir.ptr<!cir.ptr<!s32i>>, !s32i) -> !cir.ptr<!cir.ptr<!s32i>>59 // CHECK: %[[#PTR3:]] = cir.load{{.*}} %[[#PTR2]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>60 // CHECK: cir.ptr_stride %[[#PTR3]], %[[#STRIDE2]] : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i>61}62 63// This test is meant to verify code that handles the 'p = nullptr + n' idiom64// used by some versions of glibc and gcc. This is undefined behavior but65// it is intended there to act like a conversion from a pointer-sized integer66// to a pointer, and we would like to tolerate that.67 68#define NULLPTRINT ((int*)0)69 70// This should get the inttoptr instruction.71int *testGnuNullPtrArithmetic(unsigned n) {72// CHECK: testGnuNullPtrArithmetic73 return NULLPTRINT + n;74 // CHECK: %[[NULLPTR:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i>75 // CHECK: %[[N:.*]] = cir.load{{.*}} %{{.*}} : !cir.ptr<!u32i>, !u32i76 // CHECK: %[[RESULT:.*]] = cir.ptr_stride %[[NULLPTR]], %[[N]] : (!cir.ptr<!s32i>, !u32i) -> !cir.ptr<!s32i>77}78