Andrew Kaylor | 3d0a540 | 2017-09-19 20:26:40 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -S %s -emit-llvm -o - | FileCheck %s |
Andrew Kaylor | b9be536 | 2017-09-20 18:06:44 +0000 | [diff] [blame^] | 2 | // RUN: %clang_cc1 -S %s -emit-llvm -triple i686-unknown-unknown -o - | FileCheck %s |
| 3 | // RUN: %clang_cc1 -S %s -emit-llvm -triple x86_64-unknown-unknown -o - | FileCheck %s |
Andrew Kaylor | 3d0a540 | 2017-09-19 20:26:40 +0000 | [diff] [blame] | 4 | |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | // This test is meant to verify code that handles the 'p = nullptr + n' idiom |
| 8 | // used by some versions of glibc and gcc. This is undefined behavior but |
| 9 | // it is intended there to act like a conversion from a pointer-sized integer |
| 10 | // to a pointer, and we would like to tolerate that. |
| 11 | |
| 12 | #define NULLPTRI8 ((int8_t*)0) |
| 13 | |
| 14 | // This should get the inttoptr instruction. |
| 15 | int8_t *test1(intptr_t n) { |
| 16 | return NULLPTRI8 + n; |
| 17 | } |
| 18 | // CHECK-LABEL: test1 |
| 19 | // CHECK: inttoptr |
| 20 | // CHECK-NOT: getelementptr |
| 21 | |
Andrew Kaylor | 21a2aa7 | 2017-09-19 21:43:01 +0000 | [diff] [blame] | 22 | // This doesn't meet the idiom because the element type is larger than a byte. |
| 23 | int16_t *test2(intptr_t n) { |
| 24 | return (int16_t*)0 + n; |
Andrew Kaylor | 3d0a540 | 2017-09-19 20:26:40 +0000 | [diff] [blame] | 25 | } |
| 26 | // CHECK-LABEL: test2 |
| 27 | // CHECK: getelementptr |
| 28 | // CHECK-NOT: inttoptr |
| 29 | |
Andrew Kaylor | 3d0a540 | 2017-09-19 20:26:40 +0000 | [diff] [blame] | 30 | // This doesn't meet the idiom because the offset is subtracted. |
Andrew Kaylor | 21a2aa7 | 2017-09-19 21:43:01 +0000 | [diff] [blame] | 31 | int8_t* test3(intptr_t n) { |
Andrew Kaylor | 3d0a540 | 2017-09-19 20:26:40 +0000 | [diff] [blame] | 32 | return NULLPTRI8 - n; |
| 33 | } |
Andrew Kaylor | 21a2aa7 | 2017-09-19 21:43:01 +0000 | [diff] [blame] | 34 | // CHECK-LABEL: test3 |
Andrew Kaylor | 3d0a540 | 2017-09-19 20:26:40 +0000 | [diff] [blame] | 35 | // CHECK: getelementptr |
| 36 | // CHECK-NOT: inttoptr |
Andrew Kaylor | b9be536 | 2017-09-20 18:06:44 +0000 | [diff] [blame^] | 37 | |
| 38 | // This checks the case where the offset isn't pointer-sized. |
| 39 | // The front end will implicitly cast the offset to an integer, so we need to |
| 40 | // make sure that doesn't cause problems on targets where integers and pointers |
| 41 | // are not the same size. |
| 42 | int8_t *test4(int8_t b) { |
| 43 | return NULLPTRI8 + b; |
| 44 | } |
| 45 | // CHECK-LABEL: test4 |
| 46 | // CHECK: inttoptr |
| 47 | // CHECK-NOT: getelementptr |