1 // RUN: %clang_cc1 -S %s -emit-llvm -o - | FileCheck %s
2 
3 #include <stdint.h>
4 
5 // This test is meant to verify code that handles the 'p = nullptr + n' idiom
6 // used by some versions of glibc and gcc.  This is undefined behavior but
7 // it is intended there to act like a conversion from a pointer-sized integer
8 // to a pointer, and we would like to tolerate that.
9 
10 #define NULLPTRI8 ((int8_t*)0)
11 
12 // This should get the inttoptr instruction.
13 int8_t *test1(intptr_t n) {
14   return NULLPTRI8 + n;
15 }
16 // CHECK-LABEL: test1
17 // CHECK: inttoptr
18 // CHECK-NOT: getelementptr
19 
20 // This doesn't meet the idiom because the element type is larger than a byte.
21 int16_t *test2(intptr_t n) {
22   return (int16_t*)0 + n;
23 }
24 // CHECK-LABEL: test2
25 // CHECK: getelementptr
26 // CHECK-NOT: inttoptr
27 
28 // This doesn't meet the idiom because the offset is subtracted.
29 int8_t* test3(intptr_t n) {
30   return NULLPTRI8 - n;
31 }
32 // CHECK-LABEL: test3
33 // CHECK: getelementptr
34 // CHECK-NOT: inttoptr
35