llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-codegen Author: Yingwei Zheng (dtcxzyw) <details> <summary>Changes</summary> Do not set inbounds on GEP when the pointer operand is a constant null. Note: This patch is fragile :( For a more complex case `(int*)0 + offset1 + offset2`, we still set inbounds for the second addition. Related issue: https://github.com/llvm/llvm-project/issues/137833 --- Full diff: https://github.com/llvm/llvm-project/pull/137849.diff 2 Files Affected: - (modified) clang/lib/CodeGen/CGExprScalar.cpp (+2-1) - (modified) clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c (+12) ``````````diff 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 `````````` </details> https://github.com/llvm/llvm-project/pull/137849 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits