blob: ce9c9765b0f7ce377dc2fd54ca8459e62cb7bec7 [file] [log] [blame]
Andrew Kaylor3d0a5402017-09-19 20:26:40 +00001// RUN: %clang_cc1 -S %s -emit-llvm -o - | FileCheck %s
Andrew Kaylorb9be5362017-09-20 18:06:44 +00002// 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 Kaylor3d0a5402017-09-19 20:26:40 +00004
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.
15int8_t *test1(intptr_t n) {
16 return NULLPTRI8 + n;
17}
18// CHECK-LABEL: test1
19// CHECK: inttoptr
20// CHECK-NOT: getelementptr
21
Andrew Kaylor21a2aa72017-09-19 21:43:01 +000022// This doesn't meet the idiom because the element type is larger than a byte.
23int16_t *test2(intptr_t n) {
24 return (int16_t*)0 + n;
Andrew Kaylor3d0a5402017-09-19 20:26:40 +000025}
26// CHECK-LABEL: test2
27// CHECK: getelementptr
28// CHECK-NOT: inttoptr
29
Andrew Kaylor3d0a5402017-09-19 20:26:40 +000030// This doesn't meet the idiom because the offset is subtracted.
Andrew Kaylor21a2aa72017-09-19 21:43:01 +000031int8_t* test3(intptr_t n) {
Andrew Kaylor3d0a5402017-09-19 20:26:40 +000032 return NULLPTRI8 - n;
33}
Andrew Kaylor21a2aa72017-09-19 21:43:01 +000034// CHECK-LABEL: test3
Andrew Kaylor3d0a5402017-09-19 20:26:40 +000035// CHECK: getelementptr
36// CHECK-NOT: inttoptr
Andrew Kaylorb9be5362017-09-20 18:06:44 +000037
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.
42int8_t *test4(int8_t b) {
43 return NULLPTRI8 + b;
44}
45// CHECK-LABEL: test4
46// CHECK: inttoptr
47// CHECK-NOT: getelementptr