brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · 59a7c9f Raw
48 lines · c
1// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s2// RUN: %clang_cc1 %s -emit-llvm -triple i686-unknown-unknown -o - | FileCheck %s3// RUN: %clang_cc1 %s -emit-llvm -triple x86_64-unknown-unknown -o - | FileCheck %s4 5#include <stdint.h>6 7// This test is meant to verify code that handles the 'p = nullptr + n' idiom8// used by some versions of glibc and gcc.  This is undefined behavior but9// it is intended there to act like a conversion from a pointer-sized integer10// 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: test119// CHECK: inttoptr20// CHECK-NOT: getelementptr21 22// 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;25}26// CHECK-LABEL: test227// CHECK: getelementptr28// CHECK-NOT: inttoptr29 30// This doesn't meet the idiom because the offset is subtracted.31int8_t* test3(intptr_t n) {32  return NULLPTRI8 - n;33}34// CHECK-LABEL: test335// CHECK: getelementptr36// CHECK-NOT: inttoptr37 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 to40// make sure that doesn't cause problems on targets where integers and pointers41// are not the same size.42int8_t *test4(int8_t b) {43  return NULLPTRI8 + b;44}45// CHECK-LABEL: test446// CHECK: inttoptr47// CHECK-NOT: getelementptr48