blob: 4c62df132ded965a9ea27b33707ea83503a7d63a [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
20// This doesn't meet the idiom because the offset type isn't pointer-sized.
21int8_t *test2(int8_t n) {
22 return NULLPTRI8 + n;
23}
24// CHECK-LABEL: test2
25// CHECK: getelementptr
26// CHECK-NOT: inttoptr
27
28// This doesn't meet the idiom because the element type is larger than a byte.
29int16_t *test3(intptr_t n) {
30 return (int16_t*)0 + n;
31}
32// CHECK-LABEL: test3
33// CHECK: getelementptr
34// CHECK-NOT: inttoptr
35
36// This doesn't meet the idiom because the offset is subtracted.
37int8_t* test4(intptr_t n) {
38 return NULLPTRI8 - n;
39}
40// CHECK-LABEL: test4
41// CHECK: getelementptr
42// CHECK-NOT: inttoptr