blob: 0c8ad5e34dc96eede23ff750866f925e33c12e6d [file] [log] [blame]
Andrew Kaylor3d0a5402017-09-19 20:26:40 +00001// 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.
13int8_t *test1(intptr_t n) {
14 return NULLPTRI8 + n;
15}
16// CHECK-LABEL: test1
17// CHECK: inttoptr
18// CHECK-NOT: getelementptr
19
Andrew Kaylor21a2aa72017-09-19 21:43:01 +000020// This doesn't meet the idiom because the element type is larger than a byte.
21int16_t *test2(intptr_t n) {
22 return (int16_t*)0 + n;
Andrew Kaylor3d0a5402017-09-19 20:26:40 +000023}
24// CHECK-LABEL: test2
25// CHECK: getelementptr
26// CHECK-NOT: inttoptr
27
Andrew Kaylor3d0a5402017-09-19 20:26:40 +000028// This doesn't meet the idiom because the offset is subtracted.
Andrew Kaylor21a2aa72017-09-19 21:43:01 +000029int8_t* test3(intptr_t n) {
Andrew Kaylor3d0a5402017-09-19 20:26:40 +000030 return NULLPTRI8 - n;
31}
Andrew Kaylor21a2aa72017-09-19 21:43:01 +000032// CHECK-LABEL: test3
Andrew Kaylor3d0a5402017-09-19 20:26:40 +000033// CHECK: getelementptr
34// CHECK-NOT: inttoptr