https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/137851
Closes https://github.com/llvm/llvm-project/issues/137833. This patch is stacked on https://github.com/llvm/llvm-project/pull/137849. >From f1db3a540ec1383451955efab62b64ed8d180349 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng <dtcxzyw2...@gmail.com> Date: Wed, 30 Apr 2025 01:26:41 +0800 Subject: [PATCH 1/3] [Clang][CodeGen] Check `isUnderlyingBasePointerConstantNull` in `emitPointerArithmetic` --- clang/lib/CodeGen/CGExprScalar.cpp | 3 ++- .../test/CodeGen/catch-nullptr-and-nonzero-offset.c | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 8dbbcdaef25d8..d214d2af52563 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -4238,7 +4238,8 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF, else elemTy = CGF.ConvertTypeForMem(elementType); - if (CGF.getLangOpts().PointerOverflowDefined) + if (CGF.getLangOpts().PointerOverflowDefined || + CGF.isUnderlyingBasePointerConstantNull(pointerOperand)) return CGF.Builder.CreateGEP(elemTy, pointer, index, "add.ptr"); return CGF.EmitCheckedInBoundsGEP( diff --git a/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c b/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c index 63b6db2c2adeb..c5ae3f8bcc368 100644 --- a/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c +++ b/clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c @@ -431,6 +431,18 @@ char *void_ptr(void *base, unsigned long offset) { return base + offset; } +int *constant_null_add(long offset) { + // CHECK: define{{.*}} ptr @constant_null_add(i64 noundef %[[OFFSET:.*]]) + // CHECK-NEXT: [[ENTRY:.*]]: + // CHECK-NEXT: %[[OFFSET_ADDR:.*]] = alloca i64, align 8 + // CHECK-NEXT: store i64 %[[OFFSET]], ptr %[[OFFSET_ADDR]], align 8 + // CHECK-NEXT: %[[OFFSET_RELOADED:.*]] = load i64, ptr %[[OFFSET_ADDR]], align 8 + // CHECK-NEXT: %[[ADD_PTR:.*]] = getelementptr i32, ptr null, i64 %[[OFFSET_RELOADED]] + // CHECK-NEXT: ret ptr %[[ADD_PTR]] +#line 1800 + return (int *)0 + offset; +} + #ifdef __cplusplus } #endif >From 630ce89088ef4978eafe884766712279a0d8fd56 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng <dtcxzyw2...@gmail.com> Date: Wed, 30 Apr 2025 01:47:45 +0800 Subject: [PATCH 2/3] [Clang][CodeGen] Add pre-commit tests. NFC. --- clang/test/CodeGen/glibc_ptr_align.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 clang/test/CodeGen/glibc_ptr_align.c diff --git a/clang/test/CodeGen/glibc_ptr_align.c b/clang/test/CodeGen/glibc_ptr_align.c new file mode 100644 index 0000000000000..ebdb53cf7b53d --- /dev/null +++ b/clang/test/CodeGen/glibc_ptr_align.c @@ -0,0 +1,17 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5 +// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -O3 -o - -emit-llvm %s | FileCheck %s + +// Make sure that we do not set inbounds flag if the base pointer may be a constant null. + +// CHECK-LABEL: define dso_local noalias noundef ptr @glibc_ptr_align( +// CHECK-SAME: ptr noundef readnone captures(none) [[BASE:%.*]], ptr noundef readnone captures(none) [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: ret ptr null +// +char *glibc_ptr_align(char *base, char *pointer, long align_mask) { + return (sizeof(long int) < sizeof(void *) ? (base) : (char *)0) + + (((pointer) - + (sizeof(long int) < sizeof(void *) ? (base) : (char *)0) + + (align_mask)) & + ~(align_mask)); +} >From ff88c23843d07a1c3142294a3d7c0894f0267a48 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng <dtcxzyw2...@gmail.com> Date: Wed, 30 Apr 2025 01:49:10 +0800 Subject: [PATCH 3/3] [Clang][CodeGen] Add workaround for old glibc `__PTR_ALIGN` macro --- clang/lib/CodeGen/CGExpr.cpp | 3 +++ clang/test/CodeGen/glibc_ptr_align.c | 11 ++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index bba7d1e805f3f..e9e22321e2634 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -4820,6 +4820,9 @@ bool CodeGenFunction::isUnderlyingBasePointerConstantNull(const Expr *E) { const Expr *UnderlyingBaseExpr = E->IgnoreParens(); while (auto *BaseMemberExpr = dyn_cast<MemberExpr>(UnderlyingBaseExpr)) UnderlyingBaseExpr = BaseMemberExpr->getBase()->IgnoreParens(); + if (auto *Select = dyn_cast<ConditionalOperator>(UnderlyingBaseExpr)) + return isUnderlyingBasePointerConstantNull(Select->getTrueExpr()) || + isUnderlyingBasePointerConstantNull(Select->getFalseExpr()); return getContext().isSentinelNullExpr(UnderlyingBaseExpr); } diff --git a/clang/test/CodeGen/glibc_ptr_align.c b/clang/test/CodeGen/glibc_ptr_align.c index ebdb53cf7b53d..14968a8326509 100644 --- a/clang/test/CodeGen/glibc_ptr_align.c +++ b/clang/test/CodeGen/glibc_ptr_align.c @@ -3,10 +3,15 @@ // Make sure that we do not set inbounds flag if the base pointer may be a constant null. -// CHECK-LABEL: define dso_local noalias noundef ptr @glibc_ptr_align( -// CHECK-SAME: ptr noundef readnone captures(none) [[BASE:%.*]], ptr noundef readnone captures(none) [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { +// CHECK-LABEL: define dso_local noalias ptr @glibc_ptr_align( +// CHECK-SAME: ptr noundef readnone captures(none) [[BASE:%.*]], ptr noundef [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { // CHECK-NEXT: [[ENTRY:.*:]] -// CHECK-NEXT: ret ptr null +// CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[POINTER]] to i64 +// CHECK-NEXT: [[ADD:%.*]] = add nsw i64 [[ALIGN_MASK]], [[SUB_PTR_LHS_CAST]] +// CHECK-NEXT: [[NOT:%.*]] = xor i64 [[ALIGN_MASK]], -1 +// CHECK-NEXT: [[AND:%.*]] = and i64 [[ADD]], [[NOT]] +// CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr i8, ptr null, i64 [[AND]] +// CHECK-NEXT: ret ptr [[ADD_PTR]] // char *glibc_ptr_align(char *base, char *pointer, long align_mask) { return (sizeof(long int) < sizeof(void *) ? (base) : (char *)0) + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits