| 1 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir |
| 2 |
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s |
| 3 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll |
| 4 |
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s |
| 5 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll |
| 6 |
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s |
| 7 |
|
| 8 |
int addrcmp(const void* a, const void* b) { |
| 9 |
// CIR-LABEL: addrcmp |
| 10 |
// CIR: %[[R:.*]] = cir.ptr_diff |
| 11 |
// CIR: cir.cast integral %[[R]] : !s64i -> !s32i |
| 12 |
|
| 13 |
// LLVM-LABEL: define dso_local i32 @addrcmp( |
| 14 |
// LLVM: %[[PTR_A:.*]] = ptrtoint ptr {{.*}} to i64 |
| 15 |
// LLVM: %[[PTR_B:.*]] = ptrtoint ptr {{.*}} to i64 |
| 16 |
// LLVM: %[[SUB:.*]] = sub i64 %[[PTR_A]], %[[PTR_B]] |
| 17 |
// LLVM-NOT: sdiv |
| 18 |
// LLVM: trunc i64 %[[SUB]] to i32 |
| 19 |
|
| 20 |
// OGCG-LABEL: define dso_local i32 @addrcmp( |
| 21 |
// OGCG: %[[PTR_A:.*]] = ptrtoint ptr {{.*}} to i64 |
| 22 |
// OGCG: %[[PTR_B:.*]] = ptrtoint ptr {{.*}} to i64 |
| 23 |
// OGCG: %[[SUB:.*]] = sub i64 %[[PTR_A]], %[[PTR_B]] |
| 24 |
// OGCG-NOT: sdiv |
| 25 |
// OGCG: trunc i64 %[[SUB]] to i32 |
| 26 |
return *(const void**)a - *(const void**)b; |
| 27 |
} |
| 28 |
|
| 29 |
unsigned long long test_ptr_diff(int *a, int* b) { |
| 30 |
// CIR-LABEL: test_ptr_diff |
| 31 |
// CIR: %[[D:.*]] = cir.ptr_diff {{.*}} : !cir.ptr<!s32i> -> !s64i |
| 32 |
// CIR: %[[U:.*]] = cir.cast integral %[[D]] : !s64i -> !u64i |
| 33 |
// CIR: cir.return {{.*}} : !u64i |
| 34 |
|
| 35 |
// LLVM-LABEL: define dso_local i64 @test_ptr_diff( |
| 36 |
// LLVM: %[[IA:.*]] = ptrtoint ptr %{{.*}} to i64 |
| 37 |
// LLVM: %[[IB:.*]] = ptrtoint ptr %{{.*}} to i64 |
| 38 |
// LLVM: %[[SUB:.*]] = sub i64 %[[IA]], %[[IB]] |
| 39 |
// LLVM: %[[Q:.*]] = sdiv exact i64 %[[SUB]], 4 |
| 40 |
// LLVM: store i64 %[[Q]], ptr %[[RETADDR:.*]], align |
| 41 |
// LLVM: %[[RETLOAD:.*]] = load i64, ptr %[[RETADDR]], align |
| 42 |
// LLVM: ret i64 %[[RETLOAD]] |
| 43 |
|
| 44 |
// OGCG-LABEL: define dso_local i64 @test_ptr_diff( |
| 45 |
// OGCG: %[[IA:.*]] = ptrtoint ptr %{{.*}} to i64 |
| 46 |
// OGCG: %[[IB:.*]] = ptrtoint ptr %{{.*}} to i64 |
| 47 |
// OGCG: %[[SUB:.*]] = sub i64 %[[IA]], %[[IB]] |
| 48 |
// OGCG: %[[Q:.*]] = sdiv exact i64 %[[SUB]], 4 |
| 49 |
// OGCG: ret i64 %[[Q]] |
| 50 |
return a - b; |
| 51 |
} |